在“test”阶段开始时运行Maven的“dependency:tree”命令

11
我需要在“test”阶段开始时从Maven获取“dependency:tree”目标输出,以帮助调试一个问题,我需要知道正在使用的每个版本。在Ant中很容易,我已经查看了Maven文档和这里的许多答案,但仍然无法弄清楚,肯定不是那么难吧?

你是说你想让 maven-dependency-plugintest 阶段运行 tree 目标吗? - maba
3个回答

22

这将输出测试依赖树:

mvn test dependency:tree -DskipTests=true

8
如果你想确保在test阶段的开始时运行dependency:tree,那么你需要将原始的surefire:test目标移动到后面执行dependency:tree。为此,您需要按照应该运行的插件顺序来放置插件。
下面是一个完整的pom.xml示例,它在maven-surefire-plugin之前添加了maven-dependency-plugin。原始的default-test已被禁用,并添加了一个新的custom-test,这个测试将在dependency-tree执行后运行。
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.stackoverflow</groupId>
    <artifactId>Q12687743</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>${project.artifactId}-${project.version}</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.5.1</version>
                <executions>
                    <execution>
                        <id>dependency-tree</id>
                        <phase>test</phase>
                        <goals>
                            <goal>tree</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.7.2</version>
                <executions>
                    <execution>
                        <id>default-test</id>
                        <!-- Using phase none will disable the original default-test execution -->
                        <phase>none</phase>
                    </execution>
                    <execution>
                        <id>custom-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

有点尴尬,但这是禁用执行的方法。


5

在您的项目POM文件中声明如下:

 <plugin>
   <artifactId>maven-dependency-plugin</artifactId>
   <version>2.5.1</version>
   <executions>
     <execution>
       <phase>test-compile</phase>
       <goals>
         <goal>tree</goal>
       </goals>
     </execution>
   </executions>
 </plugin>

您可以采用这种模式来在特定的构建阶段触发任何插件。请参见http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Plugins
此外,还可以参见http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference获取构建阶段列表。正如maba所指出的那样,您需要仔细选择阶段,以确保在正确的时间执行tree目标。

这将在实际测试之后运行dependency:tree。 OP表示他希望它在测试阶段开始时运行。 - maba
1
好的建议。相反,他可以绑定到test-compile甚至是compile。希望理解将插件绑定到阶段的一般模式足以解决问题。(编辑了我的答案)。 - Duncan Jones
展示如何将“dependency:tree”添加到“test”阶段,加1分。我将添加另一个答案,向您展示如何在“test”阶段开始时运行“dependency:tree”。 - maba
非常感谢你们两位提供的全面而迅速的答案。我只能选择一个“正确”的答案,所以我选择了另一个,因为它稍微更准确一些,但两个答案都很有用,我非常感激。 - Ed Randall

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接