如何告诉 Maven 使用最新版本的依赖项? [英] How do I tell Maven to use the latest version of a dependency?

查看:37
本文介绍了如何告诉 Maven 使用最新版本的依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Maven 中,依赖项通常是这样设置的:

In Maven, dependencies are usually set up like this:

<dependency>
  <groupId>wonderful-inc</groupId>
  <artifactId>dream-library</artifactId>
  <version>1.2.3</version>
</dependency>

现在,如果您正在使用频繁发布的库,请不断更新 <version>标签可能有点烦人.有什么方法可以告诉 Maven 始终使用最新的可用版本(来自存储库)?

Now, if you are working with libraries that have frequent releases, constantly updating the <version> tag can be somewhat annoying. Is there any way to tell Maven to always use the latest available version (from the repository)?

推荐答案

注意:

提到的 LATESTRELEASE 元版本 已在 Maven 3 中因为插件依赖为了可重现的构建"而被删除,超过6年前.(对于常规依赖项,它们仍然可以正常工作.)有关插件依赖项,请参阅此Maven 3 兼容解决方案.

The mentioned LATEST and RELEASE metaversions have been dropped for plugin dependencies in Maven 3 "for the sake of reproducible builds", over 6 years ago. (They still work perfectly fine for regular dependencies.) For plugin dependencies please refer to this Maven 3 compliant solution.

如果您总是想使用最新版本,Maven 有两个关键字可以用作版本范围的替代.您应该谨慎使用这些选项,因为您不再能够控制正在使用的插件/依赖项.

If you always want to use the newest version, Maven has two keywords you can use as an alternative to version ranges. You should use these options with care as you are no longer in control of the plugins/dependencies you are using.

当你依赖一个插件或依赖项时,你可以使用 LATEST 或 RELEASE 的版本值.LATEST 是指特定工件的最新发布或快照版本,即特定存储库中最近部署的工件.RELEASE 是指存储库中的最后一个非快照版本.通常,设计依赖于工件的非特定版本的软件并不是最佳实践.如果您正在开发软件,您可能希望使用 RELEASE 或 LATEST 为方便起见,这样您就不必在发布第三方库的新版本时更新版本号.当您发布软件时,您应该始终确保您的项目依赖于特定版本,以减少您的构建或项目受到不受您控制的软件版本影响的机会.如果有的话,请谨慎使用 LATEST 和 RELEASE.

When you depend on a plugin or a dependency, you can use the a version value of LATEST or RELEASE. LATEST refers to the latest released or snapshot version of a particular artifact, the most recently deployed artifact in a particular repository. RELEASE refers to the last non-snapshot release in the repository. In general, it is not a best practice to design software which depends on a non-specific version of an artifact. If you are developing software, you might want to use RELEASE or LATEST as a convenience so that you don't have to update version numbers when a new release of a third-party library is released. When you release software, you should always make sure that your project depends on specific versions to reduce the chances of your build or your project being affected by a software release not under your control. Use LATEST and RELEASE with caution, if at all.

参见 Maven 书籍的 POM 语法部分 了解更多详细信息.或者在 Dependency Version Ranges 上查看此文档,其中:

See the POM Syntax section of the Maven book for more details. Or see this doc on Dependency Version Ranges, where:

  • 方括号 ( [ & ] ) 表示关闭";(含).
  • 括号 ( ( & ) ) 表示打开";(独家).
  • A square bracket ( [ & ] ) means "closed" (inclusive).
  • A parenthesis ( ( & ) ) means "open" (exclusive).

这是一个说明各种选项的示例.在 Maven 存储库中,com.foo:my-foo 具有以下元数据:

Here's an example illustrating the various options. In the Maven repository, com.foo:my-foo has the following metadata:

<?xml version="1.0" encoding="UTF-8"?><metadata>
  <groupId>com.foo</groupId>
  <artifactId>my-foo</artifactId>
  <version>2.0.0</version>
  <versioning>
    <release>1.1.1</release>
    <versions>
      <version>1.0</version>
      <version>1.0.1</version>
      <version>1.1</version>
      <version>1.1.1</version>
      <version>2.0.0</version>
    </versions>
    <lastUpdated>20090722140000</lastUpdated>
  </versioning>
</metadata>

如果需要依赖该工件,您有以下选项(其他 版本范围当然可以指定,这里只显示相关的):

If a dependency on that artifact is required, you have the following options (other version ranges can be specified of course, just showing the relevant ones here):

声明一个确切的版本(将始终解析为 1.0.1):

Declare an exact version (will always resolve to 1.0.1):

<version>[1.0.1]</version>

声明一个显式版本(除非发生冲突,否则将始终解析为 1.0.1,此时 Maven 将选择匹配的版本):

Declare an explicit version (will always resolve to 1.0.1 unless a collision occurs, when Maven will select a matching version):

<version>1.0.1</version>

声明所有 1.x 的版本范围(目前将解析为 1.1.1):

Declare a version range for all 1.x (will currently resolve to 1.1.1):

<version>[1.0.0,2.0.0)</version>

声明一个开放式版本范围(将解析为 2.0.0):

Declare an open-ended version range (will resolve to 2.0.0):

<version>[1.0.0,)</version>

将版本声明为 LATEST(将解析为 2.0.0)(从 maven 3.x 中删除)

Declare the version as LATEST (will resolve to 2.0.0) (removed from maven 3.x)

<version>LATEST</version>

将版本声明为 RELEASE(将解析为 1.1.1)(从 maven 3.x 中删除):

Declare the version as RELEASE (will resolve to 1.1.1) (removed from maven 3.x):

<version>RELEASE</version>

请注意,默认情况下,您自己的部署将更新最新"版本.Maven 元数据中的条目,但要更新发布"条目,您需要激活发布配置文件";来自 Maven 超级 POM.您可以使用-Prelease-profile"来做到这一点.或-DperformRelease=true"

Note that by default your own deployments will update the "latest" entry in the Maven metadata, but to update the "release" entry, you need to activate the "release-profile" from the Maven super POM. You can do this with either "-Prelease-profile" or "-DperformRelease=true"

值得强调的是,任何允许 Maven 选择依赖项版本(LATEST、RELEASE 和版本范围)的方法都可以让您对构建时间问题持开放态度,因为以后的版本可能有不同的行为(例如,依赖项插件以前具有将默认值从 true 切换为 false,结果令人困惑).

It's worth emphasising that any approach that allows Maven to pick the dependency versions (LATEST, RELEASE, and version ranges) can leave you open to build time issues, as later versions can have different behaviour (for example the dependency plugin has previously switched a default value from true to false, with confusing results).

因此,在发行版中定义确切的版本通常是一个好主意.作为 蒂姆的answer 指出,maven-versions-plugin 是一个方便的工具用于更新依赖版本,特别是 versions:use-latest-版本versions:use-latest-releases 目标.

It is therefore generally a good idea to define exact versions in releases. As Tim's answer points out, the maven-versions-plugin is a handy tool for updating dependency versions, particularly the versions:use-latest-versions and versions:use-latest-releases goals.

这篇关于如何告诉 Maven 使用最新版本的依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆