Mengelola paket Java

Halaman ini menjelaskan cara menambahkan, melihat, dan menghapus paket Java.

Jika Anda ingin menggunakan sbt sebagai alat build untuk Scala dan Java, bukan menggunakan Maven atau Gradle, ada plugin sbt yang dibuat komunitas tersedia. Dokumentasi ini tidak menjelaskan konfigurasi atau penggunaan klien Scala.

Sebelum memulai

  1. Jika repositori target tidak ada, buat repositori baru.
  2. Pastikan Anda memiliki izin yang diperlukan untuk repositori.
  3. Pastikan Anda telah mengonfigurasi autentikasi. Jika Anda menggunakan token akses, pastikan Anda memperbarui token sebelum terhubung ke repositori.
  4. (Opsional) Konfigurasi default untuk perintah gcloud.

Peran yang diperlukan

Untuk mendapatkan izin yang Anda perlukan untuk mengelola paket, minta administrator untuk memberi Anda peran IAM berikut di repositori:

Untuk mengetahui informasi selengkapnya tentang pemberian peran, lihat Mengelola akses ke project, folder, dan organisasi.

Anda mungkin juga bisa mendapatkan izin yang diperlukan melalui peran khusus atau peran bawaan lainnya.

Menambahkan paket

Mode repositori: Standar

Untuk menambahkan paket ke repositori:

Maven

Gunakan mvn deploy dan mvn release untuk menambahkan paket ke repositori.

Agar berhasil men-deploy project Maven yang mereferensikan induk, project harus menyertakan penyedia Wagon Artifact Registry dalam file ekstensi inti seperti yang dijelaskan dalam petunjuk autentikasi.

Gunakan mvn deploy:deploy-file untuk mengupload artefak yang dibuat di luar Maven.

Misalnya, perintah contoh ini men-deploy example/external.jar dan file project-nya example/pom.xml ke repositori us-central1-maven.pkg.dev/my-project/my-repo

mvn deploy:deploy-file \
-Durl=artifactregistry://us-central1-maven.pkg.dev/my-project/my-repo \
-DpomFile=example/pom.xml -Dfile=example/external.jar

Untuk mengonfigurasi integrasi dengan Cloud Build, lihat Mengintegrasikan dengan Cloud Build.

Gradle

Agar berhasil memublikasikan ke repositori, file build.gradle Anda harus menyertakan bagian publikasi yang menentukan file yang akan diupload.

Gunakan perintah gradle publish untuk mengupload paket ke repositori.

Melihat paket dan versi

Mode repositori: standar, jarak jauh

To view packages and package versions using the Google Cloud console or gcloud:

Console

  1. Open the Repositories page in the Google Cloud console.

    Buka halaman Repositori

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Click a package to view versions of the package.

gcloud

To list packages in a repository, run the following command:

gcloud artifacts packages list [--repository=REPOSITORY] [--location=LOCATION]

Replace the following:

  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. If you configured a default location, then you can omit this flag to use the default.

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

gcloud artifacts versions list --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION]

Replace the following:

  • PACKAGE is the ID of the package or fully qualified identifier for the package.
  • REPOSITORY is the name of the repository. If you configured a default repository, then you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.

Untuk repositori jarak jauh, daftar yang ditampilkan harus menyertakan semua dependensi langsung dan transitif.

Membuat daftar file

Mode repositori: standar, jarak jauh

Anda dapat membuat daftar file di repositori, file di semua versi paket tertentu, atau file di versi paket tertentu.

Untuk semua perintah berikut, Anda dapat menetapkan jumlah maksimum file yang akan ditampilkan dengan menambahkan flag --limit ke perintah.

Untuk membuat daftar semua file di project, repositori, dan lokasi saat nilai default dikonfigurasi:

gcloud artifacts files list

Untuk membuat daftar file di project, repositori, dan lokasi tertentu, jalankan perintah:

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION

Untuk membuat daftar file untuk semua versi paket tertentu:

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION \
    --package=PACKAGE

Untuk membuat daftar file untuk versi paket tertentu:

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION \
    --package=PACKAGE \
    --version=VERSION

Ganti nilai berikut:

Contoh

Pertimbangkan informasi paket berikut:

  • Project: my-project
  • Repositori: my-repo
  • Lokasi repositori: us-west1
  • Paket: my-app

Perintah berikut mencantumkan semua file di repositori my-repo di lokasi us-west1 dalam project default:

gcloud artifacts files list \
    --location=us-west1 \
    --repository=my-repo
Perintah berikut mencantumkan file dalam paket versi 1.0.

gcloud artifacts files list \
    --project=my-project \
    --location=us-west1 \
    --repository=my-repo \
    --package=my-app \
    --version=1.0

Mendownload paket

Mode repositori: standar, jarak jauh, virtual

Untuk mendownload artefak sebagai bagian dari build, Anda mendeklarasikan artefak sebagai dependensi.

Maven

  1. Deklarasikan paket yang ingin Anda download dalam file pom.xml project. Contoh berikut mendeklarasikan paket versi 1.0 artifact sebagai dependensi.

    <dependencies>
        <dependency>
            <groupId>group</groupId>
            <artifactId>artifact</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
    

    Untuk mempelajari dependensi Maven lebih lanjut, lihat pengantar Maven tentang dependensi dan referensi dependensi.

  2. Buat project Java.

    mvn compile
    

Gradle

  1. Deklarasikan paket yang ingin Anda download dalam file build.gradle project. Contoh berikut mendeklarasikan paket versi 1.0 artifact sebagai dependensi kompilasi eksternal.

    Untuk mempelajari dependensi Gradle lebih lanjut, lihat dokumentasi Gradle.

    dependencies {
        compile group: 'group', name: 'artifact', version: '1.0'
    }
    
  2. Buat project Java.

    gradle build
    

Untuk repositori standar, Anda mendownload paket langsung dari repositori.

Untuk repositori jarak jauh, Anda mendownload salinan paket yang di-cache dan dependensinya. Jika salinan yang di-cache tidak ada, repositori jarak jauh akan mendownload paket dari sumber upstream dan meng-cache-nya sebelum menayangkannya kepada Anda. Anda dapat memverifikasi bahwa repositori jarak jauh mengambil paket dari sumber upstream dengan melihat daftar paket di repositori.

Untuk repositori virtual, Artifact Registry menelusuri repositori upstream untuk paket yang diminta.

  • Repositori jarak jauh upstream akan mendownload dan meng-cache paket yang diminta jika salinan yang di-cache tidak ada. Repositori virtual hanya menayangkan paket yang diminta, bukan menyimpannya.
  • Jika Anda meminta versi yang tersedia di lebih dari satu repositori upstream, Artifact Registry akan memilih repositori upstream yang akan digunakan berdasarkan setelan prioritas yang dikonfigurasi untuk repositori virtual.

Misalnya, pertimbangkan repositori virtual dengan setelan prioritas berikut untuk repositori upstream:

  • main-repo: Prioritas ditetapkan ke 100
  • secondary-repo1: Prioritas ditetapkan ke 80.
  • secondary-repo2: Prioritas ditetapkan ke 80.
  • test-repo: Prioritas ditetapkan ke 20.

main-repo memiliki nilai prioritas tertinggi, sehingga repositori virtual selalu menelusurinya terlebih dahulu.

secondary-repo1 dan secondary-repo2 memiliki prioritas yang ditetapkan ke 80. Jika paket yang diminta tidak tersedia di main-repo, Artifact Registry akan menelusuri repositori ini berikutnya. Karena keduanya memiliki nilai prioritas yang sama, Artifact Registry dapat memilih untuk menayangkan paket dari salah satu repositori jika versi tersebut tersedia di keduanya.

test-repo memiliki nilai prioritas terendah dan akan menayangkan artefak yang disimpan jika tidak ada repositori upstream lain yang memilikinya.

Menghapus paket dan versi

Mode repositori: standar, jarak jauh

Anda dapat menghapus paket dan semua versinya, atau menghapus versi tertentu.

  • Setelah menghapus paket, Anda tidak dapat mengurungkan tindakan tersebut.
  • Untuk repositori jarak jauh, hanya salinan paket yang di-cache yang dihapus. Sumber upstream tidak terpengaruh. Jika Anda menghapus paket yang di-cache, Artifact Registry akan mendownload dan meng-cache-nya lagi saat repositori menerima permintaan untuk versi paket yang sama.

Before you delete a package or package version, verify that any you have communicated or addressed any important dependencies on it.

To delete a package:

Console

  1. Open the Repositories page in the Google Cloud console.

    Buka halaman Repositori

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Select the package that you want to delete.

  4. Click DELETE.

  5. In the confirmation dialog box, click DELETE.

gcloud

Run the following command:

gcloud artifacts packages delete PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION] [--async]

Replace the following:

  • PACKAGE is the name of the package in the repository.
  • REPOSITORY is the name of the repository. If you configured a default repository, then you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.

The --async flag causes the command to return immediately, without waiting for the operation in progress to complete.

To delete versions of a package:

Console

  1. Open the Repositories page in the Google Cloud console.

    Buka halaman Repositori

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Click a package to view versions of that package.

  4. Select versions that you want to delete.

  5. Click DELETE.

  6. In the confirmation dialog box, click DELETE.

gcloud

Run the following command:

gcloud artifacts versions delete VERSION \
    --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION] \
    [--async]

Replace the following:

  • VERSION is the name of the version to delete.
  • PACKAGE is the name of the package in the repository.
  • REPOSITORY is the name of the repository. If you configured a default repository, then you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.

The --async flag causes the command to return immediately, without waiting for the operation in progress to complete.

Langkah berikutnya