Maven 快照到底是什么以及我们为什么需要它?

发布于 2024-11-05 10:42:50 字数 57 浏览 1 评论 0原文

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

I am a bit confused about the meaning of a Maven SNAPSHOT and why we build one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

肥爪爪 2024-11-12 10:42:51

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

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

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

现在,如果您需要 foo-1.0-SNAPSHOT.jar 库,Maven 会知道该版本不稳定并且可能会发生更改。这就是为什么 Maven 会尝试在远程存储库中查找更新的版本,即使在本地存储库中找到了该库的版本。但是,此检查每天仅进行一次。这意味着,如果您的本地存储库中有一个 foo-1.0-20110506.110000-1.jar (即该库已于 2011/05/06 11:00:00 生成),并且如果当您在同一天再次运行 Maven 构建时,Maven 将不会检查存储库中是否有更新版本。

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

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

其中 XXX 可以是:

  • 始终:Maven 将在每次构建时检查更新版本;
  • 每天,默认值;
  • interval:XXX:以分钟为单位的间隔 (XXX)
  • never:Maven 永远不会尝试检索另一个版本。仅当本地不存在时它才会执行此操作。通过配置,SNAPSHOT 版本将作为稳定库处理。

(settings.xml 的模型可以在此处找到)

The three others answers provide you a good vision of what a -SNAPSHOT version is. I just wanted to add some information regarding the behavior of Maven when it finds a SNAPSHOT dependency.

When you build an application, Maven will search for dependencies in the local repository. If a stable version is not found there, it will search the remote repositories (defined in settings.xml or pom.xml) to retrieve this dependency. Then, it will copy it into the local repository, to make it available for the next builds.

For example, a foo-1.0.jar library is considered as a stable version, and if Maven finds it in the local repository, it will use this one for the current build.

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 provides you a way to change this update policy in your repository definition:

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

where XXX can be:

  • always: Maven will check for a newer version on every build;
  • daily, the default value;
  • interval:XXX: an interval in minutes (XXX)
  • never: Maven will never try to retrieve another version. It will do that only if it doesn't exist locally. With the configuration, SNAPSHOT version will be handled as the stable libraries.

(model of the settings.xml can be found here)

带刺的爱情 2024-11-12 10:42:51

“快照”术语意味着构建是给定时间的代码的快照。

这通常意味着该版本仍在大力开发中。

当代码准备好并且需要发布它时,您将需要更改 POM 中列出的版本。然后,您将使用“1.0”之类的标签,而不是“SNAPSHOT”。

如需版本控制方面的一些帮助,请查看语义版本控制规范

The "SNAPSHOT" term means that the build is a snapshot of your code at a given time.

It usually means that this version is still under heavy development.

When the code is ready and it is time to release it, you will want to change the version listed in the POM. Then instead of having a "SNAPSHOT" you would use a label like "1.0".

For some help with versioning, check out the Semantic Versioning specification.

寄离 2024-11-12 10:42:51

“发布”是不会更改的版本的最终版本。

“快照”是一个可以被另一个同名构建替换的构建。这意味着构建可以随时更改并且仍在积极开发中。

基于相同代码的不同构建有不同的工件。例如,您可能有一台带调试功能,另一台没有调试功能。一种适用于 Java 5.0,一种适用于 Java 6。一般来说,拥有一个可以完成您所需的一切的构建会更简单。 ;)

A "release" is the final build for a version which does not change.

A "snapshot" is a build which can be replaced by another build which has the same name. It implies that the build could change at any time and is still under active development.

You have different artifacts for different builds based on the same code. E.g. you might have one with debugging and one without. One for Java 5.0 and one for Java 6. In general, it's simpler to have one build which does everything you need. ;)

彩虹直至黑白 2024-11-12 10:42:51

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

例如,如果您的项目的版本为“1.0-SNAPSHOT”,并且您将该项目的工件部署到 Maven 存储库,
如果您要这样做,Maven 会将此版本扩展为“1.0-20080207-230803-1”
于 UTC 时间 2008 年 2 月 7 日晚上 11:08 部署版本。换句话说,当你
部署快照,您不是在发布软件组件;你是
在特定时间发布组件的快照。

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

例如,以下依赖项将始终下载 spring 的最新 1.8 开发 JAR:

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

Maven

maven 发布流程示例

在此处输入图像描述

Maven versions can contain a string literal "SNAPSHOT" to signify that a project is currently under active development.

For example, if your project has a version of “1.0-SNAPSHOT” and you deploy this project’s artifacts to a Maven repository,
Maven would expand this version to “1.0-20080207-230803-1” if you were to
deploy a release at 11:08 PM on February 7th, 2008 UTC. In other words, when you
deploy a snapshot, you are not making a release of a software component; you are
releasing a snapshot of a component at a specific time.

So mainly snapshot versions are used for projects under active development.
If your project depends on a software component that is under active development,
you can depend on a snapshot release, and Maven will periodically attempt
to download the latest snapshot from a repository when you run a build. Similarly, if
the next release of your system is going to have a version “1.8,” your project would
have a “1.8-SNAPSHOT” version until it was formally released.

For example , the following dependency would always download the latest 1.8 development JAR of spring:

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

Maven

An example of maven release process

enter image description here

甜嗑 2024-11-12 10:42:51

我想就术语发表一点看法。其他答案很好地解释了 Maven 上下文中的“快照”版本。但这是否意味着非快照版本就应该被称为“发布”版本呢?

“发布”版本的语义版本控制理念之间存在一些紧张关系,“发布”版本似乎是任何没有限定符(例如 -SNAPSHOT)但也没有限定符(例如 <代码>-beta.4; Maven 的“发布”版本的想法,似乎只包括缺少 -SNAPSHOT

换句话说,“发布”是指“我们可以将其发布到 Maven Central”还是“该软件正在向公众发布最终版本”,这在语义上存在歧义。如果我们向公众发布-beta.4,我们可以将其视为“发布”版本,但它不是“最终版本”。 语义版本控制清楚地表明像-beta.4这样的东西是“预发布”版本,因此即使没有 -SNAPSHOT,将其称为“发布”版本也是没有意义的。事实上,根据定义,即使我们可能允许公共访问进行测试,即使 -rc.5 也是一个候选版本,而不是实际版本。

因此,尽管如此,在我看来,只调用根本没有任何限定符的“发布”版本 1 似乎更合适,甚至没有 -beta.4。也许 Maven 非快照版本的更好名称是“稳定”版本(受到另一个答案的启发)。因此,我们将拥有:

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

I'd like to make a point about terminology. The other answers gave good explanations about what a "snapshot" version is in the context of Maven. But does it follow that a non-snapshot version should be termed a "release" version?

There is some tension between the semantic versioning idea of a "release" version, which would seem to be any version that does not have a qualifier such as -SNAPSHOT but also does not have a qualifier such as -beta.4; and Maven's idea of a "release" version, which only seems to include the absence of -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.

So, Maven notwithstanding, in my opinion, it seems more appropriate only to call a "release" version one that doesn't have any qualifier at all, not even -beta.4. Perhaps a better name for a Maven non-snapshot version would be a "stable" version (inspired by another answer). Thus, we would have:

  • 1.2.3-beta.4-SNAPSHOT: A snapshot version of a pre-release version.
  • 1.2.3-SNAPSHOT: A snapshot version of a release version.
  • 1.2.3-beta.4: A stable version of a pre-release version.
  • 1.2.3: A release version (which is a stable, non-snapshot version, obviously).
咿呀咿呀哟 2024-11-12 10:42:51

通常在 Maven 中我们有两种类型的构建
1)快照构建
2)Release builds

  1. snapshot builds:SNAPSHOT是特殊版本,表明当前部署副本与常规版本不同,maven检查远程存储库中每个构建的版本
    所以快照构建只不过是开发构建。

  2. 发布版本:发布意味着删除构建版本中的快照,这些是常规构建版本。

usually in maven we have two types of builds
1)Snapshot builds
2)Release builds

  1. snapshot builds:SNAPSHOT is the special version that indicate current deployment copy not like a regular version, maven checks the version for every build in the remote repository
    so the snapshot builds are nothing but development builds.

  2. Release builds:Release means removing the SNAPSHOT at the version for the build, these are the regular build versions.

燕归巢 2024-11-12 10:42:51

Maven SNAPSHOT 是由 Maven 构建创建的工件,旨在为软件开发周期中的开发人员提供帮助。
快照是一个工件(或项目构建结果),不打算在任何地方使用,它只是临时的 .jar、ear...,创建用于测试构建过程或测试尚未准备好的新需求到生产环境。
当您对 SNAPSHOT 工件质量感到满意后,您可以创建一个可供其他项目使用或可以自行部署的 RELEASE 工件。

在您的项目中,您可以使用 Maven 的 pom.xml 文件中的版本元素来定义快照:

<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/

A Maven SNAPSHOT is an artifact created by a Maven build and aims to help developers in the software development cycle.
A SNAPSHOT is an artifact (or project build result ) that is not intended 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.

In your project, you can define a SNAPSHOT using the version element in the pom.xml file of Maven:

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

If you want to understand better Maven you can look into these articles too:

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

反目相谮 2024-11-12 10:42:51

这就是存储库的快照的样子,在这种情况下未启用,这意味着此处引用的存储库是稳定的,不需要更新。

<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 将查找此存储库的更新。您还可以使用标签指定更新间隔。

This is how a snapshot looks like for a repository and in this case is not enabled, which means that the repository referred in here is stable and there's no need for updates.

<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>

Another case would be for:

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

which means that Maven will look for updates for this repository. You can also specify an interval for the updates with tag.

无声无音无过去 2024-11-12 10:42:51

只是快照意味着它是不稳定的版本。

当版本包含像 1.0.0 这样的快照时 -SNAPSHOT 意味着它不是稳定版本并查找远程存储库来解决依赖关系

simply snapshot means it is the version which is not stable one.

when version includes snapshot like 1.0.0 -SNAPSHOT means it is not stable version and look for remote repository to resolve dependencies

我ぃ本無心為│何有愛 2024-11-12 10:42:51

快照只是意味着根据您的配置,Maven 将检查特殊依赖项的最新更改。快照不稳定,因为它正在开发中,但如果特殊项目需要进行最新更改,则必须将依赖版本配置为快照版本。这种情况发生在拥有多种产品且这些产品彼此密切相关的大型组织中。

Snapshot simply means depending on your configuration Maven will check latest changes on a special dependency. Snapshot is unstable because it is under development but if on a special project needs to has a latest changes you must configure your dependency version to snapshot version. This scenario occurs in big organizations with multiple products that these products related to each other very closely.

看海 2024-11-12 10:42:51

了解 SDLC 的上下文将有助于理解快照和发布之间的区别。在开发过程中,开发人员都将他们的功能贡献给基线分支。在某个时候,领导认为已经积累了足够的功能,那么他将从基线分支中删除一个发布分支。该时间点之前的任何构建都是快照。到目前为止的构建都是发布版本。请注意,如果发布测试期间出现任何缺陷,发布版本在投入生产之前也可能会发生变化。

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.

许久 2024-11-12 10:42:51

在开发阶段,Maven 快照每天都会寻找更新的更高版本(如果在 Nexus 存储库中可用),并将其下载到本地以进行下一个构建。

您可以在存储库定义中设置四个选项

“始终”,
每日(默认),
间隔,
永远不要,

注意:在生产版本中,我们不应该依赖快照版本。

In development phase Maven snapshots everyday looks for newer higher version if available in nexus repository n download it locally for next build.

Four option you can set in respository defination

Always,
Daily (default),
Interval,
Never,

Note: In production release we should not have dependency on snapshot version.

悲歌长辞 2024-11-12 10:42:50

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

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

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

通常,快照依赖项应该在开发期间存在,并且任何发布版本(即没有非快照)都不应该依赖于快照版本。

A snapshot version in Maven is one that has not been released.

The idea is that before a 1.0 release (or any other release) is done, there exists a 1.0-SNAPSHOT. That version is what might become 1.0. It's basically "1.0 under development". This might be close to a real 1.0 release, or pretty far (right after the 0.9 release, for example).

The difference between a "real" version and a snapshot version is that snapshots might get updates. That means that downloading 1.0-SNAPSHOT today might give a different file than downloading it yesterday or tomorrow.

Usually, snapshot dependencies should only exist during development and no released version (i.e. no non-snapshot) should have a dependency on a snapshot version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文