Switch to turn on/off Atlassian Clover in Maven - 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.

Related

Custom alternative configurations for Maven life cycles

How can I add custom alternative configurations to the default life cycles in the pom.xml?
So that I for example can still call mvn package (default behavior), but also mvn quickPackage which runs the default package life cycle, but has e.g. skipTests true and the XML equivalents for other command line parameters...?
All I could find was either directing me into:
Using extra special plugins, even write my own, which I'm not allowed here.
Apply the configuration to everything, or at least always to a certain life cycle. Removing the default behavior of e.g. package.
Do I have to fallback to use command line scripts (which counters the point of having a build management system)? Or is there a pom.xml solution?
You can use Maven Profiles for that, e.g.:
<profiles>
<profile>
<id>quick</id>
<properties>
<bla.bla.bla>true</bla.bla.bla>
<this.that.then>true</this.that.then>
<everything.now>true</everything.now>
<feature.set.extra>true</feature.set.extra>
</properties>
</profile>
</profiles>
Activate it with:
mvn ... -Pquick ...

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.

Maven: Only execute plugin when a command line flag is present

I want Maven to only run a certain plugin when there is a flag on the command line when I call the mvn command.
for example:
Let's say I have a plugin called maven-foo-plugin. I only want maven to run this plugin when the flag --foo is present when I call the maven command.
So, instead of saying...
mvn install
...I would say...
mvn install --foo
The first one should NOT use/call the plugin maven-foo-plugin, but the second one should. By default, I don't want this plugin to run, but if and only if, the --foo is present. Is there another parameter that I add to this maven-foo-plugin to make it do this?
The plugin I'm using is the maven-antrun-plugin that has the task that unzips a file. Of course, sometimes this file is present and sometimes not. Sometimes, it's not present and I don't need it. So, I need a way (Preferably through command line unless someone has a better idea) to turn on/off this plugin.
As #Gab has mentioned, using Maven profiles is the way to go. Create a profile and configure your plugin in the profile. Then in the profile's activation section, reference the environment variable, with or without a value:
<profiles>
<profile>
<activation>
<property>
<name>debug</name>
</property>
</activation>
...
</profile>
</profiles>
The above example would activate the profile if you define the debug variable when calling Maven:
mvn install -Ddebug
Please note that you have to prefix environment variables with -D in order to pass them to the JVM running Maven.
More details can be found here: http://maven.apache.org/guides/introduction/introduction-to-profiles.html
The correct way to trigger conditional action in maven is to use profile.
You can so configure a specific profile including the plugin activation, you will then trigger the execution using
mvn targetPhase -P myprofile
(you can also specify a specific property value to activate the profile)
see Maven: Using a Plugin Based On Profile

Enable certain Maven tests by passing a command line switch

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.

Is there a way to achieve reverse of maven profile activation by property?

I want to have a profile that triggers a certain plugin(say PMD) but I want to explicitly disable that plugin execution sometimes.
So I want to have a profile that is always active except when a property is defined.
Something like mvn -Dnopmd clean install, and the profile gets de-activated. Other than that the profile should always be active.
You can activate a profile when a property is not specfied like so:
<profile>
<id>someprofile</id>
<activation>
<property>
<name>!property.name</name>
</property>
</activation>
</profile>
This is also explained in the Maven documentation, Introduction to Build Profiles.

Resources