Home Navigation

Friday 30 April 2021

Publish maven project to google cloud (GCP) Artifact Registry

  • Enable the Artifact Registry API.
  • Install and initialize the Cloud SDK.
  • Create the repository, you might not see the option maven. It is disabled as it is still in alpha version. To enable the option fill the form. it will take some time to approve. Lets assume your repository name is "quickstart-maven-repo" and location you selected is "us-central1"

  • Now go to your command prompt and login to gcloud console
$gcloud auth login
$gcloud config set project <myProject>
  • Set the repository, run the command:
$gcloud config set artifacts/repository quickstart-maven-repo
  • set the location
$gcloud config set artifacts/location us-central1

  • Create a service account from google cloud console or run the below command

$gcloud artifacts repositories add-iam-policy-binding quickstart-maven-repo --location=us-central1 --member='serviceAccount:ACCOUNT' --role='roles/artifactregistry.writer'

Where ACCOUNT is the ID of your service account in the format USERNAME@PROJECT-ID.iam.gserviceaccount.com

  • Download the service account key

 $gcloud iam service-accounts keys create mykey.json --iam-account=USERNAME@PROJECT-ID.iam.gserviceaccount.com

$export GOOGLE_APPLICATION_CREDENTIALS=mykey.json 
Where mykey.json is gnereated in previous step.

  • Now its time to configure the maven project
  • Choose a Maven project that you want to use. and go to the root directory of the project.
  • Run the following command to print the settings for the default quickstart-maven-repo repository.
$gcloud artifacts print-settings mvn

  • The output should look like below
       <distributionManagement>
  <snapshotRepository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/PROJECT/quickstart-maven-repo</url>
  </snapshotRepository>
  <repository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/PROJECT/quickstart-maven-repo</url>
  </repository>
</distributionManagement>

<repositories>
  <repository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/PROJECT/quickstart-maven-repo</url>
<releases>
  <enabled>true</enabled>
</releases>
<snapshots>
  <enabled>true</enabled>
</snapshots>
  </repository>
</repositories>

<build>
  <extensions>
<extension>
  <groupId>com.google.cloud.artifactregistry</groupId>
  <artifactId>artifactregistry-maven-wagon</artifactId>
  <version>2.1.1</version>
</extension>
  </extensions>
</build>

Add the output to the pom.xml
  • Run the below command to publish to the repo
$mvn clean deploy
and see the magic. go to google cloud console and then artifact-registry , you should see your published jar
  • For dependent project to access the published jar add the below settings to pom.xml
        <repositories>
<repository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/PROJECT/quickstart-maven-repo</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
  <extensions>
<extension>
  <groupId>com.google.cloud.artifactregistry</groupId>
  <artifactId>artifactregistry-maven-wagon</artifactId>
  <version>2.1.1</version>
</extension>
  </extensions>
</build>

 and then run the command 
$mvn clean compile

Project should compile and download all the dependency.