我对Maven快照的含义有点困惑,为什么我们要构建一个?


当前回答

其他三个答案让您对-SNAPSHOT版本有了一个很好的了解。我只是想添加一些关于Maven在发现SNAPSHOT依赖项时的行为的信息。

在构建应用程序时,Maven将在本地存储库中搜索依赖项。如果在那里没有找到稳定版本,它将搜索远程存储库(在settings.xml或pom.xml中定义)以检索此依赖项。然后,它将把它复制到本地存储库中,使其可用于下一个构建。

例如,foo-1.0.jar库被认为是一个稳定版本,如果Maven在本地存储库中找到它,它将在当前构建中使用这个库。

Now, if you need a foo-1.0-SNAPSHOT.jar library, Maven will know that this version is not stable and is subject to changes. That's why Maven will try to find a newer version in the remote repositories, even if a version of this library is found on the local repository. However, this check is made only once per day. That means that if you have a foo-1.0-20110506.110000-1.jar (i.e. this library has been generated on 2011/05/06 at 11:00:00) in your local repository, and if you run the Maven build again the same day, Maven will not check the repositories for a newer version.

Maven为您提供了一种在存储库定义中更改此更新策略的方法:

<repository>
    <id>foo-repository</id>
    <url>...</url>
    <snapshots>
        <enabled>true</enabled>
        <updatePolicy>XXX</updatePolicy>
    </snapshots>
</repository>

其中XXX可以是:

always: Maven将在每次构建时检查更新的版本; 每日,默认值; interval:XXX:单位为分钟的间隔(XXX)。 never: Maven永远不会尝试检索其他版本。它只在本地不存在时才会这样做。通过配置,SNAPSHOT版本将作为稳定库处理。

(settings.xml的模型可以在这里找到)

其他回答

我想谈谈术语。其他答案很好地解释了什么是Maven上下文中的“快照”版本。但是,非快照版本是否应该被称为“发布”版本呢?

在“发布”版本的语义版本控制思想之间存在一些紧张关系,“发布”版本似乎是任何没有-SNAPSHOT这样的限定符,但也没有-beta.4这样的限定符的版本;以及Maven的“发布”版本的想法,它似乎只包括没有-SNAPSHOT。

In other words, there is a semantic ambiguity of whether "release" means "we can release it to Maven Central" or "the software is in its final release to the public". We could consider -beta.4 to be a "release" version if we release it to the public, but it's not a "final release". Semantic versioning clearly says that something like -beta.4 is a "pre-release" version, so it wouldn't make sense for it to be called a "release" version, even without -SNAPSHOT. In fact by definition even -rc.5 is a release candidate, not an actual release, even though we may allow public access for testing.

因此,尽管Maven如此,在我看来,只将“发布”版本称为根本没有任何限定符(甚至没有-beta.4)的版本似乎更合适。对于Maven非快照版本来说,更好的名称可能是“稳定”版本(灵感来自另一个答案)。这样我们就有:

1.2.3-beta。4-SNAPSHOT:预发布版本的快照版本。 1.2.3-SNAPSHOT:发布版本的快照版本。 1.2.3-beta。4:预发布版本的稳定版本。 1.2.3:发布版本(显然是一个稳定的非快照版本)。

Maven版本可以包含一个字符串文字“SNAPSHOT”,以表示项目目前正在积极开发中。

例如,如果您的项目有一个“1.0-SNAPSHOT”版本,并且您将这个项目的工件部署到Maven存储库, 如果您愿意,Maven会将这个版本扩展到“1.0-20080207-230803-1” 在UTC 2008年2月7日11:08 PM部署一个版本。换句话说,当你 部署一个快照,您不是在发布一个软件组件;你是 在特定时间释放组件的快照。

因此,快照版本主要用于正在积极开发的项目。 如果您的项目依赖于正在积极开发的软件组件, 您可以依赖快照发布,Maven将定期尝试 运行构建时从存储库下载最新快照。类似地,如果 您的系统的下一个发行版将有一个“1.8”版本,您的项目也会如此 “1.8-SNAPSHOT”版本,直到正式发布。

例如,下面的依赖项总是会下载最新的1.8版本的spring开发JAR:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>1.8-SNAPSHOT”</version>
    </dependency>

Maven

maven发布过程的一个例子

其他三个答案让您对-SNAPSHOT版本有了一个很好的了解。我只是想添加一些关于Maven在发现SNAPSHOT依赖项时的行为的信息。

在构建应用程序时,Maven将在本地存储库中搜索依赖项。如果在那里没有找到稳定版本,它将搜索远程存储库(在settings.xml或pom.xml中定义)以检索此依赖项。然后,它将把它复制到本地存储库中,使其可用于下一个构建。

例如,foo-1.0.jar库被认为是一个稳定版本,如果Maven在本地存储库中找到它,它将在当前构建中使用这个库。

Now, if you need a foo-1.0-SNAPSHOT.jar library, Maven will know that this version is not stable and is subject to changes. That's why Maven will try to find a newer version in the remote repositories, even if a version of this library is found on the local repository. However, this check is made only once per day. That means that if you have a foo-1.0-20110506.110000-1.jar (i.e. this library has been generated on 2011/05/06 at 11:00:00) in your local repository, and if you run the Maven build again the same day, Maven will not check the repositories for a newer version.

Maven为您提供了一种在存储库定义中更改此更新策略的方法:

<repository>
    <id>foo-repository</id>
    <url>...</url>
    <snapshots>
        <enabled>true</enabled>
        <updatePolicy>XXX</updatePolicy>
    </snapshots>
</repository>

其中XXX可以是:

always: Maven将在每次构建时检查更新的版本; 每日,默认值; interval:XXX:单位为分钟的间隔(XXX)。 never: Maven永远不会尝试检索其他版本。它只在本地不存在时才会这样做。通过配置,SNAPSHOT版本将作为稳定库处理。

(settings.xml的模型可以在这里找到)

understanding the context of SDLC will help understand the difference between snapshot and the release. During the dev process developers all contribute their features to a baseline branch. At some point the lead thinks enough features have accumulated then he will cut a release branch from the baseline branch. Any builds prior to this time point are snapshots. Builds post to this point are releases. Be noted, release builds could change too before going to production if any defect spot during the release testing.

通常在maven中,我们有两种类型的构建 1)建立快照 2)制定版本发布

快照构建:快照是特殊版本,它指示当前部署副本,不像常规版本,maven检查远程存储库中的每个构建的版本 因此快照构建只是开发构建。 发布版本:发布意味着在版本中删除快照,这些是常规的构建版本。