mavenphase

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


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?


Solution

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