10

I need to get the "dependency:tree" goal output from Maven at the start of the "test" phase, to help debug an issue for which I need to know what versions of everything are being used.

In Ant it would have been easy, I've looked through the Maven docs and numerous answers on here but still can't figure it out, surely it's not that hard?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Ed Randall
  • 6,887
  • 2
  • 50
  • 45
  • Are you saying that you want the `maven-dependency-plugin` to run the `tree` goal during the `test` phase? – maba Oct 02 '12 at 09:36

3 Answers3

21

This will output the test dependency tree:

mvn test dependency:tree -DskipTests=true
trask
  • 621
  • 1
  • 6
  • 9
8

If you want to be sure that the dependency:tree is being run in the beginning of the test phase then you will have to move the original surefire:test goal to being conducted after the dependency:tree. To do that you will have to put the plugins in the order that they should be run.

Here is a complete pom.xml example that adds the maven-dependency-plugin before the maven-surefire-plugin. The original default-test is disabled and a new custom-test is added and this one will be run after the dependency-tree execution.

<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>

It's a little bit awkward but that is the way to disable executions.

Ed Randall
  • 6,887
  • 2
  • 50
  • 45
maba
  • 47,113
  • 10
  • 108
  • 118
5

Declare this in your project 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>

You can adopt this pattern to trigger any plugin during a specific build phase. See http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Plugins.

See also http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference for a list of the build phases. As maba points out, you need to carefully select the phase to ensure the tree goal is executed at the correct time.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • That will run the `dependency:tree` after the actual tests. The OP says that he wants it to run at the start of the test phase. – maba Oct 02 '12 at 09:40
  • Good point. Instead he could bind to `test-compile` or even `compile`. Hopefully understanding the general pattern for binding plugins to phases will be sufficient to solve the problem. (Edited my answer). – Duncan Jones Oct 02 '12 at 09:43
  • +1 for showing how to add the `dependency:tree` to the `test` phase. I will add another answer that shows you how to get the `dependency:tree` to run in the beginning of the `test` phase. – maba Oct 02 '12 at 09:52
  • Thank you both for your fully comprehensive and speedy answers. I think I can only tick one "right" so I ticked the other as it is slightly more exact but both are useful and much appreciated. – Ed Randall Oct 02 '12 at 13:05