Maven尝试在“validate”阶段下载多模块依赖。

huangapple go评论70阅读模式
英文:

Maven tries download multimodule dependency on `validate` phase

问题

从构建流水线中,我想要运行validate阶段,用于诸如enforcer插件之类的内容。然而,在多模块项目中,它对于尝试从项目内部的存储库下载依赖项的情况下无法正常工作。然而,compile阶段则不会这样做,但对我来说,这不是一个选项,因为速度太慢。

pom.xml:

<module>lib</module>
<module>app</module>

lib/pom.xml

<version>1.2.3</version>

app/pom.xml

 <dependency>
   <artifactId>lib</artifactId>
   <version>1.2.3</version>
 </dependency>

所以,如果我执行mvn compile,它能够正常工作。

但是,如果我执行mvn validate,它会在验证app模块时失败,因为它会尝试从Maven仓库下载lib-1.2.3。出于某种原因,它现在无法看到lib是相邻的依赖关系。为什么会这样?

我创建了一个小型仓库:https://github.com/kan-izh/so63963768

mvn compile

[INFO] --- maven-enforcer-plugin:3.0.0-M3:enforce (enforce-no-snapshots) @ app ---
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ app ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Workspace\repo\so63963768\app\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ app ---
[INFO] No sources to compile
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] parent ............................................ SUCCESS [1.612s]
[INFO] lib ............................................... SUCCESS [1.224s]
[INFO] app ............................................... SUCCESS [0.056s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

mvn validate

[INFO] --- maven-enforcer-plugin:3.0.0-M3:enforce (enforce-no-snapshots) @ app ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] parent ............................................ SUCCESS [0.979s]
[INFO] lib ............................................... SUCCESS [0.015s]
[INFO] app ............................................... FAILURE [0.020s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
英文:

From a build pipeline I want to run validate phase for things like the enforcer plugin. However it does not work for a multimodule project as it tries to download dependencies from repository which are inside the project. However, compile phase does not do that, but for me it is not an option as it is too slow.

pom.xml:

&lt;module&gt;lib&lt;/module&gt;
&lt;module&gt;app&lt;/module&gt;

lib/pom.xml

 &lt;version&gt;1.2.3&lt;/version&gt;

app/pom.xml

 &lt;dependency&gt;
   &lt;artifactId&gt;lib&lt;/artifactId&gt;
   &lt;version&gt;1.2.3&lt;/version&gt;
 &lt;/dependency&gt;

So, if I do mvn compile it works fine.

But if I do mvn validate it fails validating app module as it tries to download lib-1.2.3 from maven repo. For some reason it now could not see that the lib is a neighbour dependency. Why?

I have created a small repo: https://github.com/kan-izh/so63963768

mvn compile

[INFO] --- maven-enforcer-plugin:3.0.0-M3:enforce (enforce-no-snapshots) @ app ---
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ app ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Workspace\repo\so63963768\app\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ app ---
[INFO] No sources to compile
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] parent ............................................ SUCCESS [1.612s]
[INFO] lib ............................................... SUCCESS [1.224s]
[INFO] app ............................................... SUCCESS [0.056s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

mvn validate

[INFO] --- maven-enforcer-plugin:3.0.0-M3:enforce (enforce-no-snapshots) @ app ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] parent ............................................ SUCCESS [0.979s]
[INFO] lib ............................................... SUCCESS [0.015s]
[INFO] app ............................................... FAILURE [0.020s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.180s
[INFO] Finished at: Wed Sep 23 11:27:38 BST 2020
[INFO] Final Memory: 7M/34M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce (enforce-no-snapshots) on project app: Execution
 enforce-no-snapshots of goal org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce failed: org.apache.maven.shared.dependency.graph.
DependencyGraphBuilderException: Could not resolve following dependencies: [com.so.q63963768:lib:jar:1.2.3 (compile)]: Could not resolve depend
encies for project com.so.q63963768:app:jar:1.2.3: Failure to find com.so.q63963768:lib:jar:1.2.3 in http://xxxxxxxxxxxxx.xx.xxxxxxxxxxxxxxxxx.
com:8081/repository/maven-public/ was cached in the local repository, resolution will not be reattempted until the update interval of nexus has
 elapsed or updates are forced -&gt; [Help 1]

答案1

得分: 2

这是一个很好的问题,它展示了Maven在处理依赖项时存在的一个小缺陷。您需要知道,对于每个插件目标,您可以定义是否应解析依赖关系,以及解析哪个范围的依赖关系。(如果仅有pom文件足够,还是您也需要这些构件存在之间存在一些小差异。)

compiler:compile需要在编译期间所需的依赖项,compiler:testCompile需要在测试期间所需的依赖项。
对于强制执行目标,情况比较棘手:目标本身不需要已解析的依赖项,大多数规则(例如requireJavaVersion或requireMavenVersion)也不需要,但某些规则确实需要,就像您尝试强制执行的规则一样。
理想情况下,规则可以定义是否需要已解析的依赖项,但目前API不支持这一点。

因此,您有几个解决方案:始终使用compile运行,或者如果它需要构件,则将执行块绑定到编译阶段。

英文:

This is a good question, and it shows a small flaw in how Maven handles dependencies.
You need to know that for every plugin-goal you can define if dependencies should be resolved, and for which scope. (and there's a small difference if having only the poms is enough, or that you also need the artifacts)
compiler:compile requires the dependencies that are required during compile, compiler:testCompile requires the dependencies that are required during test.
For the enforce goal it is tricky: the goal itself doesn't require to have resolved dependencies, nor do most rules( like requireJavaVersion or requireMavenVersion), but some rules do, like the one you try to enforce.
Ideally rules can define if they need to have resolved dependencies, but right now the API doesn't support that.

So you have a couple of solutions: always run with compile, or have an execution-block bound to the compile-pahse if it requires artifacts.

huangapple
  • 本文由 发表于 2020年9月19日 07:05:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63963768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定