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

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

问题描述

在Maven中,依赖关系通常设置如下:

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

现在,如果您正在使用频繁发布的库,请不断更新< version>标签可能有点恼人。有没有办法告诉Maven总是使用最新的可用版本(从存储库)?

解决方案

注意: 只要!提到的 LATEST RELEASE metaversions



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


当您依赖于插件或依赖关系,您可以使用版本值LATEST或RELEASE。 LATEST是指特定工件的最新发布或快照版本,特定存储库中最近部署的工件。 RELEASE是指存储库中的最后一个非快照版本。一般而言,设计依赖于非特定版本的工件的软件不是最佳做法。如果您正在开发软件,您可能希望使用RELEASE或LATEST来方便使用,当新版本的第三方库被发布时,您不必更新版本号。当您发布软件时,应始终确保您的项目依赖于特定版本,以减少您的构建或项目受到不受您控制的软件版本的影响。如果有的话请使用LATEST和RELEASE。


请参阅 Maven书籍的POM语法部分,更多细节。或者参阅依赖版本范围上的此文档,其中:




  • 方括号( [& ] )表示已关闭(含)。

  • 圆括号(& )表示打开(排他)。



以下是说明各种选项的示例。在Maven资源库中,com.foo:my-foo具有以下元数据:

  ?xml version =1.0encoding =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>

如果需要对该工件的依赖,则可以使用以下选项(其他版本范围,只显示声明一个确切的版本(将始终解析为1.0.1):

 < version> [1.0.1]< / version> 

声明一个显式版本(除非发生碰撞,否则总是会解析为1.0.1,当Maven会选择一个匹配的版本):

 < version> 1.0.1< / version> 

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



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

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

 < version> [1.0.0,)< / version> 

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

 < version> LATEST< / version> 

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

 < version> RELEASE< / version> 

请注意,默认情况下,您自己的部署将更新Maven元数据中的最新条目,但是更新发布条目,您需要从 Maven超级POM 。您可以使用-Prelease-profile或-DperformRelease = true来执行此操作






值得强调的是方法允许Maven选择依赖版本(LATEST,RELEASE和版本范围)可以让您开放构建时间问题,因为更高版本可以具有不同的行为(例如,依赖插件以前将默认值从true切换到false ,令人困惑的结果)。



因此,在发布中定义确切版本通常是个好主意。作为蒂姆答案指出, maven-versions-plugin 是一个方便的更新依赖关系版本的工具,特别是版本:use-最新版本版本:use-最新发布的目标。


In Maven, dependencies are usually set up like this:

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

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

解决方案

NOTE: This answer applies to Maven 2 only! The mentioned LATEST and RELEASE metaversions have been dropped in Maven 3 "for the sake of reproducible builds", over 6 years ago.

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.

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.

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

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

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

<version>[1.0.1]</version>

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>

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

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

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

<version>[1.0.0,)</version>

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

<version>LATEST</version>

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

<version>RELEASE</version>

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"


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

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天全站免登陆