[JAVA] Dependency management in Gradle using Maven repository on Amazon S3

Overview

As the title suggests, it describes how to create a Maven repository on Amazon S3 and use it with Gradle.

When developing a module that depends on an in-house module, it is used to resolve and build dependencies on the server.

Goal of this article

The goals of this article are 1 and 2 below.

  1. Upload the library to Maven repository on S3
  2. Download the above library from another Gradle project

Regarding 2, specifically, make it possible to download the library when executing the "gradle build" command with the following build.gradle settings.

build.gradle


dependencies {
    compile 'jp.co.goalist:library:1.0.0'
}

The following content is beyond the scope of this article. Someday again.

Premise

Upload to Maven repository

Gradle project creation

Execute the following command.

mkdir gradle-upload-s3repo
cd gradle-upload-s3repo/
gradle init --type=java-library

The generated file looks like this.

.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   └── java
    │       └── Library.java
    └── test
        └── java
            └── LibraryTest.java

At this point, the contents of build.gradle are as follows. (Unnecessary comments and dependent library settings have been removed)

build.gradle


apply plugin: 'java'

//Dependent library is undefined, so
//No repositories and dependencies are needed at this time,
//I will use it later, so I will leave it.
repositories {
    jcenter()
}

dependencies {
}

For confirmation, type "gradle build" in the project root and "gradle-upload-s3repo.jar" will be generated in build / libs. So far, this is the default feature of the java plugin. After checking, let's delete the build result by "gradle clean".

Define groupId, artifactId, version

Maven uniquely defines a library with a combination of groupId, artifactId and version.

The library created this time follows the convention,

Key value
groupId jp.co.goalist
artifactId library
version 1.0.0

Let's.

Add the following settings to build.gradle.

build.gradle


group = "jp.co.goalist"
version = "1.0.0" 

Modify settings.gradle as follows.

settings.gradle


rootProject.name = 'library'

Create a bucket for your repository on S3

We'll skip the steps, but create a bucket to use as the repository.

This time, create "s3: //repository.hoge/maven/".

Set access information to S3 from the development environment

Set the repository URL, IAM user access key ID and secret access key in ~ / .gradle / gradle.properties. By referring to this on the project side, it is not necessary to include it in the project, so it is safe in terms of security. (I'm saying normal things) You don't have to set the access key ID etc. in each project.

gradle.properties


goalistRepoUrl=s3://repository.hoge/maven/
awsAccessKeyId = AKIAJWORQEXXXXXXXXXX
awsSecretAccessKey = waTa0aakgK2e5hXXXXXXXXXXXXXXXXXXXXXXXXXX

Set up loadArchives task for maven plugin

Use the maven plugin uploadArchives to upload to the Maven repository. Add the settings to refer to the repository URL, IAM user access key ID, and secret access key you set earlier.

build.gradle


apply plugin: 'maven'

build.gradle


uploadArchives {
    repositories {
        mavenDeployer {
            configuration = configurations.deployerJars
            repository(url: goalistRepoUrl) {
              authentication(userName: awsAccessKeyId, password: awsSecretAccessKey)
            }
        }
    }
}

We will use org.springframework.build: aws-maven to access S3, so add the setting to dependencies. This time I will use the latest version 5.0.0.RELEASE.

http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.springframework.build%22%20AND%20a%3A%22aws-maven%22

build.gradle


configurations {
    deployerJars
}

dependencies {
    deployerJars 'org.springframework.build:aws-maven:5.0.0.RELEASE'//add to
}

This completes the setting. The final result is as follows.

build.gradle


apply plugin: 'java'
apply plugin: 'maven'

group = "jp.co.goalist"
version = "1.0.0" 

repositories {
    jcenter()
}

configurations {
    deployerJars
}

uploadArchives {
    repositories {
        mavenDeployer {
            configuration = configurations.deployerJars
            repository(url: goalistRepoUrl) {
              authentication(userName: awsAccessKeyId, password: awsSecretAccessKey)
            }
        }
    }
}

dependencies {
    deployerJars 'org.springframework.build:aws-maven:5.0.0.RELEASE'
}

settings.gradle


rootProject.name = 'library'

Run the uploadArchives task

Execute the following command, and if BUILD SUCCESSFULL is displayed, it is successful.

gradle uploadArchives

When I check S3

I think the library has been uploaded under s3: //repository.hoge/maven/.

Download from Maven repository

Create a Gradle project that depends on the library you uploaded this time.

mkdir gradle-upload-s3repo-child
cd gradle-upload-s3repo-child/
gradle init --type=java-library

Modify build.gradle to set the S3 repository reference and dependency settings.

build.gradle


apply plugin: 'java'

repositories {
    jcenter()
    maven {
        url goalistRepoUrl
        credentials(AwsCredentials) {
            accessKey awsAccessKeyId
            secretKey awsSecretAccessKey
        }
    }
}

dependencies {
    compile 'jp.co.goalist:library:1.0.0'
}

Execute the following command, and if you can confirm that it is BUILD SUCCESSFUL, you are done.

gradle build

at the end

In actual operation, I think that it is possible to operate it as if it is possible to upload only from the build server so that the developer can only refer to it. Let's reuse the module.

Recommended Posts

Dependency management in Gradle using Maven repository on Amazon S3
Build Maven repository on AWS S3 service
Build a Maven repository on AWS S3
Dependency management in layered architecture
Popular trends in gradle and maven
Browse local Maven artifacts in Gradle
Creating a project (and GitHub repository) using Java and Gradle in IntelliJ IDEA
Generate DB documentation using SchemaSpy in Gradle