管理 Java 套件

本頁面說明如何新增、查看及刪除 Java 封裝。

如要使用 sbt 做為 Scala 和 Java 的建構工具,而非 Maven 或 Gradle,可以使用社群建立的 sbt 外掛程式。本文件不會說明 Scala 用戶端的設定或使用方式。

事前準備

  1. 如果目標存放區不存在,請建立新的存放區
  2. 確認您具備存放區的必要權限
  3. 確認您已設定驗證。如果使用存取權杖,請務必先更新權杖,再連線至存放區。
  4. (選用) 設定 gcloud 指令的預設值

必要的角色

如要取得管理套件所需的權限,請要求管理員在存放區中授予下列 IAM 角色:

如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

您或許還可透過自訂角色或其他預先定義的角色取得必要權限。

新增套件

存放區模式標準

如要將套件新增至存放區,請按照下列步驟操作:

Maven

使用 mvn deploymvn release 將套件新增至存放區。

如要順利部署參照父項的 Maven 專案,專案必須在核心擴充功能檔案中加入 Artifact Registry Wagon 提供者,詳情請參閱驗證說明。

使用 mvn deploy:deploy-file 上傳在 Maven 以外建構的構件。

舉例來說,下列指令會將 example/external.jar 及其專案檔案 example/pom.xml 部署至存放區 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

如要設定與 Cloud Build 的整合,請參閱「與 Cloud Build 整合」。

Gradle

如要順利發布至存放區,build.gradle 檔案必須包含 publications 區段,定義要上傳的檔案。

使用 gradle publish 指令將套件上傳至存放區。

查看套件和版本

存放區模式標準、遠端

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

Console

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

    開啟「存放區」頁面

  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.

如果是遠端存放區,傳回的清單應包含所有直接和遞移依附元件。

列出檔案

存放區模式標準、遠端

您可以列出存放區中的檔案、指定套件所有版本中的檔案,或套件特定版本中的檔案。

對於下列所有指令,您可以在指令中加入 --limit 標記,設定要傳回的檔案數量上限。

如要列出已設定預設值的預設專案、存放區和位置中的所有檔案,請執行下列指令:

gcloud artifacts files list

如要列出特定專案、存放區和位置中的檔案,請執行下列指令:

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

如要列出特定套件所有版本的檔案:

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

如要列出特定套件版本的檔案,請執行下列指令:

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

替換下列值:

  • LOCATION:存放區的區域或多區域位置
  • PROJECT:您的 Google Cloud 專案 ID。 如果專案 ID 包含冒號 (:),請參閱「網域範圍專案」。
  • REPOSITORY:儲存映像檔的存放區名稱。
  • PACKAGE:套件名稱。
  • VERSION:套件版本。

範例

請參考下列套件資訊:

  • 專案:my-project
  • 存放區:my-repo
  • 存放區位置:us-west1
  • 套件:my-app

下列指令會列出預設專案us-west1 位置的 my-repo 存放區內所有檔案:

gcloud artifacts files list \
    --location=us-west1 \
    --repository=my-repo
下列指令會列出套件 1.0 版中的檔案。

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

下載套件

存放區模式標準、遠端、虛擬

如要下載構件做為建構的一部分,請將構件宣告為依附元件。

Maven

  1. 在專案 pom.xml 檔案中宣告要下載的套件。 以下範例會將套件 artifact 的 1.0 版宣告為依附元件。

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

    如要進一步瞭解 Maven 依附元件,請參閱 Maven 的依附元件簡介依附元件參考資料

  2. 建構 Java 專案。

    mvn compile
    

Gradle

  1. 在專案 build.gradle 檔案中宣告要下載的套件。 以下範例會將套件 artifact 的 1.0 版宣告為外部編譯依附元件。

    如要進一步瞭解 Gradle 依附元件,請參閱 Gradle 說明文件

    dependencies {
        compile group: 'group', name: 'artifact', version: '1.0'
    }
    
  2. 建構 Java 專案。

    gradle build
    

如果是標準存放區,您可以直接從存放區下載套件。

如果是遠端存放區,您會下載套件及其依附元件的快取副本。如果沒有快取副本,遠端存放區會從上游來源下載套件並快取,然後再提供給您。您可以查看存放區中的套件清單,確認遠端存放區已從上游來源擷取套件。

如果是虛擬存放區,Artifact Registry 會在上游存放區中搜尋要求的套件。

  • 如果沒有快取副本,上游遠端存放區就會下載並快取要求的套件。虛擬存放區只會提供所要求的套件,不會儲存這些套件。
  • 如果您要求的版本在多個上游存放區中都有,Artifact Registry 會根據為虛擬存放區設定的優先順序,選擇要使用的上游存放區。

舉例來說,假設虛擬存放區的上游存放區有下列優先順序設定:

  • main-repo:優先順序設為 100
  • secondary-repo1:優先順序設為 80
  • secondary-repo2:優先順序設為 80
  • test-repo:優先順序設為 20

main-repo 的優先順序值最高,因此虛擬存放區一律會先搜尋這個存放區。

secondary-repo1secondary-repo2 的優先順序都設為 80。如果 main-repo 中沒有要求的套件,Artifact Registry 會接著搜尋這些存放區。由於兩者具有相同的優先順序值,如果兩個存放區都有該版本,Artifact Registry 可以選擇從任一存放區提供套件。

test-repo 的優先順序值最低,如果其他上游存放區都沒有儲存構件,就會提供該構件。

刪除套件和版本

存放區模式標準、遠端

您可以刪除套件和所有版本,也可以刪除特定版本。

  • 套件刪除後即無法復原。
  • 如果是遠端存放區,系統只會刪除套件的快取副本。上游來源不受影響。如果刪除快取的套件,下次存放區收到相同套件版本的請求時,Artifact Registry 會再次下載並快取該套件。

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.

    開啟「存放區」頁面

  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.

    開啟「存放區」頁面

  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.

後續步驟