It's very common for a company to host a private maven reposiroty, so that the development team could share java artifacts, there are also lots of solutions for self hosted maven repository, e.g: Nexus from sonatype.com or JFrog Artifactory, these are cool products, but most of the time we just want a place to store the artifacts and could be downloaded by internal development team. if we could leverage the online storage service to act as our maven repository then we do not need to spend effort to manage the the repository by ourselvs and do not need to worry about the disk issue backup etc. here comes the solution to host maven reposority on AWS S3

maven-on-s3

setup s3 as maven repository

I have a terraform script to do this , the whole script could be found under my github account.

  • create the s3 bucket as my maven repository
resource "aws_s3_bucket" "corp_maven_repo" {
  bucket = "${var.bucket_name}"

  versioning {
    enabled = false
  }

  lifecycle {
    prevent_destroy = false
  }
}
  • create 2 users to access the s3 bucket, one user is called ro user which is used by the development team to download the artifact, the other user is called rw user which could be used by CI server to upload artifact to s3
# we need a service account user
resource "aws_iam_user" "s3_repo_user_rw" {
  name = "srv_${var.bucket_name}_rw"
}

# generate keys for service account user
resource "aws_iam_access_key" "s3_repo_user_keys_rw" {
  user = "${aws_iam_user.s3_repo_user_rw.name}"
}

# we need a service account user
resource "aws_iam_user" "s3_repo_user_ro" {
  name = "srv_${var.bucket_name}_ro"
}

# generate keys for service account user
resource "aws_iam_access_key" "s3_repo_user_keys_ro" {
  user = "${aws_iam_user.s3_repo_user_ro.name}"
}
//the access key
output "iam_access_key_id_rw" {
  value = "${aws_iam_access_key.s3_repo_user_keys_rw.id}"
}
//the access key secret
output "iam_access_key_secret_rw" {
  value = "${aws_iam_access_key.s3_repo_user_keys_rw.secret}"
}
//the access key
output "iam_access_key_id_ro" {
  value = "${aws_iam_access_key.s3_repo_user_keys_ro.id}"
}
//the access key secret
output "iam_access_key_secret_ro" {
  value = "${aws_iam_access_key.s3_repo_user_keys_ro.secret}"
}

use s3 as repository in gradle project

Gradle support s3 as maven repository out of box, so in build.gradle file, we could simply specify repositories as below

repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
        maven {
            credentials(AwsCredentials) {
                accessKey "xxxxx"
                secretKey "xxxxx"
            }
            url 's3://corp_maven_repo/maven/snapshots/'
        }
        maven {
            credentials(AwsCredentials) {
                accessKey "xxxxx"
                secretKey "xxxxx"
            }
            url 's3://corp_maven_repo/maven/releases/'
        }
    }

use s3 as repository in maven project

maven does not support s3 out of box, but there are plugins to support it, e.g: https://github.com/seahen/maven-s3-wagon. we could config the extention first

<build>
  <extensions>
    <extension>
      <groupId>com.github.seahen</groupId>
      <artifactId>maven-s3-wagon</artifactId>
      <version>[S3 Wagon Version]</version>
   </extension>
  </extensions>
</build>

then add the maven repository in the repositories

<repositories>
    <repository>
        <id>corp-releases</id>
        <url>s3://corp_maven_repo/maven/releases</url>
        <releases>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>        

to authenticat to s3, we need to a section in ~/.m2/settings.xml

<server>
  <id>corp-releases</id>
  <username>xxxx</username>
  <password>xxxx</password>
</server>

now we could use s3 as maven repository for both of maven and gradle projects