Maven 依赖范围未按预期工作

发布于 2024-09-26 15:46:53 字数 2081 浏览 1 评论 0原文

Maven 2.2.1 声称支持版本范围(参见例如 http://www.sonatype.com/books/mvnref-book/reference/pom-relationships-sect-project-dependency.html#pom-relationships-sect-version -ranges

我尝试从全新的maven安装以下pom:

<project>

  <modelVersion>4.0.0</modelVersion>
  <artifactId>rangetest</artifactId>
  <groupId>my.group</groupId>
  <version>1.0</version>
  <packaging>jar</packaging>

  <description>test project containing one dependency, only</description>
  <dependencies>
   <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8</version>
    <scope>test</scope>
   </dependency>
  </dependencies>
 </project>

依赖项应该解析为junit 4.8.2,对吧? 但相反,版本 4.8 已得到解决:

C:\Users>mvn dependency:tree
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - my.group:rangetest:jar:1.0
[INFO]    task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] my.group:rangetest:jar:1.0
[INFO] \- junit:junit:jar:4.8:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Thu Oct 07 14:30:40 CEST 2010
[INFO] Final Memory: 9M/23M
[INFO] ------------------------------------------------------------------------

您可能认为这是 Junit 的问题,因为 4.8 是现有版本,但事实并非如此。在我的项目中,我部署了从 1.0.0 到 1.0.15 的版本(没有版本 1.0!),但是 mvn dependency:tree 抱怨缺少版本 1.0 的工件。

Maven 2.2.1 claims to support version ranges (see e.g. http://www.sonatype.com/books/mvnref-book/reference/pom-relationships-sect-project-dependencies.html#pom-relationships-sect-version-ranges)

I tried from a brandnew maven installation the following pom:

<project>

  <modelVersion>4.0.0</modelVersion>
  <artifactId>rangetest</artifactId>
  <groupId>my.group</groupId>
  <version>1.0</version>
  <packaging>jar</packaging>

  <description>test project containing one dependency, only</description>
  <dependencies>
   <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8</version>
    <scope>test</scope>
   </dependency>
  </dependencies>
 </project>

The dependency should resolve to junit 4.8.2, right?
But instead, the version 4.8 is resolved:

C:\Users>mvn dependency:tree
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - my.group:rangetest:jar:1.0
[INFO]    task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] my.group:rangetest:jar:1.0
[INFO] \- junit:junit:jar:4.8:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Thu Oct 07 14:30:40 CEST 2010
[INFO] Final Memory: 9M/23M
[INFO] ------------------------------------------------------------------------

You might think it's an issue with Junit, as 4.8 is an existing version, but it's not. In my projects, I have versions deployed from 1.0.0 to 1.0.15 (no version 1.0!), but mvn dependency:tree complains about missing artifact of version 1.0.

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

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

发布评论

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

评论(4

如若梦似彩虹 2024-10-03 15:46:53

仅当您实际指定版本范围(如果您有多个 POM)时,这才有效。您使用 4.8 这是一个单一版本号,因此 Maven 尝试直接解析它。版本范围必须以 [( (分别包含和排除)开头。

根据您的情况,请尝试: [4.8,4.9)

应该为您提供具有相同 API 的最高版本(即所有错误修复但没有重大更改)。

本文档展示了 Maven 如何解释版本: https://maven.apache.org/enforcer /enforcer-rules/versionRanges.html

第一行似乎支持你的立场(4.8 表示“至少 4.8”),但有一个问题:Maven 只会选择更高的版本,如果任何人特别要求它。因此,除非您的构建中有更多 POM,并且其中一个 POM 要求 4.8.2,否则 4.8 对于 Maven 来说“足够好”。

将其视为解决冲突的提示。如果 POM A 要求 4.8,B 要求 4.8.2 并且 B 依赖于 A,那么 Maven 必须做出决定。应该失败吗?应该用4.8吗?还是4.8.2?

规则通过选择 4.8.2 解决了此冲突,而不给出警告或错误。

如果 A 要求 [4.8],您会收到错误,因为 Maven 无法默默地将依赖项“升级”到 4.8.2,并且它当然也无法将 4.8.2 降级到 4.8。

That only works if you actually specify a version range or if you have more than one POM. You use 4.8 which is a single version number, so Maven tries to resolve it directly. A version range must start with [ or ( (inclusive and exclusive, respectively).

In your case, try: [4.8,4.9)

That should give you the highest version with the same API (i.e. all bug fixes but no breaking changes).

This document shows how Maven interprets versions: https://maven.apache.org/enforcer/enforcer-rules/versionRanges.html

The first line seems to support your position (4.8 means "at least 4.8") but there is a catch: Maven will only select a higher version if anyone specifically asks for it. So unless you have more POMs in your build and one of those asks for 4.8.2, 4.8 is "good enough" for Maven.

Think of it as a hint for conflict resolution. If POM A asks for 4.8 and B asks for 4.8.2 and B depends on A, then Maven has to make a decision. Should it fail? Should it use 4.8? Or 4.8.2?

The rules resolve this conflict by picking 4.8.2 without giving a warning or an error.

If A asks for [4.8], you'd get an error instead since Maven can't silently "upgrade" the dependency to 4.8.2 and it certainly can't downgrade 4.8.2 to 4.8.

日久见人心 2024-10-03 15:46:53

如果您想使用版本范围,请按照其他人指出的那样指定版本范围。目前,你还没有。

但我真正的建议是根本不使用版本范围,版本范围对于构建可重复性来说是一个坏主意,您最不想要的就是构建由于未知原因突然开始失败。只是不要这样做,它们是一种不好的做法(这可能就是版本范围不再被记录的原因)。

If you want to use version ranges, specify a version range as others pointed out. Currently, you're not.

But my real advice would be to not use version ranges at all, version ranges are a bad idea for build reproducibility and the last thing you want is a build that suddenly starts to fail because of an unknown reason. Just don't, they are a bad practice (which is probably why version ranges aren't documented anymore).

べ映画 2024-10-03 15:46:53

您的 version 标记中似乎没有范围限定符。也许您打算使用以下命令来要求版本 4.8 或更高版本:

<version>[4.8,)</version>

There doesn't seem to be a range qualifier in your version tag. Maybe you meant to use the following to require version 4.8 or later:

<version>[4.8,)</version>
逐鹿 2024-10-03 15:46:53

就我而言,一切正常,但突然出现此错误

No versions are present in the repository for the artifact with a range [x,y)

,我会转到 .m2/repository 文件夹内的库位置并删除整个文件夹

,如果您删除了特定版本的文件夹 该库不起作用,应该删除该库的所有版本的文件夹

,还有一件事,如果该库是可传递的,您可能需要删除依赖路径中的所有库

希望这可以帮助您

In my case, everything was working but out of the sudden I got this error

No versions are present in the repository for the artifact with a range [x,y)

I went to the library location inside .m2/repository folder and deleted the whole folder,

if you removed folder of a specific version of the lib it won't work, should remove folders of all versions of that lib

one more thing, if the lib is transitive you might need to remove all libraries in the dependency path

hope this helps you

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