[JAVA] Build a Maven repository on AWS S3

If you need a Maven repository

There are many cases where you want to manage Java libraries in a private environment such as in-house. At a certain scale, we will introduce Nexus, but there are times when we want to make it a little easier. This entry is a way to build a Maven repository using AWS S3.

procedure

Create and use Maven repository in the following order.

  1. Create a bucket for Maven repository on S3
  2. Create a user to access the S3 Maven repository
  3. Library registration settings with Gradle plugin
  4. Client settings for using the library

1. Create a bucket for the Maven repository

There is nothing special to mention, and as usual, create a bucket for S3. Let's say you created a bucket called my-maven.

2. Create a user to access the Maven repository

Although not required, create a user for the Maven repository and add S3 permissions for proper access control. Also, at this timing, you will get the AWS ʻAccess key ID and Secret access key`.

Access policy


{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListAllMyBuckets"
            ],
            "Resource": "arn:aws:s3:::*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:*"
            ],
            "Resource": [
                "arn:aws:s3:::my-maven",
                "arn:aws:s3:::my-maven/*"
            ]
        }
    ]
}

3. Library registration settings with Gradle plugin

Apply the maven-publish plugin and configure the settings for registering the library.

build.gradle


apply plugin: 'maven-publish'

def tag = System.getenv('CIRCLE_TAG') //Detects git tag push(Example for CircleCI)
def buildVersion = "1.0.0"
group = 'com.example'
version = tag ? "${tag}-RELEASE" : "${buildVersion}-SNAPSHOT" //RELEASE for tag push, SNAPSHOT otherwise(This area is specified according to the actual operation)

//To include the source in the library registered in the repository
task sourceJar(type: Jar) {
    from sourceSets.main.allJava
}

publishing {
    repositories {
        maven {
            url "s3://my-maven"
            credentials(AwsCredentials) {
                accessKey System.getenv('AWS_ACCESS_KEY_ID')
                secretKey System.getenv('AWS_SECRET_ACCESS_KEY')
            }
        }
    }
    publications {
        mavenJava(MavenPublication) {
            from components.java
            artifact sourceJar {
                classifier "sources"
            }
        }
    }
}

Build the source and register the repository with the following commands. Normally, you will use it via CI etc.

$ gradle publish

4. Client settings for using the library

If you add the created Maven repository on the client side, you can use it like any other repository.

build.gradle


repositories {
	mavenCentral()
	//Add the following
	maven {
		url "s3://my-maven"
		credentials(AwsCredentials) {
			accessKey System.getenv('AWS_ACCESS_KEY_ID')
			secretKey System.getenv('AWS_SECRET_ACCESS_KEY')
		}
	}
}

Summary

It's easy to build, so it's a great way to manage your library when you need it. Also, this example was Gradle, but you can do the same with Maven.

Recommended Posts

Build a Maven repository on AWS S3
Build Maven repository on AWS S3 service
Build a Maven in-house repository on Google Cloud Storage
Build a Minecraft server on AWS
Build a Laravel environment on an AWS instance
Memo to build a Servlet environment on AWS EC2
Build a Ruby on Rails development environment on AWS Cloud9
Dependency management in Gradle using Maven repository on Amazon S3
Build a XAMPP environment on Ubuntu
How to create a Maven repository for 2020
Build a Java development environment on Mac
Build Java 8 development environment on AWS Cloud9
Build an environment with Docker on AWS
Build a JMeter environment on your Mac
Build a development environment on AWS EC2 with CentOS7 + Nginx + pm2 + Nuxt.js
[AWS SDK for Java] Set a retry policy on the S3 client
Build a test flow on CircleCI using Jib
How to deploy a container on AWS Lambda
Build a DHCP and NAT router on Ubuntu 16.04
Build a streaming server on your iOS app
I tried setting up a Maven remote repository
Build a Java runtime environment on Sakura VPS
How to build a Pytorch environment on Ubuntu
A high bill from AWS during development on rails ...
Maven repository local cache
Maven on CentOS 7 tutorial
java build a triangle
Creating a local repository
Build Zabbix on Ubuntu 20.04
Creating a docker host on AWS using Docker Machine (personal memorandum)
[Amateur remarks] Build multiple WordPress on AWS using Docker Compose
A memorandum when running Apache Maven on an in-house proxy
[Docker] Build a site on Hugo and publish it on GitHub
How to get inside a container running on AWS Fargate
How to deploy a kotlin (java) app on AWS fargate
A memo to build Jitsi Meet on Azure with docker-compose
Build a CentOS 8 virtual environment on your Mac with VirtualBox
I tried running a Docker container on AWS IoT Greengrass 2.0
How to deploy a Rails application on AWS (article summary)
With [AWS] CodeStar, you can build a Spring (Java) project running on Lambda in just 3 minutes! !!