Gitlab CI : use AWS CodeArtifact with Maven

Gitlab CI : use AWS CodeArtifact with Maven


5 min read

If you are looking to store your Artifcats online in a secure way, take a try with Amazon CodeArtifact. This can be an alternative to JFrog Artifactory.

In this article, we describe the way to push a maven artifact from the gitlab CI to AWS CodeArtifact.

First of all, you need to create a repository on AWS CodeArtifact. You can try the "Get started" button on the aws cloud console.


In the repository creation, you have to enter the name of your repository and choose your domain. After that, you are redirected to your repository and can find all the connection instructions.


In the connection instructions, select mvn and copy the CodeArtifact authorization token line. image.png

The main objective of this article is to push an artifact during the maven deployment process. In our case, we use Gitlab for CI/CD and we can configure the deployment in the gitlab-ci.yaml described bellow.

In the step of the gitlab-ci.yaml, we use a docker image, Maven with AWS and ECS CLI tools

As the token of aws codeartifact change every 3hours, we create a "before_script" to get back the token and export it for the settings.xml file that will be described in a second step. For this export, paste the line that was copied in the connection instructions of AWS.

After that, we use the command "mvn deploy" in the script section to deploy the artifact on AWS.

  MAVEN_CLI_OPTS_DEV: "-s .m2/settings.xml --batch-mode"
  DOMAIN_OWNER: xxxxxx
  APP_DOMAIN: myappdomain
  APP_PACKAGE: myapppackage

    name: softinstigate/maven-aws
  stage: deploymavenaws
    - export CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token --domain $APP_DOMAIN --domain-owner $DOMAIN_OWNER --query authorizationToken --output text)
    - mvn $MAVEN_CLI_OPTS_DEV deploy

As you can see in the gitlab-ci.yaml, the mvn deploy command had a parameter with a settings file. If you dont already have this settings.xml in your project, you can create it. To fill this file with the correct content, you should go back to your AWS CodeArtifact connections instructions. This time, you have to copy the step 2 and 3.


Here is what your file should look like

<settings xsi:schemaLocation=""
    xmlns="" xmlns:xsi="">

A good practise would be to save your credentials informations as variables, for example in a secure vault to avoid having this value hardcoded in your project.

Additionally, for Maven, you need to add the following lines at the end of your pom.xml file. The fields and are the same values that you copy in the settings.xml file. The name represent the repository name.



With this method, you should have a new artifact on AWS CodeArtifact every time you launch your gitlab CI/CD !

On gitlab, you should see the sucessfull pipeline


And finally, you should find your artifact on AWS, including the .war and the .pom file. image.png

Tips : if you launch your CI/CD with the same version in your pom.xml, there will be a 409 conflict issue on aws because you artifact already exist. You can delete artifact with the same id (for example on a dev CI/CD branch) by adding the following lines in your before_script :

- apt-get install -y libxml2 libxml2-utils
- RELEASE_VERSION=$(xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' pom.xml)
- aws codeartifact delete-package-versions  --domain $APP_DOMAIN --repository $APP_REPO --format maven --package $APP_PACKAGE --namespace $APP_NS --versions $RELEASE_VERSION || true

You can find the files in our public repository on Github

Feel free to comment this article if you have questions.