我对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的模型可以在这里找到)

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

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

这就是存储库的快照的样子,在本例中没有启用快照,这意味着这里引用的存储库是稳定的,不需要更新。

<project>
    ...
    <repositories>
        <repository>
            <id>lds-main</id>
            <name>LDS Main Repo</name>
            <url>http://code.lds.org/nexus/content/groups/main-repo</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

另一种情况是:

<snapshots>
        <enabled>true</enabled>
</snapshots>

这意味着Maven将为这个存储库查找更新。您还可以使用标记指定更新间隔。

Maven中的快照版本是尚未发布的版本。

其思想是,在1.0发行版(或任何其他发行版)完成之前,存在1.0- snapshot。这个版本可能会变成1.0。它基本上是“正在开发的1.0版本”。这可能接近于真正的1.0发行版,或者相当远(例如,就在0.9发行版之后)。

“真实”版本和快照版本之间的区别是快照可能会得到更新。这意味着今天下载1.0-SNAPSHOT的文件可能与昨天或明天下载的文件不同。

通常,快照依赖关系应该只存在于开发过程中,没有发布版本(即没有非快照版本)应该依赖于快照版本。

A Maven SNAPSHOT is an artifact created by a Maven build and pretends to help developers in the software development cycle. A SNAPSHOT is an artifact (or project build result ) that is not pretended to be used anywhere, it's only a temporarily .jar, ear, ... created to test the build process or to test new requirements that are not yet ready to go to a production environment. After you are happy with the SNAPSHOT artifact quality, you can create a RELEASE artifact that can be used by other projects or can be deployed itself.

在你的项目中,你可以使用Maven的pom.xml文件中的version元素定义一个SNAPSHOT:

<groupId>example.project.maven</groupId>
<artifactId>MavenEclipseExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<description>Maven pom example</description>

如果你想更好地理解Maven,你也可以看看这些文章:

https://connected2know.com/programming/menu-maven-articles/