CryptoFool
CryptoFool

Reputation: 23129

How do I get Maven to not run tests during a "package" execution?

I want to disable the execution of tests during a "package" execution of a Maven build. That is, I want to only enable the "surefire" plugin when I've typed "mvn test" at my command prompt, but not when I've typed "mvn package". Seems like an obvious want, right?? I have a project with a bunch of leaf modules, each with a pom.xml file, and a parent pom that is included by each leaf pom file. Just a few of my modules have tests associated with them.

I've tried a bunch of things, and nothing gives me the behavior I want. Reading the documentation, looking at some threads on the internet, and asking ChatGPT about this, all suggest that this directive in my parent pom file should give me the behavior I desire:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M5</version> <!-- Or your version -->
    <configuration>
        <skipTests>true</skipTests> <!-- Skip tests by default for all phases -->
    </configuration>
    <executions>
        <execution>
            <id>run-tests-in-test-phase</id>
            <goals>
                <goal>test</goal> <!-- Ensure that tests are run during the "test" phase -->
            </goals>
            <phase>test</phase> <!-- This binds the test execution only to the "test" phase -->
            <configuration>
                <skipTests>false</skipTests> <!-- Don't skip tests during "test" phase -->
            </configuration>
        </execution>
    </executions>
</plugin>

But this doesn't work. My tests run when I execute both "mvn clean package" and "mvn test". I only want my tests to run when I exec "mvn test". I believe that this code only reiterates the default behavior. I've wasted hours playing with different proposed solutions. Nothing works.

I think I know why this is. So this SO question is really aimed at getting confirmation that the following mind boggling fact is correct, or an example of why it is not:

There's no way to get the behavior I want. When I execute the "package" phase, the "test" phase is run because it proceeds the "package" phase. This is an unmodifiable reality in Maven. So the question is, how do modify Maven's behavior based on the "end phase" that I asked to have executed? I don't believe that I can. I think the confusion here is that the final phase that I've specified is not what's being tested by the code above or any other proposed solution. Since the "test" phase will be included regardless of if I asked for a "package" or "test" execution, any logic based on matching the "test" phase will not make the distinction I'm looking for. I want to either exclude the "test" phase when I specify the end/target phase of "package", or I need a way to conditionally affect Maven's execution in some way based on the target phase I've asked for, like by setting a variable based on the target phase. What I'm finding is that there is quite literally no way to do this in Maven. I find that mind boggling. I figure that I must be wrong. Am I?

Upvotes: 0

Views: 111

Answers (2)

Illya Kysil
Illya Kysil

Reputation: 1756

You may bind the execution with id default-test to a non-existing phase to achieve expected result, for example

<executions>
  <execution>
    <id>default-test</id>
    <phase>never</phase>
  </execution>
</executions>

Upvotes: 0

J Fabian Meier
J Fabian Meier

Reputation: 35843

The answer is exactly what you said. There is no way to do this in Maven. But of course, this is not the end of the story. I mean, developers usually use some IDE like IntelliJ or Eclipse, and you can probably configure the behaviour you want in the IDE. You could also think about triggering Maven commands by commits, either locally or on you build server. Or you can write shell commands that help you.

Upvotes: 0

Related Questions