Enable certain Maven tests by passing a command line switch - maven

I have a single module project that has some unit tests that require an external hardware device. I don't want these tests to execute unless I indicate that the device is available.
I feel like this is solvable using Maven properties and the SureFire exclusion/inclusion configuration, but I can't quite see how to do it. A similar question shows how to disable/enable all the tests in a project based on a Maven property, but doesn't quite answer my issue.
In summary, I wish to identify a pattern (e.g. **/*ResourceTest.java) that describes the tests I don't want to run, unless I pass a Maven property to enable them.
E.g.
mvn clean install (runs the standard tests, but skips device-related tests)
mvn -Drun.device.tests=true clean install (runs all the tests)
Thanks in advance.
(Edited to remove the misleading usage of the word "resource" > replaced with "hardware device").

You also can just use the JUnit Assume methods to decide (inside the test) if a test should be executed or skipped.
The best option IMHO would however be to 'declare' the device dependend tests to be "integration tests" and let them be executed by the Maven Failsafe Plugin. I think this would be the "build in" maven solution without any profile 'magic'.

The link you provided gave the good answer.
The right way
Use a mix of Profile Management and Surefire Configuration inclusion / exlcusion is the right way.
You should ask yourself WHY you want to activate some tests dependings on a resource. The resource should always been in your classpath.
If not, you probably just want to activate some test manually, for some tricky reasons. In that case consider this is a bad use of Maven (how would you automate that on a distant server for instance ?)
What you asked
If you really really want to do that, because you have some good reasons that we are not aware of, simply use this :
This example will trigger the profile when the generated file target/generated-sources/axistools/wsdl2java/org/apache/maven is missing.
Example from Maven official doc : http://maven.apache.org/guides/introduction/introduction-to-profiles.html
<profiles>
<profile>
<activation>
<file>
<missing>target/generated-sources/axistools/wsdl2java/org/apache/maven</missing>
</file>
</activation>
...
</profile>
</profiles>
As of Maven 2.0.9, the tags and could be interpolated. Supported variables are system properties like ${user.home} and environment variables like ${env.HOME}. Please note that properties and values defined in the POM itself are not available for interpolation here, e.g. the above example activator cannot use ${project.build.directory} but needs to hard-code the path target.
You could find more information here : http://www.sonatype.com/books/mvnref-book/reference/profiles-sect-activation.html
Hope that will help.
Don't hesitate to challenge my point of view with you own reasons (even legacy code ;) ) or experience

To expand on #Jean-Rémy answer, I have done the following in my project POM file:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<excludes>
<exclude>${tests.to.skip}</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<!-- This profile will be used when running tests without a device -->
<id>default-profile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<tests.to.skip>**/*DeviceTest.java</tests.to.skip>
</properties>
</profile>
<profile>
<id>device-profile</id>
<activation>
<property>
<name>device</name>
<value>true</value>
</property>
</activation>
<properties>
<!-- Unsure how to match nothing -->
<tests.to.skip>NOTHING</tests.to.skip>
</properties>
</profile>
This creates two profiles, the default profile will exclude the device tests, whereas the "device-profile" will execute all tests.
To execute the device profile, one can execute mvn -Ddevice=true test.

Related

Maven skip module when we package / test

I have multiple modules in my maven project.
<modules>
<module>module-1</module>
<module>module-2</module>
<module>module-3</module>
</modules>
I would like to skip 1 module , say module-3 when i do package or test etc. This below profile option does not seem to work.
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>module-1</module>
<module>module-2</module>
</modules>
</profile>
</profiles>
How to make this work or are there any alternatives?
After reading your comment, let me suggest the following:
module-3 is a module with integration tests. Its ok to compile it with other modules, it will be pretty fast and probably you'll want it anyway, otherwise it will eventually stop to compile. In addition, I don't think you should "play" with module structure like this, because the IDE should open all the modules (probably you open this "main" pom.xml)
Having said that you will probably want to run the tests only with some kind of flag. In this case you can define a profile for running integration tests instead of running the module.
In maven integration tests are usually managed by failsafe-plugin. So you can configure this plugin so that it will be actually executed only upon some property. It can be done with maven profiles.

Is it possible to use a maven property to activate a profile based upon a file?

I would like to download the JACOB dlls when they're not in my local repository.
As a consequence, I have those two profiles
<profile>
<id>use-jacob-dll</id>
<activation>
<file>
<exists>${settings.localRepository}/com/hynnet/jacob/1.18/jacob-1.18-x64.dll</exists>
</file>
</activation>
<dependencies>
<dependency>
<groupId>${jacob.groupId}</groupId>
<artifactId>jacob</artifactId>
<type>dll</type>
<classifier>x64</classifier>
<version>${jacob.version}</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>download-jacob-dll</id>
<activation>
<file>
<missing>${settings.localRepository}/com/hynnet/jacob/1.18/jacob-1.18-x64.dll</missing>
</file>
</activation>
But, even when download-jacob-dll has accomplished its goal, a call to mvn help:active-profiles indicates the following
The following profiles are active:
- tests-for-eclipse (source: com.capgemini.admdt:kpitv:1.2.4-SNAPSHOT)
- download-jacob-dll (source: com.capgemini.admdt:kpitv:1.2.4-SNAPSHOT)
I suspect it is due to the fact that I use the ${settings.localRepository} in my activation property.
Question: Is it the cause of the failure? And if so, how can I activate my profile only when dependency is missing ?
Is it possible to use a maven property to activate a profile based upon a file?
No, as stated by the Maven documentation on profiles
Supported variables are system properties like ${user.home} and environment variables like ${env.HOME}. Please note that properties and values defined in the POM itself are not available for interpolation here, e.g. the above example activator cannot use ${project.build.directory} but needs to hard-code the path target.
However, from the POM documentation we also get that
a given filename may activate the profile by the existence of a file, or if it is missing. NOTE: interpolation for this element is limited to ${basedir}, System properties and request properties.
Hence, indeed no Maven properties except ${basedir} are allowed.
And if so, how can I activate my profile only when dependency is missing?
By hardcoded path to the dependency or concerned file would be a solution, even though not portable like the solution you meant.
Alternatively you could use a request property as mentioned by the documentation above, thus need to configure the activation with a property which then must be passed from the command line (more portable but more fragile as well):
<activation>
<file>
<missing>${path}/com/hynnet/jacob/1.18/jacob-1.18-x64.dll</missing>
</file>
</activation>
Then invoke maven as following:
mvn clean install -Dpath=path_to_local_rep
The solution above could be reasonable in some contexts like Jenkins jobs.

Skip compilation in gmavenplus plugin

I currently work in big project built by Maven which has many integration tests modules which are marked as main (not testing) sources.
I am trying to create a profile which would skip compilation of these modules.
I expected gmaven plugin to allow "skip" configuration parameter but this is not the case.
Is there any way to skip module processing without pointing gmaven plugin to non-existent directory and without copy-paste of all modules except integration tests to a separate profile?
You can put the integration test modules in a separate profile of the parent pom where you list the modules. The profile should be active unless you disable it by setting a property when running the Maven build (-DskipIntegrationTestModules). (Don't use activeByDefault.)
<modules>
<module>my-project</module>
</modules>
<profiles>
<profile>
<id>build-integration-tests</id>
<activation>
<property>
<name>!skipIntegrationTestModules</name>
</property>
</activation>
<modules>
<module>my-project-integration-test</module>
</modules>
</profile>
</profiles>
You can find more details in the Maven Introduction to Build Profiles.
You should also know that it can be dangerous to have modules in build profiles because they could be accidentally left out when doing release builds. I think it should be OK in this case because the profile has to be deactivated explicitly.

What is the difference between mvn clean install -Drelease-build and mvn clean install?

I need to know what is the difference between these two commands which do almost same thing if we execute this command.
Maven doesn't assign any special meaning to release-build. However, projects can use properties (-Dproperty-name=value or just -Dproperty-name) to activate profiles that change the way the project is built (Maven - activate profile based on project property). It's likely to enable some extra steps that are only necessary for final releases. For example, this project uses it to include a native library in the build:
<profile>
<id>native</id>
<activation>
<property>
<name>release-build</name>
</property>
</activation>
<modules>
<module>pi4j-native</module>
</modules>
</profile>
There's no general answer: you'll need to consult the documentation, or build, of the project you're working with.
You can do this much more simpler automatically to activate a profile in case of a release simply via the maven-release-plugin which already supports that out-of-the-box like this:
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<releaseProfiles>release</releaseProfiles>
</configuration>
</plugin>
Apart from that it's really bad to acitvate a module only in case of a profile is active. This will lead so several problem.

Switch to turn on/off Atlassian Clover in Maven

I am using Clover via Maven during every build. The majority of the time the code coverage does not change much so I now do not want to have it run every time. Clover slows down the compilation a lot. Is there a maven switch that allows me to not run clover? It is run as a plugin so an alternative question is whether it is possible in Maven to disable some plugins with certain switches.
FYI the clover website is here for those of you who do not use it.
http://www.atlassian.com/software/clover/overview
You should be able to accomplish this with profile activation and a custom property.
If you surround your clover plugin with a profile and use profile activation, you can define the property on the command line when you want to run it.
<profiles>
<profile>
<activation>
<property>
<name>clover</name>
</property>
</activation>
...
</profile>
</profiles>
Then, when calling maven, use -Dclover on the command line to activate the clover steps. If you don't provide the -Dclover then clover will not run.

Resources