Building and Installing Maven Packages with AWS CodeArtifact
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.
<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>
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.
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}
# 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}
[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.