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
-domain-owner-id
.d.codeartifact.us-west-2
.amazonaws.com/maven/my-repo
/' credentials { username "aws" password System.env.CODEARTIFACT_AUTH_TOKEN } }
For example, with a repository named my-repo
, the URL should be the
following:
url 'https://
my-domain
-domain-owner-id
.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 PortalpluginManagement
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
-domain-owner-id
.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
-domain-owner-id
.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-ownerdomain-owner-id
--repositorymy-repo
--formatmaven
\ --namespacecom.company.framework
--packagemy-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.
The example shows the gradle.properties
file located in GRADLE_USER_HOME
.
-
Update your
build.gradle
file with the following snippet:repositories { maven { url 'https://
my-domain
-domain-owner-id
.d.codeartifact.region
.amazonaws.com/maven/my-repo
/' credentials { username "aws" password "$codeartifactToken" } } } -
Fetch a CodeArtifact auth token:
export CODEARTIFACT_AUTH_TOKEN=`aws codeartifact get-authorization-token --domain
my-domain
--domain-ownerdomain-owner-id
--query authorizationToken --output text --profileprofile-name
` -
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.
-
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
-domain-owner-id
.d.codeartifact.region
.amazonaws.com/maven/my-repo
/' credentials { username "aws" password props.getProperty("codeartifactToken") } } } -
Fetch a CodeArtifact auth token:
export CODEARTIFACT_AUTH_TOKEN=`aws codeartifact get-authorization-token --domain
my-domain
--domain-ownerdomain-owner-id
--query authorizationToken --output text --profileprofile-name
` -
Write the auth token into the file that was specified in your
build.gradle
file:echo "codeartifactToken=$CODEARTIFACT_AUTH_TOKEN" >
file