このページでは、Java パッケージの追加、表示、削除について説明します。
Maven や Gradle を使用するのではなく、sbt を Scala と Java 用のビルドツールとして使用する場合、コミュニティで作成された sbt プラグインをご利用いただけます。このドキュメントでは、Scala クライアントの構成や使用法は説明しません。
始める前に
- ターゲット リポジトリが存在しない場合は、新しいリポジトリを作成します。
- リポジトリに必要な権限が付与されていることを確認します。
- 認証を構成済みであることを確認します。アクセス トークンを使用している場合は、リポジトリに接続する前にトークンを更新するようにしてください。
- (省略可)gcloud コマンドのデフォルトを構成します。
必要なロール
パッケージの管理に必要な権限を取得するには、リポジトリに対する次の IAM ロールの付与を管理者に依頼してください。
- パッケージまたはパッケージ内のファイルを表示する:
Artifact Registry 読み取り(
roles/artifactregistry.reader
) - パッケージをダウンロードまたはインストールする:
Artifact Registry 読み取り(
roles/artifactregistry.reader
) -
リポジトリにパッケージを追加する:
Artifact Registry 書き込み(
roles/artifactregistry.writer
) -
パッケージを削除する:
Artifact Registry リポジトリ管理者(
roles/artifactregistry.repoAdmin
)
ロールの付与については、プロジェクト、フォルダ、組織へのアクセスを管理するをご覧ください。
必要な権限は、カスタムロールや他の事前定義ロールから取得することもできます。
パッケージの追加
リポジトリ モード: 標準
リポジトリにパッケージを追加するには:
Maven
mvn deploy
と mvn 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
Open the Repositories page in the Google Cloud console.
In the repository list, click the appropriate repository.
The Packages page lists the packages in the repository.
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
ダウンロードするパッケージをプロジェクトの
pom.xml
ファイルで宣言します。次の例では、パッケージartifact
のバージョン 1.0 を依存関係として宣言します。<dependencies> <dependency> <groupId>group</groupId> <artifactId>artifact</artifactId> <version>1.0</version> </dependency> </dependencies>
Maven の依存関係について詳しくは、Maven の依存関係の概要と依存関係のリファレンスをご覧ください。
Java プロジェクトをビルドします。
mvn compile
Gradle
ダウンロードするパッケージをプロジェクトの
build.gradle
ファイルで宣言します。次の例では、パッケージartifact
のバージョン 1.0 を外部コンパイルの依存関係として宣言します。Gradle の依存関係について詳しくは、Gradle のドキュメントをご覧ください。
dependencies { compile group: 'group', name: 'artifact', version: '1.0' }
Java プロジェクトをビルドします。
gradle build
標準リポジトリの場合は、リポジトリからパッケージを直接ダウンロードします。
リモート リポジトリの場合は、パッケージとその依存関係のキャッシュに保存されたコピーをダウンロードします。キャッシュに保存されたコピーが存在しない場合は、リモート リポジトリがアップストリーム ソースからパッケージをダウンロードし、それを提供する前にキャッシュに保存します。リモート リポジトリがアップストリーム ソースからパッケージを取得したことを確認するには、リポジトリ内のパッケージの一覧を表示します。
仮想リポジトリの場合は、Artifact Registry はリクエストされたパッケージのアップストリーム リポジトリを検索します。
- キャッシュに保存されたコピーが存在しない場合、アップストリームのリモート リポジトリは、リクエストされたパッケージをダウンロードしてキャッシュに保存します。仮想リポジトリはリクエストされたパッケージを提供だけして、保存しません。
- 複数のアップストリーム リポジトリに存在する利用可能なバージョンをリクエストすると、Artifact Registry は仮想リポジトリ用に構成された優先度設定に基づいて、使用するアップストリーム リポジトリを選択します。
たとえば、アップストリーム リポジトリに次の優先度設定がある仮想リポジトリについて考えます。
main-repo
: 優先度が100
に設定secondary-repo1
: 優先度が80
に設定secondary-repo2
: 優先度が80
に設定test-repo
: 優先度が20
に設定
main-repo
の優先度の値は最も高いため、仮想リポジトリは常に最初に検索します。
secondary-repo1
と secondary-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
Open the Repositories page in the Google Cloud console.
In the repository list, click the appropriate repository.
The Packages page lists the packages in the repository.
Select the package that you want to delete.
Click DELETE.
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
Open the Repositories page in the Google Cloud console.
In the repository list, click the appropriate repository.
The Packages page lists the packages in the repository.
Click a package to view versions of that package.
Select versions that you want to delete.
Click DELETE.
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.