Quickstart for Maven and Gradle

This quickstart shows you how to set up an Artifact Registry Maven repository and upload a package to it.

Package management is in alpha. It is only available to alpha users, and might not include all features available for container management. To apply for the alpha, complete the sign up form.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to the project selector page

  3. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  4. Enable the Artifact Registry API.

    Enable the API

  5. Install and initialize the Cloud SDK.

Create a Maven repository

Create the repository for your Java artifacts.

  1. Create the repository:

    Console

    1. Open the Repositories page in the Cloud Console.

      Open the Repositories page

    2. Click Create Repository.

    3. Specify quickstart-maven-repo as the repository name.

    4. Choose Maven as the format.

    5. Under Location Type, select Region and then choose the location us-central1.

    6. Click Create.

    The repository is added to the repository list.

    gcloud

    1. Run the following command to create a new Maven repository in the current project named quickstart-maven-repo in the location us-central1.

      gcloud artifacts repositories create quickstart-maven-repo --repository-format=maven \
      --location=us-central1 [--description="Maven repository"]
      
    2. Run the following command to verify that your repository was created:

      gcloud artifacts repositories list

    For more information about Artifact Registry commands, run the command gcloud artifacts.

  2. To simplify gcloud commands, set the default repository to quickstart-maven-repo and the default location to us-central1. After the values are set, you do not need to specify them in gcloud commands that require a repository or a location.

    To set the repository, run the command:

    gcloud config set artifacts/repository quickstart-maven-repo
    

    To set the location, run the command:

    gcloud config set artifacts/location us-central1
    

    For more information about these commands, see the gcloud config set documentation.

Set up a service account for your client

  1. Create a service account to act on behalf of your application, or choose an existing service account that you use for automation.

    You will need the location of the service account key file to set up authentication with Artifact Registry. For existing accounts, you can view keys and create new keys on the Service Accounts page.

    Go to the Service Accounts page

  2. Grant the service account read and write access to your repository.

    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.

  3. Assign the service account key file location to the variable GOOGLE_APPLICATION_CREDENTIALS so that the Artifact Registry credential helper can obtain your key when connecting with repositories.

    export GOOGLE_APPLICATION_CREDENTIALS=KEY-FILE
    

    Where KEY-FILE is path to the service account key file.

    To learn about setting up password authentication with the service account key file instead, see authentication#auth-password documentation.

Configure Maven

  1. Choose a Maven project that you want to use.

    If you don't have a simple project available, you can create the Maven in 5 minutes project.

  2. Run the following command to print the settings for the default quickstart-maven-repo repository.

    gcloud artifacts print-settings mvn
    

    The output of the gcloud command looks like the following snippet, where PROJECT is your project ID.

    <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>
    
  3. Add the settings to the pom.xml file for your Maven project. The following outline shows the relative placement of each main element. See the Maven POM reference for details about the structure of the file.

    <project xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                          http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
      <!-- Build Settings -->
      <build>
        ...
        <extensions>...</extensions>
      </build>
    
      <!-- Environment Settings -->
      <repositories>...</repositories>
      <distributionManagement>...</distributionManagement>
    </project>
    

Configure Gradle

  1. Create a simple Gradle project to deploy a package to the Artifact Registry repository you created. If you don't have a test Gradle build, you can follow the introductory Gradle tutorial to create a simple Gradle build.

  2. To simplify gcloud commands, set the default repository to quickstart-maven-repo and the default location to us-central1. After the values are set, you do not need to specify them in gcloud commands that require a repository or a location.

    To set the repository, run the command:

    gcloud config set artifacts/repository quickstart-maven-repo
    

    To set the location, run the command:

    gcloud config set artifacts/location us-central1
    

    For more information about these commands, see the gcloud config set documentation.

  3. Add settings to connect to the repository to the build.gradle file. The following command prints settings to add for the default quickstart-maven-repo repository.

    gcloud artifacts print-settings gradle
    

    The output of the gcloud command looks like the following example, where PROJECT is your project ID.

    plugins {
      id "maven-publish"
      id "com.google.cloud.artifactregistry.gradle-plugin" version "2.1.1"
    }
    
    publishing {
      repositories {
        maven {
          url "artifactregistry://us-central1-maven.pkg.dev/PROJECT/quickstart-maven-repo"
        }
      }
    }
    
    repositories {
      maven {
        url "artifactregistry://us-central1-maven.pkg.dev/PROJECT/quickstart-maven-repo"
      }
    }
    
  4. Verify that the build.gradle file includes a publications section. The publications section is a child of publishing as shown in the following example:

    publishing {
      publications {
           mavenJava(MavenPublication) {
              groupId  'maven.example.id'
              from components.java
           }
      }
      repositories {
        maven {
          url "artifactregistry://us-central1-maven.pkg.dev/PROJECT/quickstart-maven-repo"
        }
      }
    }
    

You can also add repository configuration to your init.gradle or settings files. For details about these options, see Setting up authentication.

Upload a package to the repository

For Maven, you can use mvn deploy and mvn release to add packages to the repository.

For Gradle, use the gradle publish command to upload a package to the repository.

View the package in the repository

To verify that your package was added to the repository:

Console

  1. Open the Repositories page in the Cloud Console.

    Open the Repositories page

  2. In the repository list, click the quickstart-maven-repo repository.

    The Packages page lists the packages in the repository.

gcloud

To list the packages in the quickstart-maven-repo repository, run the following command:

 gcloud artifacts packages list --repository=quickstart-maven-repo

To view versions for a package, run the following command:

gcloud artifacts versions list --package=PACKAGE

Where PACKAGE is the package ID.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this quickstart, follow these steps.

Before you remove the repository, ensure that any packages you want to keep are available in another location.

To delete the repository:

Console

  1. Open the Repositories page in the Cloud Console.

    Open the Repositories page

  2. In the repository list, select the quickstart-maven-repo repository.

  3. Click Delete.

gcloud

  1. To delete the quickstart-maven-repo repository, run the following command:

    gcloud artifacts repositories delete quickstart-maven-repo
    
  2. If you want to remove the default repository and location settings that you configured for the active gcloud configuration, run the following commands:

    gcloud config unset artifacts/repository
    gcloud config unset artifacts/location
    

What's next