Building and Installing Maven Packages with AWS CodeArtifact

By Siva Dondapati
10 mins read

Maven is a popular build automation tool used by developers to manage dependencies and build Java-based projects. AWS CodeArtifact is a fully managed artifact repository service that makes it easy to store, manage, and share software packages.


In this blog post, we will explore how to upload Maven packages to AWS CodeArtifact and then download and install them from CodeArtifact.

Pre-requisites

Before we get started, you'll need the following:
  • AWS Account: You should have an AWS account to use AWS CodeArtifact.
  • Maven Installed: Make sure you have Maven installed on your local machine.
  • AWS CLI: Install and configure the AWS Command Line Interface (AWS CLI) with access to your AWS account.
Creating AWS CodeArtifact Repository
The first step is to create a CodeArtifact repository to store your Maven packages. Follow these steps:
Step 1: Sign in to the AWS Management Console.
Step 2: Open the CodeArtifact Console.
Step 3: Create a Repository: Click on "Create repository."
Step 4: Choose a name for your repository. Select "Maven" as the package format.
Step 5: Optionally, you can configure permissions and add tags.
Step 6: Click "Create repository."
Now you have a repository in CodeArtifact to store your Maven packages.
Configure Maven for CodeArtifact
To use CodeArtifact as a Maven repository, you need to configure Maven settings. Here's how to do it:

Get Repository Endpoint URL:
In the CodeArtifact Console, go to your repository. On the "Repository details" page, copy the endpoint URL.

Configure Maven:
Step 1: Open your ~/.m2/settings.xml file (create one if it doesn't exist).
Step 2: Add a <servers> section if it's not already there as shown below.

<servers>
	<server>
                 <id>aws-maven</id>
                 <username>aws</username>
                 <password>{Your CodeArtifact Authorization Token}</password>
	</server>
</servers>

Step 3: Add a <repositories> section to your settings.xml file as shown below.


<repositories>
	<repository>
                <id>codeartifact</id>
                <url>https://{your-repo-name}.{region}.d.codeartifact.{region}.amazonaws.com/maven/{your-repo-name}/</url>
        </repository>
</repositories>

Now Maven is configured to use CodeArtifact as a repository.

Step 4: Update pom.xml File


We need to update our pom.xml file to include the AWS CodeArtifact repository as a distribution management repository. Here’s how you can do it:


<distributionManagement>
     <repository>
           <id>aws-codeartifact</id>
           <url>https://my-domain-123456789012.d.codeartifact.region.amazonaws.com/maven/my-repo/</url>
     </repository>
</distributionManagement>

Build and Upload the Maven Package to AWS CodeArtifact

Finally, we can build our Maven project and upload the package to AWS CodeArtifact. Here’s how to do it:


# Build the project
mvn clean install
 
# Get an authorization token for your repository
export CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token \ 
--domain "my-domain" --query authorizationToken --output text)
 
# Deploy the package
mvn deploy

After the deployment is successful, go to your CodeArtifact repository in the AWS Console.

You should see your uploaded packages under the "Versions" tab.

With these steps, you have successfully uploaded Maven packages to AWS CodeArtifact.

Download the Maven package from AWS CodeArtifact

To do so, let us create a file named "config.properties" to store the variables that contains CodeArtifact repo details as shown below.


aws_account=${aws_account}
aws_region=${aws_region}
codeartifact_domain=${codeartifact_domain}
codeartifact_repo=${codeartifact_repo}
codeartifact_package_format='maven'
codeartifact_namespace='com.cpt'
package_name=${package_name}
package_version=${package_version}
package_rpm=${package_rpm}
Finally Download package from CodeArtifact and install it on your server.

# Export authorization token for your repository
export CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token \ 
--domain "my-domain" --query authorizationToken --output text)
 
# Download the package
aws codeartifact get-package-version-asset --domain ${codeartifact_domain} \
--domain-owner ${aws_account} \
--repository ${codeartifact_repo} \
--format ${codeartifact_package_format} \
--region ${aws_region } \
--namespace ${codeartifact_namespace} \
--package ${package_name} \
--package-version ${version} \
--asset ${package_rpm}  /tmp/${package_rpm}
With the above command we will get RPM of that package. We can install this RPM using Yum.

Install the Maven package using Yum repo

Step 1: Create a yum repo file "development.repo" (with the below mentioned content) in "/etc/yum.repos.d" folder of your target machine where you want to install this package:

[development]
name=Development
baseurl=file:///opt/yum_repo
enabled=1
gpgcheck=0
metadata_expire=30s
autorefresh=1
protect=0
type=rpm-md

Step 2: Check if the package you are trying to install already exists, if so uninstall it as follows:


echo -e "\nREMOVING OLD PACKAGE..."
sudo yum remove $package_name -y
sudo rm -rf /opt/yum_repo/*

Step 3: Install the new RPM package. For that we first need to create a yum repo with "/opt/yum_repo" as base url (this is the path where our new downloaded rpm is moved to)


if [ -f /tmp/${package_rpm} ]
then
    mv /tmp/${package_rpm} /opt/yum_repo
    sudo createrepo /opt/yum_repo
 
    echo -e "\nINSTALLING PACKAGE..."
    sudo yum install $package_name -y
fi

With this your pacake will be installed on your target machine from AWS Code Artifact.

Conclusion


AWS CodeArtifact provides a secure, scalable, and cost-effective solution for storing and sharing packages.

By integrating it with Maven, you can easily manage your Java dependencies and ensure that your applications always have access to the packages they need.