【问题标题】:How to find the exact version of a maven dependency programmatically when version is inherited?继承版本时,如何以编程方式查找 Maven 依赖项的确切版本?
【发布时间】:2014-03-08 06:21:48
【问题描述】:

我有一个 maven 项目,其中许多子 pom 的依赖项版本都列在父 pom 的 <dependencyManagement> 标记下。这个 pom 树可以有任何深度。

我的问题是,当给定 pom.xml 时,如何使用 java 解决其依赖项的确切(继承)版本?

我们在 pom.xml 文件上运行 mvn dependency:resolve -DincludeTransitive=false 时给出的输出结果

例如,假设父 pom 在 <dependencyManagement> 下定义了以下依赖项

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>group-a</groupId>
        <artifactId>artifact-a</artifactId>
        <version>1.0</version>
      </dependency>
      <dependency>
        <groupId>group-a</groupId>
        <artifactId>artifact-b</artifactId>
        <version>1.0</version>
        <type>bar</type>
        <scope>runtime</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

子 pom.xml 有以下依赖

<dependencies>
    <dependency>
      <groupId>group-a</groupId>
      <artifactId>artifact-a</artifactId>
    </dependency>
    <dependency>
      <groupId>group-a</groupId>
      <artifactId>artifact-b</artifactId>
    </dependency>
  </dependencies>

如何使用 Java 以编程方式检索 group-a:artifact-agroup-b:artifact-b 的版本?

谢谢。

【问题讨论】:

    标签: java maven pom.xml maven-dependency-plugin parent-pom


    【解决方案1】:

    尝试运行

    mvn dependency:tree -Doutput=/path/to/file
    

    mvn dependency:tree -DoutputFile=/path/to/file
    

    来自 maven 文档:http://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html

    【讨论】:

    • 我实际上尝试过这个,但使用 mvn 依赖项:resolve -DincludeTransitive=false -outputFile=out.txt。但是有没有办法避免读取中间文件并直接从它自己的 Java 代码中解析版本
    • 不知道你用的是什么工具,不过我以前也用过eclipse的maven插件。如果您打开 pom.xml,您可以看到完全解析的版本,包括它继承的所有依赖项。此处有安装说明:theopentutorials.com/tutorials/eclipse/… 如果您想自己放入,可以从此处下载:eclipse.org/m2e/download
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    • 2023-04-01
    • 1970-01-01
    • 2017-03-21
    • 2020-07-26
    • 2019-01-24
    • 1970-01-01
    相关资源
    最近更新 更多