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.
Create and use Maven repository in the following order.
There is nothing special to mention, and as usual, create a bucket for S3.
Let's say you created a bucket called my-maven
.
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/*"
]
}
]
}
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
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')
}
}
}
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