Loading

Using an AWS S3 Bucket as a Maven repository

Using an AWS S3 Bucket as a Maven repository

In this article, we will learn, how to use an AWS S3 bucket as a Maven repository. Amazon S3 looks to be a good option for keeping a private Maven repository or artifacts. It's an alternative to feature-rich Maven repository managers like Nexus and Artifactory. For Private artifacts, you might want to deploy your Jar or war files on the server and make sure that they are visible only to your team. Let's start the following steps for accessing AWS S3 Bucket as a Maven repository or artifacts.

1. Accessing the artifacts in Maven

Now, we need to configure Maven to be able to connect to your S3 bucket. Update the maven setting configuration file by editing the settings.xml file located in the .m2 folder [ location of the maven settings.xml file location will vary base on your setup]. According to the Maven documentation:

There are two locations where a settings.xml file may live:

The Maven install: $M2_HOME/conf/settings.xml

A user’s install: ${user.home}/.m2/settings.xml

Tip: If you need to create user-specific settings from scratch, it’s easiest to copy the global settings from your Maven >installation to your ${user.home}/.m2 directory. Maven’s default settings.xml is a template with comments and examples >so you can quickly tweak it to match your needs.

Once you find the settings.xml file locate the section of the file please add the following:

<pluginGroup>org.apache.maven.plugins</pluginGroup>
<pluginGroup>org.codehaus.mojo</pluginGroup>  

Now update section in the settings.xml file and ddd the following information:

<server>
    <id><your-maven-artifacts-id></id>
    <username><aws-access-key-id></username>
    <password><aws-secret-access-key-id></password>
</server>

the final output should look like this:

<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
   ...
   <servers>
     <server>
           <id><your-maven-artifacts-id></id>
    <username><aws-access-key-id></username>
    <password><aws-secret-access-key-id></password>
     </server>
   </servers>
   <pluginGroups>
     <pluginGroup>org.apache.maven.plugins</pluginGroup>
     <pluginGroup>org.codehaus.mojo</pluginGroup>
   </pluginGroups>
 </settings>

Once the settings have been updated please add the following information to your POM.xml file

<dependency>
        <groupId>com.example</groupId>
    <artifactId>custom-plugin</artifactId>
    <version>1.0.1</version>
</dependency>
<repositories>
    <repository>
        <id><your-maven-artifacts-id></id>
        <url>s3://<your-s3-bucket-name>/releases</url>
    </repository>
</repositories>
<build>
    ...
    <extensions>
        <extension>
            <groupId>com.allogy.maven.wagon</groupId>
            <artifactId>maven-s3-wagon</artifactId>
        </extension>
    </extensions>
</build>

We have used Maven Wagon to read from S3 Maven repositories and write to them with private permissions.

2. Accessing the artifacts in Gradle

To use the S3 repository in any Gradle project and access the artifacts, we need to modify the Gradle build file. Fortunately, Gradle supports S3 buckets as Maven repositories out-of-the-box.

repositories {
    mavenCentral()
    maven {
        url = 's3://<your-s3-bucket-name>/releases'
        credentials(AwsCredentials) {
            accessKey = '<your-aws-access-key>'
            secretKey = '<your-aws-secret-key>'
        }
    }
}

Once the Gradle properties have been updated please add a custom plugin to your build.gradle file.

compile group: 'com.example', name: 'custom-plugin', version: '0.0.1'

Thanks for reading and as always, feedback is very much appreciated. Please leave a comment at info@codinghub.net.

Related Articles