Use CodeArtifact with mvn
You use the mvn
command to execute Maven builds. This section shows how to
configure mvn
to use a CodeArtifact repository.
After you have the CodeArtifact auth token in an environment variable as described in Passing 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 configure mvn
to fetch dependencies from a CodeArtifact repository, you must
edit the Maven configuration file, settings.xml
, and optionally,
your project's POM.
-
In
settings.xml
(typically found at~/.m2/settings.xml
), add a<servers>
section with a reference to theCODEARTIFACT_AUTH_TOKEN
environment variable so that Maven passes the token in HTTP requests.<settings> ... <servers> <server> <id>codeartifact</id> <username>aws</username> <password>${env.CODEARTIFACT_AUTH_TOKEN}</password> </server> </servers> ... </settings>
-
Add the URL endpoint for your CodeArtifact repository in a
<repository>
element. You can do this insettings.xml
or your project's POM file.Add the repository endpoint to
settings.xml
as follows.<settings> ... <profiles> <profile> <id>default</id> <repositories> <repository> <id>codeartifact</id> <url>https://
my-domain
-domain-owner-id
.d.codeartifact.us-west-2
.amazonaws.com/maven/my-repo
/</url> </repository> </repositories> </profile> </profiles> <activeProfiles> <activeProfile>default</activeProfile> </activeProfiles> ... </settings>Or, you can add the
<repositories>
section to a project POM file to use CodeArtifact for that project only.<project> ... <repositories> <repository> <id>codeartifact</id> <name>codeartifact</name> <url>https://
my-domain
-domain-owner-id
.d.codeartifact.us-west-2
.amazonaws.com/maven/my-repo
/</url> </repository> </repositories> ... </project>
You can use any value in the <id>
element, but it must be the
same in both the <server>
and <repository>
elements. This enables the specified credentials to be included in requests to
CodeArtifact.
After you make these configuration changes, you can build the project.
mvn compile
Maven logs the full URL of all the dependencies it downloads to the console.
[INFO] ------------------< com.example.example:myapp >------------------- [INFO] Building myapp 1.0 [INFO] --------------------------------[ jar ]--------------------------------- Downloading from codeartifact: https://<domain>.d.codeartifact.us-west-2.amazonaws.com/maven/myrepo/commons-cli/commons-cli/1.4/commons-cli-1.4.pom Downloaded from codeartifact: https://<domain>.d.codeartifact.us-west-2.amazonaws.com/maven/myrepo/commons-cli/commons-cli/1.4/commons-cli-1.4.pom (11 kB at 3.9 kB/s) Downloading from codeartifact: https://<domain>.d.codeartifact.us-west-2.amazonaws.com/maven/myrepo/org/apache/commons/commons-parent/42/commons-parent-42.pom Downloading from codeartifact: https://<domain>.d.codeartifact.us-west-2.amazonaws.com/maven/myrepo/org/apache/commons/commons-parent/42/commons-parent-42.pom Downloaded from codeartifact: https://<domain>.d.codeartifact.us-west-2.amazonaws.com/maven/myrepo/org/apache/commons/commons-parent/42/commons-parent-42.pom (68 kB at 123 kB/s) Downloading from codeartifact: https://<domain>.d.codeartifact.us-west-2.amazonaws.com/maven/myrepo/commons-cli/commons-cli/1.4/commons-cli-1.4.jar Downloaded from codeartifact: https://<domain>.d.codeartifact.us-west-2.amazonaws.com/maven/myrepo/commons-cli/commons-cli/1.4/commons-cli-1.4.jar (54 kB at 134 kB/s)
Publish artifacts
To publish a Maven artifact with mvn
to a CodeArtifact repository, you must
also edit ~/.m2/settings.xml
and the project POM.
-
Add a
<servers>
section tosettings.xml
with a reference to theCODEARTIFACT_TOKEN
environment variable so that Maven passes the token in HTTP requests.<settings> ... <servers> <server> <id>codeartifact</id> <username>aws</username> <password>${env.CODEARTIFACT_TOKEN}</password> </server> </servers> ... </settings>
-
Add a
<distributionManagement>
section to your project'spom.xml
.<project> ... <distributionManagement> <repository> <id>codeartifact</id> <name>codeartifact</name> <url>https://
my-domain
-domain-owner-id
.d.codeartifact.us-west-2.amazonaws.com/maven/my-repo
/</url> </repository> </distributionManagement> ... </project>
After you make these configuration changes, you can build the project and publish it to the specified repository.
mvn deploy
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:
{ "defaultDisplayVersion": null, "format": "
maven
", "namespace": "com.company.framework
", "package": "my-package-name
", "versions": [ { "version": "1.0", "revision": "REVISION-SAMPLE-1-C7F4S5E9B772FC", "status": "Published" } ] }
Publish third-party artifacts
You can publish third-party Maven artifacts to a CodeArtifact repository with mvn deploy:deploy-file
. This can be
helpful to users that want to publish artifacts and only have JAR files and don't
have access to package source code
or POM files.
The mvn deploy:deploy-file
command will generate a POM file based on the information
passed in the command line.
Publish third-party Maven artifacts
-
Create a
~/.m2/settings.xml
file with the following contents:<settings> <servers> <server> <id>codeartifact</id> <username>aws</username> <password>${env.CODEARTIFACT_AUTH_TOKEN}</password> </server> </servers> </settings>
-
Fetch a CodeArtifact authorization token:
export CODEARTIFACT_AUTH_TOKEN=`aws codeartifact get-authorization-token --domain
my-domain
--domain-ownerdomain-owner-id
--query authorizationToken --output text --profileprofile-name
-
Run the
mvn deploy:deploy-file
command:mvn deploy:deploy-file -DgroupId=commons-cli \ -DartifactId=commons-cli \ -Dversion=1.4 \ -Dfile=./commons-cli-1.4.jar \ -Dpackaging=jar \ -DrepositoryId=codeartifact \ -Durl=https://
my-domain
-domain-owner-id
.d.codeartifact.region
.amazonaws.com/maven/repo-name
/Note The example above publishes
commons-cli 1.4
. Modify the groupId, artifactID, version, and file arguments to publish a different JAR.
These instructions are based on examples in the Guide to deploying 3rd party JARs to remote repository
For more information, see these topics on the Apache Maven Project website: