arrow_back

Create a Maven Artifact Registry and Upload Code

Anmelden Teilnehmen
Zugriff auf über 700 Labs und Kurse nutzen

Create a Maven Artifact Registry and Upload Code

Lab 30 Minuten universal_currency_alt 1 Guthabenpunkt show_chart Einsteiger
info Dieses Lab kann KI-Tools enthalten, die den Lernprozess unterstützen.
Zugriff auf über 700 Labs und Kurse nutzen

gem-artifact-registry-maven

Google Cloud self-paced labs logo

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

  2. Your output should now look like this:

Output:

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = <project_ID>

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Overview

In this lab, you will learn how to create a Maven Artifact Registry in Google Cloud and upload a sample Maven project to it. You'll use the gcloud CLI and Maven commands to interact with Artifact Registry, enabling you to store and manage your Java dependencies effectively. This lab assumes you have basic familiarity with Google Cloud, Maven, and command-line operations.

Task 1. Enable Artifact Registry API and Configure gcloud

Enable the Artifact Registry API and configure the gcloud CLI to interact with your Google Cloud project.

  1. Enable the Artifact Registry API:
gcloud services enable artifactregistry.googleapis.com Note:
This command enables the Artifact Registry API in your project.
  1. Set your Project ID:
gcloud config set project {{{ project_0.project_id | "PROJECT_ID" }}} Note:
This command sets your active project identity.
  1. Set your default region to
gcloud config set compute/region {{{ project_0.default_region | "REGION" }}} Note:
This command sets your active compute region.

Task 2. Create a Maven Repository in Artifact Registry

Create a new Maven repository in Artifact Registry to store your Maven artifacts.

  1. Create a new Maven repository in Artifact Registry.
gcloud artifacts repositories create my-maven-repo \ --repository-format=maven \ --location={{{ project_0.default_region | "REGION" }}} \ --description="Maven repository" Note:
Replace my-maven-repo with your desired repository name.
  1. Verify the repository was created successfully by listing the available repositories.
gcloud artifacts repositories list --location={{{ project_0.default_region | "REGION" }}} Note:
This command lists all Artifact Registry repositories in the specified region.

Task 3. Configure Maven for Artifact Registry

Configure Maven to authenticate with Artifact Registry.

  1. Configure Maven authentication using the gcloud CLI.
gcloud artifacts print-settings mvn --repository=my-maven-repo --project={{{ project_0.project_id | "PROJECT_ID" }}} --location={{{ project_0.default_region | "REGION" }}} Note:
Replace my-maven-repo with your desired repository name. This command generates XML configuration for Maven.
  1. The output from the previous command will be similar to below:
artifact-registry artifactregistry://{{{ project_0.default_region | "REGION" }}}-maven.pkg.dev/{{{ project_0.project_id | "PROJECT_ID" }}}/my-maven-repo artifact-registry artifactregistry://{{{ project_0.default_region | "REGION" }}}-maven.pkg.dev/{{{ project_0.project_id | "PROJECT_ID" }}}/my-maven-repo artifact-registry artifactregistry://{{{ project_0.default_region | "REGION" }}}-maven.pkg.dev/{{{ project_0.project_id | "PROJECT_ID" }}}/my-maven-repo true true com.google.cloud.artifactregistry artifactregistry-maven-wagon 2.2.0 Note:
The output provides a valuable template that can be used to define the settings for an example Maven configuration.

Task 4. Create and Deploy a Sample Maven Project

Create a sample Maven project and deploy it to Artifact Registry.

  1. Create a new Maven project using the mvn archetype:generate command. Choose a suitable archetype (e.g., maven-archetype-quickstart).
mvn archetype:generate \ -DgroupId=com.example \ -DartifactId=my-app \ -Dversion=1.0-SNAPSHOT \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DinteractiveMode=false Note:
This command generates a basic Maven project structure. On successful completion a file pom.xml will be generated in the designated folder.
  1. Navigate to the project directory.
cd my-app Note:
Change directory to your new maven app
  1. Output the mvn Artifact Registry settings to a file example.xml.
gcloud artifacts print-settings mvn --repository=my-maven-repo --project={{{ project_0.project_id | "PROJECT_ID" }}} --location={{{ project_0.default_region | "REGION" }}} > example.pom Note:
If you are unsure how to make the edits, use the example.xml file to validate the sample application settings.
  1. Update the generated pom.xml file to include the Artifact Registry repository information for deployment. Add a <distributionManagement> section:
artifact-registry artifactregistry://{{{ project_0.default_region | "REGION" }}}-maven.pkg.dev/{{{ project_0.project_id | "PROJECT_ID" }}}/my-maven-repo artifact-registry artifactregistry://{{{ project_0.default_region | "REGION" }}}-maven.pkg.dev/{{{ project_0.project_id | "PROJECT_ID" }}}/my-maven-repo Note:
Place the new distributionManagement xml section within the project block of the pom.xml.
  1. Add a build section to provider Artifact Registry Authentication in the pom.xml
com.google.cloud.artifactregistry artifactregistry-maven-wagon 2.2.0 Note:
Place the new build xml section within the project block of the pom.xml.
  1. The updated pom.xml will look similar to below:
4.0.0 com.example my-app jar 1.0-SNAPSHOT my-app http://maven.apache.org com.google.cloud.artifactregistry artifactregistry-maven-wagon 2.2.0 artifact-registry artifactregistry://{{{ project_0.default_region | "REGION" }}}-maven.pkg.dev/{{{ project_0.project_id | "PROJECT_ID" }}}/my-maven-repo artifact-registry artifactregistry://{{{ project_0.default_region | "REGION" }}}-maven.pkg.dev/{{{ project_0.project_id | "PROJECT_ID" }}}/my-maven-repo junit junit 3.8.1 test Note:
The above changes enable the build process to interact with Artifact Registry.
  1. Deploy the artifact to Artifact Registry using the mvn deploy command.
mvn deploy Note:
This command deploys the Maven artifact to Artifact Registry.

Task 5. Validate Artifact Registry

  1. Verify the artifact was deployed successfully by navigating to your Artifact Registry repository in the Google Cloud Console or by listing the artifacts using the gcloud CLI.
gcloud artifacts versions list --repository=my-maven-repo --package=com.example:my-app --location={{{ project_0.default_region | "REGION" }}} Note:
This command lists package in the specified repository.
  1. Verify that your package is listed in the Artifact Registry repository.
PACKAGE: my-package CREATE_TIME: 2025-06-25T06:06:10 UPDATE_TIME: 2025-06-25T06:06:11 ANNOTATIONS:

Congratulations!

You have successfully created a Maven Artifact Registry, configured Maven to authenticate with it, and deployed a sample Maven project. This process allows you to securely store and manage your Java dependencies within Google Cloud.

Additional Resources

Manual Last Updated Jun 25, 2025

Lab Last Tested Jun 25, 2025

Vorbereitung

  1. Labs erstellen ein Google Cloud-Projekt und Ressourcen für einen bestimmten Zeitraum
  2. Labs haben ein Zeitlimit und keine Pausenfunktion. Wenn Sie das Lab beenden, müssen Sie von vorne beginnen.
  3. Klicken Sie links oben auf dem Bildschirm auf Lab starten, um zu beginnen

Privates Surfen verwenden

  1. Kopieren Sie den bereitgestellten Nutzernamen und das Passwort für das Lab
  2. Klicken Sie im privaten Modus auf Konsole öffnen

In der Konsole anmelden

  1. Melden Sie sich mit Ihren Lab-Anmeldedaten an. Wenn Sie andere Anmeldedaten verwenden, kann dies zu Fehlern führen oder es fallen Kosten an.
  2. Akzeptieren Sie die Nutzungsbedingungen und überspringen Sie die Seite zur Wiederherstellung der Ressourcen
  3. Klicken Sie erst auf Lab beenden, wenn Sie das Lab abgeschlossen haben oder es neu starten möchten. Andernfalls werden Ihre bisherige Arbeit und das Projekt gelöscht.

Diese Inhalte sind derzeit nicht verfügbar

Bei Verfügbarkeit des Labs benachrichtigen wir Sie per E-Mail

Sehr gut!

Bei Verfügbarkeit kontaktieren wir Sie per E-Mail

Es ist immer nur ein Lab möglich

Bestätigen Sie, dass Sie alle vorhandenen Labs beenden und dieses Lab starten möchten

Privates Surfen für das Lab verwenden

Nutzen Sie den privaten oder Inkognitomodus, um dieses Lab durchzuführen. So wird verhindert, dass es zu Konflikten zwischen Ihrem persönlichen Konto und dem Teilnehmerkonto kommt und zusätzliche Gebühren für Ihr persönliches Konto erhoben werden.