1. Overview
1.概述
In this tutorial, we’ll explain the differences between Maven snapshot and release repositories.
在本教程中,我们将解释Maven快照和发布库的区别。
2. Maven Repositories
2.Maven Repositories
A Maven repository holds a collection of pre-compiled artifacts that we can use in our application as dependencies. With traditional Java applications, these are usually .jar files.
Maven资源库存放着一系列预编译的工件,我们可以在应用中作为依赖项使用。在传统的Java应用程序中,这些通常是.jar文件。
Generally, there are two types of repositories: local and remote.
一般来说,有两种类型的存储库:本地和远程。
A local repository is the repository Maven creates on the computer it is building on. It is usually located under the $HOME/.m2/repository directory.
本地资源库是Maven在其构建的计算机上创建的资源库。它通常位于$HOME/.m2/repository目录下。
When we build an application, Maven will search for the dependency in our local repository. If a certain dependency isn’t found, Maven will search for it in the remote repositories (defined inside the settings.xml or pom.xml files). Moreover, it will copy the dependency to our local repository for future use.
当我们构建一个应用程序时,Maven会在我们的本地仓库中搜索该依赖性。如果没有找到某个依赖,Maven会在远程仓库(定义在settings.xml或pom.xml文件中)搜索。此外,它还会将该依赖性复制到我们的本地仓库,以供日后使用。
A remote repository is an external repository that contains artifacts. Once Maven has downloaded an artifact from the remote repository, it will prefer to look for the artifact in the local repository to limit artifact downloads.
远程资源库是一个包含工件的外部资源库。一旦Maven从远程仓库下载了工件,它就会选择在本地仓库中寻找工件,以限制工件的下载。
Additionally, we can differentiate repositories based on the artifact types between snapshot and release repositories.
此外,我们可以根据快照和发布库的工件类型来区分存储库。
3. Snapshot Repository
3.快照库
The snapshot repository is a repository used for incremental, unreleased artifact versions.
快照库是一个用于增量的、未发布的工件版本的库房。
A snapshot version is a version that has not yet been released. The general idea is to have a snapshot version before the released version. It allows us to deploy the same transient version incrementally, without requiring projects to upgrade the artifact version they’re consuming. Those projects can use the same version to get an updated snapshot version.
快照版本是一个尚未发布的版本。一般的想法是在发布的版本之前有一个快照版本。它允许我们逐步部署相同的暂存版本,而不要求项目升级他们正在使用的工件版本。那些项目可以使用同一个版本来获得更新的快照版本。
For instance, before releasing version 1.0.0, we can have its snapshot version. The snapshot version has a SNAPSHOT suffix after the version (for example, 1.0.0-SNAPSHOT).
例如,在发布版本1.0.0之前,我们可以有其快照版本。快照版本在版本后面有一个SNAPSHOT后缀(例如,1.0.0-SNAPSHOT)。
3.1. Deploy an Artifact
3.1.部署一个工件
Continuous development commonly uses snapshot versioning. With the snapshot version, we can deploy an artifact whose number consists of a timestamp and build number.
持续开发通常使用快照版本。通过快照版本,我们可以部署一个工件,其编号由时间戳和构建编号组成。
Let’s assume we have a project under development with a SNAPSHOT version:
让我们假设我们有一个正在开发的项目,有一个SNAPSHOT版本。
<groupId>com.baeldung</groupId>
<artifactId>maven-snapshot-repository</artifactId>
<version>1.0.0-SNAPSHOT</version>
We’re going to deploy the project to a self-hosted Nexus repository.
我们将把该项目部署到一个自我托管的Nexus资源库。
Firstly, let’s define the publishing repository information where we want to deploy the artifact. We can use the distribution management plugin:
首先,让我们定义我们要部署工件的发布库信息。我们可以使用发布管理插件。
<distributionManagement>
<snapshotRepository>
<id>nexus</id>
<name>nexus-snapshot</name>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
Further, we’ll deploy our project using the mvn deploy command.
此外,我们将使用mvn deploy命令部署我们的项目。
After the deployment, the actual artifact version will contain a timestamp value instead of the SNAPSHOT value. For instance, when we deploy 1.0.0-SNAPSHOT, the actual value will contain the current timestamp and the build number (for example, 1.0.0-20220709.063105-3).
部署后,实际的工件版本将包含一个时间戳值,而不是SNAPSHOT值。例如,当我们部署1.0.0-SNAPSHOT时,实际值将包含当前的时间戳和构建号(例如,1.0.0-20220709.063105-3)。
The timestamp value is calculated during the artifact deployment. Maven generates the checksum and uploads the artifact’s files with the same timestamp.
时间戳值是在构件部署过程中计算的。Maven会生成校验和,并以相同的时间戳上传工件的文件。
The maven-metadata.xml file holds precise information about the snapshot version and its link to the latest timestamp value:
maven-metadata.xml文件持有快照版本的精确信息及其与最新时间戳值的链接。
<metadata modelVersion="1.1.0">
<groupId>com.baeldung</groupId>
<artifactId>maven-snapshot-repository</artifactId>
<version>1.0.0-SNAPSHOT</version>
<versioning>
<snapshot>
<timestamp>20220709.063105</timestamp>
<buildNumber>3</buildNumber>
</snapshot>
<lastUpdated>20220709063105</lastUpdated>
<snapshotVersions>
<snapshotVersion>
<extension>jar</extension>
<value>1.0.0-20220709.063105-3</value>
<updated>20220709063105</updated>
</snapshotVersion>
<snapshotVersion>
<extension>pom</extension>
<value>1.0.0-20220709.063105-3</value>
<updated>20220709063105</updated>
</snapshotVersion>
</snapshotVersions>
</versioning>
</metadata>
Metadata files help manage translation from the snapshot version to the timestamp value.
元数据文件有助于管理从快照版本到时间戳值的转换。
Every time we deploy the project under the same snapshot version, Maven will generate the version containing the new timestamp value and the new build number.
每次我们在同一快照版本下部署项目,Maven都会生成包含新时间戳值和新构建号的版本。
3.2. Download an Artifact
3.2.下载一个人工制品
Before downloading the snapshot artifact, Maven downloads its associated maven-metadata.xml file. That way, Maven can check if there’s a newer version, based on the timestamp value and build number.
在下载快照工件之前,Maven会下载其相关的maven-metadata.xml文件。这样一来,Maven就能根据时间戳值和构建号检查是否有更新的版本。
The retrieval of such an artifact can still use the SNAPSHOT version.
对这种工件的检索仍然可以使用SNAPSHOT版本。。
To download the artifact from the repository, firstly, we’d need to define a dependencies repository:
要从资源库下载工件,首先,我们需要定义一个依赖性资源库。
<repositories>
<repository>
<id>nexus</id>
<name>nexus-snapshot</name>
<url>http://localhost:8081/repository/maven-snapshots/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
Snapshot versions are not enabled by default. We need to enable them manually:
快照版本默认不启用。我们需要手动启用它们。
<snapshots>
<enabled>true</enabled>
</snapshots>
By enabling snapshots, we can define how often we’d like to check for a newer version of the SNAPSHOT artifacts. However, the default update policy is set to once per day. We can override this behavior by setting a different update policy:
通过启用快照,我们可以定义多长时间检查一次较新版本的SNAPSHOT工件。然而,默认的更新策略被设置为每天一次。我们可以通过设置一个不同的更新策略来覆盖这一行为。
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
There are four different values we can place inside the updatePolicy element:
我们可以在updatePolicy元素中放置四个不同的值。
- always — check for a newer version every time
- daily (default value) — check for a newer version once a day
- interval:mm — check for a newer version based on an interval set in minutes
- never — never try to get a newer version (compared to the one we already have locally)
Additionally, instead of defining the updatePolicy, we can force an update of all snapshot artifacts by passing the -U argument in the command:
此外,我们可以通过在命令中传递-U参数来强制更新所有快照工件,而不是定义updatePolicy。
mvn install -U
Furthermore, the dependency will not be re-downloaded if it has already been downloaded and the checksum is the same as the one we already have in our local repository.
此外,如果依赖关系已经被下载,并且校验和与我们在本地仓库中已有的校验和相同,则不会被重新下载。
Next, we can add a snapshot version of an artifact to our project:
接下来,我们可以在我们的项目中添加一个工件的快照版本。
<dependencies>
<dependency>
<groupId>com.baeldung</groupId>
<artifactId>maven-snapshot-repository</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
Using a snapshot version during the development phase can prevent having multiple versions of the artifact. We can use the same SNAPSHOT version whose build will contain the snapshot of our code at a given time.
在开发阶段使用快照版本可以防止出现多个版本的工件。我们可以使用同一个SNAPSHOT版本,其构建将包含我们在特定时间的代码的快照。
4. Release Repository
4.发布库
The release repository contains the final versions (releases) of artifacts. Simply put, a release artifact stands for an artifact whose content should not be modified.
发布库包含工件的最终版本(发布)。简单地说,一个发布的工件代表一个工件,其内容不应该被修改。
Release repositories are enabled by default for all repositories defined in our settings.xml or pom.xml files.
对于我们的settings.xml或pom.xml文件中定义的所有存储库,发布存储库默认为启用。
4.1. Deploy an Artifact
4.1.部署一个工件
Now, let’s deploy the project in the local Nexus repository. Let’s assume we’ve finished with the development and are ready to release the project:
现在,让我们在本地Nexus资源库中部署该项目。让我们假设我们已经完成了开发并准备发布项目。
<groupId>com.baeldung</groupId>
<artifactId>maven-release-repository</artifactId>
<version>1.0.0</version>
Let’s define the release repository inside the distribution manager:
让我们在发布管理器中定义发布库。
<distributionManagement>
<repository>
<id>nexus</id>
<name>nexus-release</name>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus</id>
<name>nexus-snapshot</name>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
Once we remove the word SNAPSHOT from the project version, the release repository will be chosen automatically instead of the snapshot repository during the deployment.
一旦我们从项目版本中移除SNAPSHOT字样,在部署过程中就会自动选择发行库而不是快照库。
Furthermore, if we want to redeploy the artifact under the same version, we may get an error: “Repository does not allow updating assets”. Once we deploy the released artifact version, we cannot change its content. Therefore, to resolve the problem, we’d need to simply release the next version.
此外,如果我们想在同一版本下重新部署工件,我们可能会得到一个错误。”存储库不允许更新资产“。一旦我们部署了已发布的工件版本,我们就不能改变其内容。因此,为了解决这个问题,我们需要简单地发布下一个版本。
4.2. Download an Artifact
4.2.下载一个人工制品
Maven defaults to looking for components from the Maven Central Repository. This repository uses a release version policy by default.
Maven默认从Maven中央仓库寻找组件。该资源库默认使用发布版本策略。
Release repositories will only resolve released artifacts. In other words, it should contain only published artifact versions whose content should not change in the future.
发布库将只解决已发布的工件。换句话说,它应该只包含已发布的工件版本,其内容在未来不应改变。
If we want to download the released artifact, we’d need to define the repository:
如果我们想下载已发布的工件,我们就需要定义存储库。
<repository>
<id>nexus</id>
<name>nexus-release</name>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
Finally, let’s simply add the released version to our project:
最后,让我们简单地将发布的版本添加到我们的项目中。
<dependencies>
<dependency>
<groupId>com.baeldung</groupId>
<artifactId>maven-release-repository</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
5. Conclusion
5.总结
In this tutorial, we learned the differences between Maven snapshot and release repositories. To sum up, we should use the snapshot repositories for projects that are still under development and release repositories for projects that are ready for production. As always, the source code for the examples is available over on GitHub.
在本教程中,我们了解了Maven快照库和发布库的区别。总而言之,我们应该将快照仓库用于仍在开发中的项目,而将发布仓库用于准备投入生产的项目。一如既往,这些例子的源代码可在GitHub上找到。