Search code examples
javamavenwarningsmaven-surefire-plugin

In maven - how to display a message to the user when the verify phase is run on the default execution?


In our maven build - we're using the default profile for builds, and a different profile for tests. When our tests are run under the default profile, for various reasons they break.

eg good in our team with our build

mvn -Pfoo verify

bad in our team with our build

mvn verify

We want to encourage people in our team to run the tests under the 'foo' lifecycle, and warn them when they don't.

My current approach to solving this problem is to create a new surefire test for the default profile, exclude all the tests except a new DefaultProfileWarningTest whose sole purpose is to tell the user to run the tests under the foo profile.

So the test might look like:

public class DefaultProfileWarningTest {
    @Test
    public void displayWarning() {
        System.out.println("The tests aren't running - you should have run **mvn -Pfoo verify**");
    }
}

With an execution in the pom.xml similar to:

<profiles>
    <profile>
        <id>foo</id>
        ...      
    </profile>
    <profile>
        <id>my-default-profile</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                    <configuration>
                        <includes>
                            <include>DefaultProfileWarningTest.java</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
<profiles>

But this seems like an overwrought kludge. Assuming we can't fix the profile thing - is there a simple lightweight way to display a message to the user when the verify phase is for the default profile?

My question is: In maven - how to display a message to the user when the verify phase is run on the default execution?


Solution

  • A possible solution would be to define an execution of the maven-antrun-plugin that echos a message with the echo task. It would be skipped when run under the foo profile, using the attribute skip. The switch can be made from a Maven property that is set to true in the foo profile and false by default.

    Consider the following:

    <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
            <execution>
                <phase>test</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <target>
                        <echo>The tests aren't running - you should have run mvn -Pfoo verify</echo>
                    </target>
                    <skip>${isRightProfile}</skip>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    Then you can configure your profiles like so:

    <profile>
        <id>foo</id>
        <properties>
            <isRightProfile>true</isRightProfile>
        </properties>
    </profile>
    <profile>
        <id>my-default-profile</id>
        <properties>
            <isRightProfile>false</isRightProfile>
        </properties>
        <!-- rest unchanged -->
    </profile>
    

    When the foo profile is activated, the isRightProfile property will be set to true, so the execution of the AntRun plugin will be skipped and no messages will be printed. When the default profile is activated, this property will be set to false and the messages will be echoed.