Use CodeArtifact with Gradle - CodeArtifact

Use CodeArtifact with Gradle

After you have the CodeArtifact auth token in an environment variable as described in Pass an auth token using an environment variable, follow these instructions to consume Maven packages from, and publish new packages to, a CodeArtifact repository.

Fetch dependencies

To fetch dependencies from CodeArtifact in a Gradle build, add a maven section to the repositories section in the project build.gradle file.

maven { url 'https://my_domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/maven/my_repo/' credentials { username "aws" password System.env.CODEARTIFACT_AUTH_TOKEN } }

The url in the sample above is your CodeArtifact repository's endpoint. Gradle uses the endpoint to connect to your repository. In the sample, my_domain is the name of your domain, 111122223333 is the ID of the owner of the domain, and my_repo is the name of your repository. You can retrieve a repository's endpoint by using the get-repository-endpoint AWS CLI command.

For example, with a repository named my_repo inside a domain named my_domain, the command is as follows:

aws codeartifact get-repository-endpoint --domain my_domain --domain-owner 111122223333 --repository my_repo --format maven

The get-repository-endpoint command will return the repository endpoint:

url 'https://my_domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/maven/my_repo/'

To use the CodeArtifact repository as the only source for your project dependencies, remove any other sections in repositories from build.gradle. If you have more than one repository, Gradle searches each repository for dependencies in the order they are listed.

After you configure the repository, you can add project dependencies to the dependencies section with standard Gradle syntax.

dependencies { implementation 'com.google.guava:guava:27.1-jre' implementation 'commons-cli:commons-cli:1.4' testImplementation 'org.testng:testng:6.14.3' }

Fetch plugins

By default Gradle will resolve plugins from the public Gradle Plugin Portal. To pull plugins from a CodeArtifact repository, add a pluginManagement block to your settings.gradle file. The The pluginManagement block must appear before any other statements in settings.gradle:

pluginManagement { repositories { maven { name 'repository-name' url 'https://domain-name-111122223333.codeartifact.us-west-2.amazonaws.com/maven/repository-name/' credentials { username 'aws' password System.env.CODEARTIFACT_AUTH_TOKEN } } } }

This will ensure that Gradle resolves plugins from the specified repository. The repository must have an upstream repository with an external connection to the Gradle Plugin Portal (e.g. gradle-plugins-store) so that commonly-required Gradle plugins are available to the build. For more information, see the Gradle documentation.

Publish artifacts

This section describes how to publish a Java library built with Gradle to a CodeArtifact repository.

First, add the maven-publish plugin to the plugins section of the project's build.gradle file.

plugins { id 'java-library' id 'maven-publish' }

Next, add a publishing section to the project build.gradle file.

publishing { publications { mavenJava(MavenPublication) { groupId = 'group-id' artifactId = 'artifact-id' version = 'version' from components.java } } repositories { maven { url 'https://my_domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/maven/my_repo/' credentials { username "aws" password System.env.CODEARTIFACT_TOKEN } } } }

The maven-publish plugin generates a POM file based on the groupId, artifactId, and version specified in the publishing section.

After these changes to build.gradle are complete, run the following command to build the project and upload it to the repository.

./gradlew publish

Use list-package-versions to check that the package was successfully published.

aws codeartifact list-package-versions --domain my_domain --domain-owner 111122223333 --repository my_repo --format maven\ --namespace com.company.framework --package my-package-name

Sample output:

{ "format": "maven", "namespace": "com.company.framework", "package": "example", "versions": [ { "version": "1.0", "revision": "REVISION-SAMPLE-1-C7F4S5E9B772FC", "status": "Published" } ] }

For more information, see these topics on the Gradle website:

Run a Gradle build in IntelliJ IDEA

You can run a Gradle build in IntelliJ IDEA that pulls dependencies from CodeArtifact. You can store and use a CodeArtifact auth token in gradle.properties or a separate file of your choice.

Method 1: Putting the auth token in gradle.properties

Use this method if you are not using the gradle.properties file and can overwrite its contents with your auth token. If you are using gradle.properties, you can modify this method to add the token instead of overwriting the file's contents.

Note

The example shows the gradle.properties file located in GRADLE_USER_HOME.

  1. Update your build.gradle file with the following snippet:

    repositories { maven { url 'https://my_domain-111122223333.d.codeartifact.region.amazonaws.com/maven/my_repo/' credentials { username "aws" password "$codeartifactToken" } } }
  2. Fetch a CodeArtifact auth token:

    export CODEARTIFACT_AUTH_TOKEN=`aws codeartifact get-authorization-token --domain my_domain --domain-owner 111122223333 --query authorizationToken --output text --profile profile-name`
  3. Write the auth token into the gradle.properties file:

    echo "codeartifactToken=$CODEARTIFACT_AUTH_TOKEN" > ~/.gradle/gradle.properties

Method 2: Putting the auth token in a separate file

Use this method if you do not want to modify your gradle.properties file.

  1. Update your build.gradle file with the following snippet:

    def props = new Properties() file("file").withInputStream { props.load(it) } repositories { maven { url 'https://my_domain-111122223333.d.codeartifact.region.amazonaws.com/maven/my_repo/' credentials { username "aws" password props.getProperty("codeartifactToken") } } }
  2. Fetch a CodeArtifact auth token:

    export CODEARTIFACT_AUTH_TOKEN=`aws codeartifact get-authorization-token --domain my_domain --domain-owner 111122223333 --query authorizationToken --output text --profile profile-name`
  3. Write the auth token into the file that was specified in your build.gradle file:

    echo "codeartifactToken=$CODEARTIFACT_AUTH_TOKEN" > file