Host Private Maven Repository Using Amazon S3

A private Maven repository is particularly useful when developing company’s internal projects which cannot be shared publicly such as on Maven Central. While there are multiple options for running your own private Maven repo – such as via Nexus Sonatype, there is a simples approach which works very well for small to medium sized companies.

Amazon’s S3 is one candidate for this simple approach. its high availability and the fact that it’s completely managed by Amazon makes it a reliable option. So lets proceed.

I’m going to use a Gradle project to demonstrate the steps but the process should be very similar with Maven as well.

Lets assume there are two Gradle projects – plugin-project and project-crud-api. Plugin-Project to be to installed in the maven repo and used in project-crud-api.

Step 1 – Start by choosing an S3 bucket – existing or new. Add two directories in it with the names “snapshot” and “release”. These will be used to manage release and snapshot versions.

Step 2 – Create a new IAM user which has read-write access to the two directories we just created. Ideally this used should have permission to access only our maven directories for improving system security.

Step 3 – In project-plugin‘s build.gradle add the following code to –

apply plugin: 'maven'
apply plugin: 'maven-publish'

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
            groupId '<your-group-id>'
            artifactId '<your-artifact-name>'
        }
    }

    repositories {
        maven {
            url "s3://<bucket-name>/${project.version.endsWith('-SNAPSHOT') ? 'snapshot' : 'release'}"
            credentials(AwsCredentials) {
                accessKey AWS_ACCESS_KEY
                secretKey AWS_SECRET_KEY
            }
        }
    }
}

Step 4 – Now we need to store our AWS S3 credentials somewhere. Storing them in sourcecode itself is not a good idea as it can get compromised if someone gets hands on your code. Instead we’ll store them in system’s gradle.property file.

Open ~/.gradle/gradle.properties file (for linux) and add following lines in it –

AWS_ACCESS_KEY=<your-aws-access-key>
AWS_SECRET_KEY=<your-aws-secret-key>

We are now ready to publish our first artifact to S3. From terminal execute

./gradlew ​clean publish

and your artifact should be uploaded to S3.

To use artifact form S3 in a project simply define the custom Maven repo in build.gradle –

repositories {
   maven {
      url "s3://<bucket-name>/release"
      credentials(AwsCredentials) {
         accessKey AWS_ACCESS_KEY
         secretKey AWS_SECRET_KEY
      }
   }
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s