7

Is it possible to run a maven test build (mvn clean test) in a multi module maven project and to skip/ignore a particular module's test? like -Dmaven.test.skip=true but for a particular module and not all the modules? I dont want to change the surefire <configuration> to include <skipTests>true</skipTests> for the module which I want to skip for tests. I wanted to know if this can be done from command line. I need this because in my project I've many modules and particular one or two takes really long to execute test, so when I want to test only couple of modules I'd like to skip these time taking modules to which I've not made any changes.

Jay
  • 1,195
  • 1
  • 12
  • 24
  • 1
    Yes. You need to read up on [maven profiles](http://maven.apache.org/guides/introduction/introduction-to-profiles.html) – Brian Roach Feb 14 '12 at 15:57
  • See also [maven: How can I skip test in some projects via command line options?](https://stackoverflow.com/questions/9123075/maven-how-can-i-skip-test-in-some-projects-via-command-line-options) – Vadzim Sep 15 '19 at 10:48

2 Answers2

5

Is is really a problem for you to change the configuration of the surefire plugin ? Because you could change it one time only in your module ...

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12</version>
            <configuration>
                <skipTests>${skip.foo.module.tests}</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>

... and delegate the true/false value of the skipTests tag to a maven property, activated by a dedicated profile :

<properties>
    <skip.foo.module.tests>false</skip.foo.module.tests>
</properties>

<profiles>
    <profile>
        <id>SKIP_FOO_MODULE_TESTS</id>
        <properties>
            <skip.foo.module.tests>true</skip.foo.module.tests>
        </properties>
    </profile>
</profiles>

So that you could deactivate the tests in Foo module with the command line :

mvn clean test -P SKIP_FOO_MODULE_TESTS

Yanflea
  • 3,716
  • 1
  • 12
  • 14
  • Thanks Yanflea. Although this was not exactly what I wanted, but this works for me. With this approach I can have one profile at a time, so in case if I want to skip tests for more than one modules, I cant. But anyway that would be little too much. – Jay Feb 15 '12 at 18:01
  • Your modules can share the same parent pom, in which you define your profiles. What prevents you from using the same profile to skip tests on several child modules at the same time ? – Yanflea Feb 15 '12 at 22:11
  • That will work only if I I want to skip same modules all the time. When I'm developing, sometime I make change to module-1 which does not affect module-7 and module-9, so I would like to skip those, but at some other time if I make changes to module-3, which affects module-7, but not module-9, in that case I'd like to skip only module-9. For such scenarios, sharing profiles in parent will not work. Now I don't want to pollute my parent pom with all sorts of profiles skipping tests for different modules and combinations. – Jay Feb 16 '12 at 10:22
  • 1
    Ok, I see. Actually, what you really want is not just skip tests of some modules, but rather build at a given moment only a tree part of your entire project, is that it ? Consider the build option _-pl_ in that case. It allows to build only a subset of your project. The maven reactor will build only the modules list passed after _-pl_ AND the modules on which they depend. – Yanflea Feb 16 '12 at 14:59
  • Thanks Yanflea. Your inputs have really helped a lot. – Jay Feb 17 '12 at 07:56
0

You could accomplish that with a profile that did the configuration of surefire to skip. That'd allow you to keep the test running most of the time, but when you wanted to skip the tests for one of the modules sometime, you could invoke that profile. You can then exclude all tests using skip tests or you could use the excludes option to only exclude the one or two tests that are long executing.

Michael
  • 5,271
  • 1
  • 18
  • 21