【问题标题】:Maven run "dependency:tree" at start of "test" phaseMaven 在“测试”阶段开始时运行“依赖:树”
【发布时间】:2012-09-23 03:44:57
【问题描述】:

我需要在“测试”阶段开始时从 Maven 获取“dependency:tree”目标输出,以帮助调试我需要知道正在使用的所有版本的问题。

在 Ant 中这很容易,我查看了这里的 Maven 文档和许多答案,但仍然无法弄清楚,肯定没那么难吗?

【问题讨论】:

  • 您是说您希望maven-dependency-plugintest 阶段运行tree 目标吗?

标签: maven phase


【解决方案1】:

这将输出测试依赖树:

mvn test dependency:tree -DskipTests=true

【讨论】:

    【解决方案2】:

    如果您想确定dependency:tree 正在test 阶段的开始运行,那么您必须将原来的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>
    

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

    【讨论】:

      【解决方案3】:

      在你的项目 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 说他希望它在测试阶段开始时运行。
      • 好点。相反,他可以绑定到test-compile 甚至compile。希望了解将插件绑定到阶段的一般模式将足以解决问题。 (编辑了我的答案)。
      • +1 用于展示如何将dependency:tree 添加到test 阶段。我将添加另一个答案,向您展示如何让dependency:treetest 阶段开始时运行。
      • 感谢两位提供全面而迅速的答复。我想我只能勾选一个“正确”,所以我勾选了另一个,因为它稍微更准确,但两者都很有用且非常感谢。
      猜你喜欢
      • 1970-01-01
      • 2015-03-03
      • 2012-01-04
      • 1970-01-01
      • 2011-04-12
      • 2023-01-24
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多