VanesBee
VanesBee

Reputation: 616

Maven calling maven plugin goal from command line

Can you tell me how to call maven surefire in command line with the following configuration ?

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <executions>
        <execution>
            <id>Custom tests</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <classesDirectory>target/generated/classes/normalCase/</classesDirectory>
                <reportsDirectory>target/generated/reports/normalCase/</reportsDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

When surefire is defined like this in my pom.xml and I execute the phase test, it work exactly how i want it to work : it tries to run my tests on the classes located in target/generated/classes/normalCase.

So I tried this command line:

mvn surefire:test -DclassesDirectory="target/generated/classes/normalCase/"

But no, it keep checking the classes in the default value directory which is "target/classes".

So how can I achieve this in command line ?

Upvotes: 1

Views: 1241

Answers (1)

Tunaki
Tunaki

Reputation: 137104

To recap the situation you have. You are generating multiple version of your source code during the build, each of those version ends up in a separate folder under target. For each of those versions, you would like to execute your unit tests with the maven-surefire-plugin. Let's consider the base directory to be target/generated/classes. That means you have multiple subdirectories target/generated/classes/version1, target/generated/classes/version2... for each version.

A possible solution would be to use the iterator-maven-plugin to iterate over all subdirectories of a folder and invoke the maven-surefire-plugin from all those subdirectories. The variable @item@ holds the current item.

<plugin>
    <groupId>com.soebes.maven.plugins</groupId>
    <artifactId>iterator-maven-plugin</artifactId>
    <version>0.3</version>
    <executions>
        <execution>
            <id>iterate</id>
            <phase>test</phase>
            <goals>
                <goal>iterator</goal>
            </goals>
            <configuration>
                <folder>target/generated/classes</folder>
                <pluginExecutors>
                    <pluginExecutor>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-surefire-plugin</artifactId>
                            <version>2.19.1</version>
                        </plugin>
                        <goal>test</goal>
                        <configuration>
                            <classesDirectory>target/generated/classes/@item@</classesDirectory>
                            <reportsDirectory>target/generated/reports/@item@</reportsDirectory>
                        </configuration>
                    </pluginExecutor>
                </pluginExecutors>
            </configuration>
        </execution>
    </executions>
</plugin>

Upvotes: 1

Related Questions