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, use the following procedure.
To fetch dependencies from CodeArtifact in a Gradle build
If you haven't, create and store a CodeArtifact auth token in an environment variable by following the procedure in Pass an auth token using an environment variable.
-
Add a
mavensection to therepositoriessection in the projectbuild.gradlefile.maven { url 'https://my_domain-111122223333.d.codeartifact.region.amazonaws.com/maven/my_repo/' credentials { username "aws" password System.env.CODEARTIFACT_AUTH_TOKEN } }The
urlin the preceding example is your CodeArtifact repository's endpoint. Gradle uses the endpoint to connect to your repository. In the sample,my_domainis the name of your domain,111122223333is the ID of the owner of the domain, andmy_repois the name of your repository. You can retrieve a repository's endpoint by using theget-repository-endpointAWS CLI command.For example, with a repository named
my_repoinside a domain namedmy_domain, the command is as follows:aws codeartifact get-repository-endpoint --domainmy_domain--domain-owner111122223333--repositorymy_repo--format mavenThe
get-repository-endpointcommand will return the repository endpoint:url 'https://my_domain-111122223333.d.codeartifact.region.amazonaws.com/maven/my_repo/'The
credentialsobject in the preceding example includes the CodeArtifact auth token you created in Step 1 that Gradle uses to authenticate to CodeArtifact.Note
To use a dualstack endpoint, use the
codeartifact.endpoint.region.on.aws (Optional) - To use the CodeArtifact repository as the only source for your project dependencies, remove any other sections in
repositoriesfrombuild.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
dependenciessection 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
If you haven't, create and store a CodeArtifact auth token in an environment variable by following the procedure in Pass an auth token using an environment variable.
Add a
pluginManagementblock to yoursettings.gradlefile. ThepluginManagementblock must appear before any other statements insettings.gradle, see the following snippet:pluginManagement { repositories { maven { name 'my_repo' url 'https://my_domain-111122223333.d.codeartifact.region.amazonaws.com/maven/my_repo/' 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.region.amazonaws.com/maven/my_repo/' credentials { username "aws" password System.env.CODEARTIFACT_AUTH_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 --domainmy_domain--domain-owner111122223333--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. To authenticate with CodeArtifact, you must provide Gradle with a CodeArtifact authorization token. There are three methods to provide an auth token.
Method 1: Storing the auth token in
gradle.properties. Use this method if you are able to overwrite or add to the contents of thegradle.propertiesfile.Method 2: Storing the auth token in a separate file. Use this method if you do not want to modify your
gradle.propertiesfile.Method 3: Generating a new auth token for each run by running
awsas an inline script inbuild.gradle. Use this method if you want the Gradle script to fetch a new token on each run. The token won't be stored on the file system.