Building Reusable Libraries with Google Cloud Artifact Registry

When you’re building multiple independently deployable microservices, you may encounter many common functional or non-functional use cases. In such cases, it makes sense to build a reusable library, package it as an artifact, and import it into all your microservice projects. Some common examples include customizing logging and tracing functionality or integrating with a third-party authorization provider.

One of the most important tasks when building a reusable library is packaging and deploying it to an artifact server. Google Cloud Platform offers a solution for this by providing a dedicated service called Artifact Registry, where you can deploy these artifacts. Artifact Registry is a fully managed service that allows you to securely store and manage artifacts such as JAR files and Docker images. Once deployed, these artifacts can be easily imported and used through build automation tools like Maven or Gradle.

This guide provides step-by-step instructions to deploy any Gradle-based Java project to Google Cloud Artifact Registry.

Prerequisites

Before you begin, make sure you have the following:

  • A Google Cloud account with an active project.
  • Google Cloud SDK installed on your machine.
  • An Artifact Registry repository created in your GCP project.
  • A Service Account Key in JSON format with permission to push to Artifact Registry.

Step 1: Authenticate with Google Cloud

1. Authenticate using your service account key:
				
					gcloud auth activate-service-account --key-file /path/to/your/service-account-key.json
 	
If you don’t already have a service account key file, follow this guide to create one. – 

https://cloud.google.com/iam/docs/keys-create-delete
				
			
2. Set the active project:
				
					gcloud config set project YOUR_PROJECT_ID

				
			
3.Enable Artifact Registry API:
				
					gcloud services enable artifactregistry.googleapis.com

				
			

Step 2: Update build.gradle for Publishing

 Add Required Plugins
				
					plugins {
    id 'java'
    id 'maven-publish'
    id 'com.google.cloud.artifactregistry.gradle-plugin' version '2.2.1'
}

				
			

Configure Publishing Block

Configure Artifact Publishing to Artifact Registry Add the publishing block to configure Artifact Registry as your destination for the published artifact:

				
					publishing {
 publications {
     mavenJava(MavenPublication) {
         groupId = group  // Your group ID
         artifactId = 'my-artifact'  // Your desired artifact name
         version = buildTimeStamp  // Use the dynamically generated version

         from components.java  // Publish the Java component (e.g., JAR file)
     }
 }

 repositories {
     maven {
         url = uri("https://${YOUR_PROJECT_ID}-maven.pkg.dev/${YOUR_LOCATION}/${YOUR_REPO_NAME}")
         credentials {
             username = "_json_key"
             password = file("/path/to/your/service-account-key.json").text  // Path to your service account key file
         }
         authentication {
             basic(BasicAuthentication)
         }
     }
 }
}

				
			

Key Points:

  • Replace YOUR_PROJECT_ID, YOUR_LOCATION, and YOUR_REPO_NAME with your actual Google Cloud project, location, and Artifact Registry repository.
  • service-account-key.json should be the path to your service account JSON key file, which will authenticate your request to publish the artifact.

Step 3: Create an Artifact Registry Repository

To create a repository from Google Cloud console:

https://console.cloud.google.com/artifacts.

You can also achieve this by using the gcloud CLI.

YOUR_LOCATION can be one of the available regions served by Google Cloud Platform. This should ideally be the region physically closest to your build or publishing server. Here, we select us-central-1.

With this configuration, the repository path to be configured in build.gradle would be :

https://us-central1-maven.pkg.dev/apps-project/logging-library

Step 4: Build and Publish the Artifact 

Once you’ve configured your build.gradle file, follow these steps to build and publish your artifact to Google Cloud Artifact Registry:

Build the project: gradle build This will compile your project and create a .jar (or other appropriate artifact).

Publish the artifact to Artifact Registry: gradle publish

This will:

  • Create the artifact (e.g., JAR file).
  • Upload the artifact to your Artifact Registry repository in Google Cloud.

When you publish your artifact, you may run into 403 permissions error. Please make sure your service account has permissions to read and write to your artifact repository.

Step 5: Verify the Artifact

After publishing your artifact, verify that it has been successfully uploaded:

  • Open the Google Cloud Console.
  • Navigate to Artifact Registry under the Storage section.
  • Select your repository.
  • You should see the uploaded artifact listed with the version you specified (or timestamp if configured that way). You can now use this artifact in other projects or CI/CD pipelines.

Step 6: Use the Library in Another Gradle Project

In your gradle-based Java project, configure the artifact repository in the list of repositories in build.gradle:

				
					repositories {
    mavenCentral()
    maven {
        url "artifactregistry://us-central1-maven.pkg.dev/apps-project/logging-library"
        credentials {
            username = "_json_key_base64"
            password = "${System.getenv("ARTIFACT_REGISTRY_SECRET")}"
        }
        authentication {
            basic(BasicAuthentication)
        }
    }
}

				
			
Fetch ARTIFACT_REGISTRY_SECRET, run the following command:
				
					gcloud artifacts print-settings gradle \
    --project=apps-project \
    --repository=logging-library \
    --location=us-central1

				
			

In this article, we saw how to publish and reuse libraries via Google Cloud Artifact Registry. First, how to enable the Artifact Registry API and create a repository. Develop the library, define dependencies, and package it. Authenticate with a service account, then deploy using gradle publish. Finally, how to import the library in other projects by configuring the registry URL in build files.

Scroll to Top