1. 概览
借助 Artifact Registry,您可以存储不同的工件类型;在单个项目中创建多个代码库,并将某个特定区域或多区域与每个代码库相关联。代码库模式有多种。每种模式的用途各不相同。下图展示了您可以将不同模式的代码库搭配使用的方式之一。该图显示了两个 Google Cloud 项目之间的工作流。在开发项目中,开发者构建 Java 应用。在单独的运行时项目中,另一个 build 会创建包含应用的容器映像,以便部署到 Google Kubernetes Engine。
在本实验中,您将学习如何执行以下任务。
- 使用标准代码库部署私有软件包
- 使用远程仓库缓存 Maven 中央制品库
- 使用虚拟代码库在一个配置中组合多个上游代码库
自定进度的环境设置
- 登录 Google Cloud 控制台,然后创建一个新项目或重复使用现有项目。如果您还没有 Gmail 或 Google Workspace 账号,则必须创建一个。
- 项目名称是此项目参与者的显示名称。它是 Google API 尚未使用的字符串。您可以随时更新。
- 项目 ID 在所有 Google Cloud 项目中是唯一的,并且是不可变的(一经设置便无法更改)。Cloud 控制台会自动生成一个唯一字符串;通常情况下,您无需关注该字符串。在大多数 Codelab 中,您都需要引用项目 ID(通常用
PROJECT_ID
标识)。如果您不喜欢生成的 ID,可以再随机生成一个 ID。或者,您也可以尝试自己的项目 ID,看看是否可用。完成此步骤后便无法更改该 ID,并且此 ID 在项目期间会一直保留。 - 此外,还有第三个值,即部分 API 使用的项目编号,供您参考。如需详细了解所有这三个值,请参阅文档。
- 接下来,您需要在 Cloud 控制台中启用结算功能,以便使用 Cloud 资源/API。运行此 Codelab 应该不会产生太多的费用(如果有费用的话)。若要关闭资源以避免产生超出本教程范围的结算费用,您可以删除自己创建的资源或删除整个项目。Google Cloud 的新用户符合参与 $300 USD 免费试用计划的条件。
Workspace 设置
设置 gcloud
在 Cloud Shell 中,设置您的项目 ID 和项目编号。将它们保存为 PROJECT_ID
和 PROJECT_NUMBER
变量。
export PROJECT_ID=$(gcloud config get-value project)
export PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')
启用 API
gcloud services enable artifactregistry.googleapis.com
克隆代码库
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/container-registry/container-analysis
2. 标准代码库
标准代码库提供了一种存储私有软件包并在其他应用中共享这些软件包的方式
创建标准 Maven 代码库
在 Cloud Shell 中,运行以下命令以创建 Java 工件仓库:
gcloud artifacts repositories create container-dev-java-repo \
--repository-format=maven \
--location=us-central1 \
--description="Java package repository for Container Dev Workshop"
如果系统显示 Cloud Shell 授权提示,请点击“授权”
前往 Google Cloud 控制台 - Artifact Registry - Repositories,然后找到您新创建的名为 container-dev-java-repo
的 Maven 代码库。如果您点击该代码库,会发现它目前是空的。
gcloud artifacts repositories describe container-dev-java-repo \
--location=us-central1
应返回类似以下内容的响应
Encryption: Google-managed key Repository Size: 0.000MB createTime: '2023-03-21T19:01:45.461589Z' description: Java package repository for Container Dev Workshop format: MAVEN mavenConfig: {} mode: STANDARD_REPOSITORY name: projects/qwiklabs-gcp-03-4304110dc461/locations/us-central1/repositories/container-dev-java-repo updateTime: '2023-03-21T19:01:45.461589Z'
为 Artifact Registry 配置 Maven
运行以下命令来输出要添加到 Java 项目的代码库配置:
gcloud artifacts print-settings mvn \
--repository=container-dev-java-repo \
--location=us-central1
上述命令会返回要添加到项目 pom.xml 中的 xml。
- repositories 部分指定 Maven 可以将远程工件下载到何处,以供当前项目使用。
- distributionManagement 部分用于指定项目在部署时将推送到哪个远程代码库。
- extensions 部分添加了 artifactregistry-maven-wagon,该 wagon 启用了连接到 Artifact Registry 所需的身份验证和传输层
- 注意:扩展程序可以存在于 pom.xml 或 extensions.xml 中。如果项目依赖于父项目,则在加载 pom.xml 中的其余条目之前会访问这些依赖项。为确保父级有权访问该扩展程序,您可以将其放置在 extensions.xml 文件中,该文件会在 pom.xml 之前加载,以便父级依赖项可以使用该扩展程序。
复制这三个部分,然后在 Cloud Shell Editor 中打开 pom.xml
,并将返回的设置添加到文件底部的闭合 project
标记内。
提示:在 Cloud Shell 中,在终端中运行以下命令,即可在当前目录中打开编辑器。
cloudshell workspace .
示例:(您的项目名称在网址中会有所不同)
...
<distributionManagement>
<snapshotRepository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
</snapshotRepository>
<repository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
</repository>
</distributionManagement>
<repositories>
<repository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-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.2.0</version>
</extension>
</extensions>
</build>
</project>
将 Java 软件包上传到 Artifact Registry
在 Maven 中配置 Artifact Registry 后,您现在可以使用 Artifact Registry 存储 Java jar,以供贵组织中的其他项目使用。
运行以下命令将 Java 软件包上传到 Artifact Registry:
mvn deploy -DskipTests
如果您想再次运行此命令,请务必在 pom.xml 中提高版本。
在 Artifact Registry 中查看 Java 软件包
依次前往 Cloud 控制台 - Artifact Registry - 代码库。点击 container-dev-java-repo
,检查是否存在 hello-world
二进制工件:
3. 远程代码库
远程代码库可缓存第三方软件包,从而提高可靠性和安全性。
创建远程代码库
注意:如需详细了解身份验证和配置,请参阅产品文档。
在 Cloud Shell 中,运行以下命令为 Maven Central 工件创建远程仓库:
gcloud artifacts repositories create maven-central-cache \
--project=$PROJECT_ID \
--repository-format=maven \
--location=us-central1 \
--description="Remote repository for Maven Central caching" \
--mode=remote-repository \
--remote-repo-config-desc="Maven Central" \
--remote-mvn-repo=MAVEN-CENTRAL
在控制台中查看代码库
依次前往 Cloud 控制台 - Artifact Registry - 代码库 点击 maven-central-cache
,您会发现它已创建且目前为空
在终端中查看代码库
gcloud artifacts repositories describe maven-central-cache \
--location=us-central1
将代码库集成到您的项目中
运行以下命令来输出要添加到 Java 项目的代码库配置:
gcloud artifacts print-settings mvn \
--repository=maven-central-cache \
--location=us-central1
将仓库部分添加到 pom.xml 中。请勿从输出中复制外部 <repositories> 标记。
将新添加的代码库的 ID 更改为“central”,以确保每个代码库条目都有唯一的 ID。
示例:(您的项目名称在网址中会有所不同)
...
<distributionManagement>
<snapshotRepository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
</snapshotRepository>
<repository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
</repository>
</distributionManagement>
<repositories>
<repository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>central</id>
<url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/maven-central-cache</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.2.0</version>
</extension>
</extensions>
</build>
</project>
在终端中运行以下命令,为您的项目创建 extensions.xml
,以使用核心扩展程序机制,确保 Maven 可以从 Artifact Registry 解析父级或插件依赖项。
mkdir .mvn
cat > .mvn/extensions.xml << EOF
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
<extension>
<groupId>com.google.cloud.artifactregistry</groupId>
<artifactId>artifactregistry-maven-wagon</artifactId>
<version>2.2.0</version>
</extension>
</extensions>
EOF
从远程代码库拉取依赖项
运行以下命令,使用远程代码库编译应用:
rm -rf ~/.m2/repository
mvn compile
在控制台中查看软件包
前往 Cloud 控制台 - Artifact Registry - 代码库。点击 maven-central-cache
,检查其中是否缓存了二进制工件:
4. 虚拟代码库
虚拟代码库充当一个接口,可通过单个配置访问多个代码库。这样可以简化工件使用方的客户端配置,并通过减少依赖项混淆攻击来提高安全性。
创建政策文件
cat > ./policy.json << EOF
[
{
"id": "private",
"repository": "projects/${PROJECT_ID}/locations/us-central1/repositories/container-dev-java-repo",
"priority": 100
},
{
"id": "central",
"repository": "projects/${PROJECT_ID}/locations/us-central1/repositories/maven-central-cache",
"priority": 80
}
]
EOF
创建虚拟代码库
gcloud artifacts repositories create virtual-maven-repo \
--project=${PROJECT_ID} \
--repository-format=maven \
--mode=virtual-repository \
--location=us-central1 \
--description="Virtual Maven Repo" \
--upstream-policy-file=./policy.json
将代码库集成到您的项目中
运行以下命令来输出要添加到 Java 项目的代码库配置:
gcloud artifacts print-settings mvn \
--repository=virtual-maven-repo \
--location=us-central1
将 pom 中的整个仓库部分替换为输出中的虚拟仓库部分。
示例:(您的项目名称在网址中会有所不同)
...
<distributionManagement>
<snapshotRepository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
</snapshotRepository>
<repository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
</repository>
</distributionManagement>
<repositories>
<repository>
<id>artifact-registry</id>
<url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/virtual-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.2.0</version>
</extension>
</extensions>
</build>
</project>
从虚拟代码库拉取依赖项
由于虚拟仓库是直通的,不会存储任何实际软件包,因此为了清晰地演示此过程,您将删除之前创建的 maven-central-cache 仓库并重新创建该仓库,以便从一个空仓库重新开始
运行以下命令以重新创建缓存仓库
gcloud artifacts repositories delete maven-central-cache \
--project=$PROJECT_ID \
--location=us-central1 \
--quiet
gcloud artifacts repositories create maven-central-cache \
--project=$PROJECT_ID \
--repository-format=maven \
--location=us-central1 \
--description="Remote repository for Maven Central caching" \
--mode=remote-repository \
--remote-repo-config-desc="Maven Central" \
--remote-mvn-repo=MAVEN-CENTRAL
您可以在控制台中查看空仓库。前往 Cloud 控制台 - Artifact Registry - 代码库
现在,使用以下命令构建项目,以便使用虚拟代码库
rm -rf ~/.m2/repository
mvn compile
在控制台中查看软件包
前往 Cloud 控制台 - Artifact Registry - 代码库。点击 maven-central-cache
,检查二进制工件是否已配置为从虚拟代码库拉取,但最终从 maven-central-cache
拉取:
5. 恭喜!
恭喜,您已完成此 Codelab!
您学习了以下内容
- 使用标准代码库部署私有软件包
- 使用远程代码库缓存 Maven 中央软件包
- 使用虚拟代码库在一个配置中组合多个上游代码库
清理
运行以下命令以删除项目
gcloud projects delete ${PROJECT_ID}
—
上次更新时间:2023 年 3 月 22 日