Maven run "dependency:tree" at start of "test" phase

This will output the test dependency tree:

mvn test dependency:tree -DskipTests=true

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.


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.

Tags:

Maven

Phase