[DevOps] Publish Java package to AWS CodeArtifact

AWS CodeArtifact is one of the AWS devops feature for package management. Even compare with NexusOSS, it cannot upload package manually, but it still worth to use if using AWS as DevOps Platform.In this demo, it will show how to config Java project (Maven / Gradle) to publish to CodeArtifact.

  1. Create AWS CodeArtifact Domain.
    In AWS CodeArtifact, select Domains > Create Domain. Input domain name and click button Create Domain.
  2. Create Repository.
    In AWS CodeArtifact, select Repository > Create Repository, input Repository name and click Next.
  3. Select Repository’s domain.
    Select AWS account to This AWS account and select related Domain, then click Next.
  4. Confirm repository settings and create.
    After review settings, then click Create Repository.
  5. Setup Maven repository settings for local computer
    Create file setting.xml in %USER%\.m2\

    <settings>
      <servers>
        <server>
          <id>CodeArtifact</id>
          <username>aws</username>
          <password>${env.CODEARTIFACT_AUTH_TOKEN}</password>
        </server>
        </servers>
    <mirrors>
      <mirror>
        <id>CodeArtifact</id>
        <name>CodeArtifact</name>
        <url>https://{{domain-name}}-{{domain-id}}.d.codeartifact.ap-southeast-1.amazonaws.com/maven/{{repo-name}}/</url>
        <mirrorOf>*</mirrorOf>
      </mirror>
    </mirrors>
    </settings>
  6. Generate AWS authentication token and store in variable.
    In command prompt / bash, execute command and store variable in CODEARTIFACT_AUTH_TOKEN.

    :: For Windows:
    FOR /F "tokens=*" %a in ('aws codeartifact get-authorization-token --domain {{domain-name}} --domain-owner {{domain-id}} --query authorizationToken --output text') do SET CODEARTIFACT_AUTH_TOKEN=%a
    
    :: For Linux: 
    export CODEARTIFACT_AUTH_TOKEN=`aws codeartifact get-authorization-token --domain {{domain-name}} --domain-owner {{domain-id}} --query authorizationToken --output text`
  7. Update project settings.
    Edit pom.xml (for Maven project) / build.gradle (for Gradle project).
    pom.xml

    <project>
    ...
      <groupId>com.test</groupId>
      <artifactId>demo</artifactId>
      <version>1.0.1</version>
      <packaging>jar</packaging>
      <name>demo</name>
    ...
      <distributionManagement>
      	<repository>
            <id>d-sg-2--d-sg-ca-repo</id>
            <name>aws-codeArtifact</name>
            <url>https://{{domain-name}}-{{domain-id}}.d.codeartifact.ap-southeast-1.amazonaws.com/maven/{{repo-name}}/</url>
      	</repository>
      </distributionManagement>
    ...
    </project>

    build.gradle

    ...
    plugins {
        id 'maven-publish'
    }
    ...
    ext {
        // Package metadata.
        packageGroup = 'com.test'
        artifact='demo'
        packageName = 'demo'
        packageVersion = '1.0.1'
    }
    ...
    repositories {
        //mavenCentral()
        //mavenLocal()
        maven {
          url 'https://{{domain-name}}-{{domain-id}}.d.codeartifact.ap-southeast-1.amazonaws.com/maven/{{repo-name}}/'
          credentials {
              username "aws"
              password System.env.CODEARTIFACT_AUTH_TOKEN
          }
        }
    }
    ...
    publishing {
        publications {
            mavenJava(MavenPublication) {
            	groupId="${packageGroup}"
                artifactId="${packageName}"
                version="${packageVersion}"
                from components.java
            }
        }
        repositories {
          // AWS CodeArtifact
          maven {
              url 'https://{{domain-name}}-{{domain-id}}.d.codeartifact.ap-southeast-1.amazonaws.com/maven/{{repo-name}}/'
              credentials {
                  username "aws"
                  password System.env.CODEARTIFACT_AUTH_TOKEN
              }
          }
        }
    }
    ...
  8. Publish to AWS CodeArtifact.
    In command prompt / bash, execute command.

    ::Maven project.
    mvn deploy
    
    ::Gradle project.
    gradle publish

  9. Verify delivery.
    In CodeArtifact, click Repositories > select repository, see published package found or not.
About C.H. Ling 262 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.