How to run the exec-maven-plugin if a system property id set in Maven 3.x? - maven

I want to run the 'exec-maven-plugin' if a system property is set. How do I accomplish this in Maven 3.x?
For example, given:
mvn clean install -DrunTheExec="yes"
Then how can I implement this logic:
<!-- if $(runTheExec) == yes then run this plugin -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
...
</plugin>

I was just about to add the same suggestion as Johnathon, but using the profile a little differently.
<profiles>
<profile>
<id>runTheExec</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
...
then to activate it:
mvn clean install -PrunTheExec

You'll need to define a profile in your pom, with the plugin defined inside of it. In your example, this would be:
<profiles>
<profile>
<activation>
<property>
<name>runTheExec</name>
<value>yes</value>
</property>
</activation>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
...
</plugin>
</plugins>
</profile>
</profiles>

Related

<activeByDefault/> doesn't work when another profile triggered

I have profiles look like this.
<profile>
<id>active-by-default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>${version.org.mongodb}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>java8</id>
<activation>
<jdk>(,1.8]</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<configuration>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
<lib>${java.home}/lib/jce.jar</lib>
<lib>${java.home}/lib/jsse.jar</lib>
</libs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>java9</id>
<activation>
<jdk>[1.9,)</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<configuration>
<libs>
<lib>${java.home}/jmods/java.base.jmod</lib>
</libs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Those two java- profiles are for ProGuard.
And I found the active-by-default profile doesn't work even with no profiles specified.
I found it works when I remove one of those java- profile which meets my current JDK version.
Is this intended?
Yes, this is the documented behaviour :
This profile will automatically be active for all builds unless another profile in the same POM is activated using one of the previously described methods. All profiles that are active by default are automatically deactivated when a profile in the POM is activated on the command line or through its activation config.
https://maven.apache.org/guides/introduction/introduction-to-profiles.html

Maven Plugin Surefire no accepting outputDirectory

In Maven pom tried to have a single profile only for tests and their reports. Also all test related reports should end up in a single folder per project.
Acording to the documentation this should do the trick
<profiles>
<profile>
<id>integration</id>
<activation>
<property>
<name>integration</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<excludes>
<exclude>**/*$*</exclude>
<exclude>**/*_closure*</exclude>
<exclude>**/support/**</exclude>
</excludes>
<outputDirectory>${basedir}/target/reports</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Still all reports end up in ..\target\surefire-reports. Any idea why the ouputDirectory settings doesn't work
According to the documentation you have to use the reportsDirectory instead of outputDirectory which does not exist.

Maven overrides configuration in profiles plugin

I have a 3 maven profiles with plugins:
<profile>
<id>first</id>
<build>
<plugins>
<plugin>
...
<configuration>
<var>1</var>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>second</id>
<build>
<plugins>
<plugin>
...
<configuration>
<var>2</var>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>third</id>
<build>
<plugins>
<plugin>
...
<configuration>
<var>3</var>
</configuration>
</plugin>
</plugins>
</build>
</profile>
When i start my build with mvn clean install -P first,second,third -X, I discovered that all this plugins was executed with configuration from from third profile. Is there any way to preserve my configuration for each of my plugins and not to be overriden by third configuration?
As discussed in comments section, You would have to invoke 3 build activating each profile differently
for example
mvn clean install -Pfirst
mvn clean install -Psecond
mvn clean install -Pthird
and to disable compilation in second and third, you could configure maven-compiler-plugin for these profiles and use skipMain property to disable main's source compilation, also for tests

Skip exec-maven-plugin from Command Line Argument in Maven

By default in my project POM, exec-maven-plugin, rpm-maven-plugin will be executed,
which is not required in local compilation/build.
I want to skip these plugin execution by passing Command Line Arguments
I tried below command to skip them like normal plugins, but didn't work though!
mvn install -Dmaven.test.skip=true -Dmaven.exec.skip=true
-Dmaven.rpm.skip=true
This page should tell you that the name of the argument to be passed by cmdline (i.e. the user property) is called skip, which is a poorly chosen name. To fix this do the following:
<properties>
<maven.exec.skip>false</maven.exec.skip> <!-- default -->
</properties>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<skip>${maven.exec.skip}</skip>
</configuration>
</plugin>
Try -Dexec.skip from specification:
http://www.mojohaus.org/exec-maven-plugin/java-mojo.html#skip
Using profiles (as little as possible) and execution phase you may achieve what you want for plugins that do not handle the skip property:
Plugin configuration:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>rpm-maven-plugin</artifactId>
<executions>
<execution>
<phase>${rpmPackagePhase}</phase>
<id>generate-rpm</id>
<goals>
<goal>rpm</goal>
</goals>
</execution>
</executions>
<configuration>
...
</configuration>
</plugin>
Profile configuration:
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<rpmPackagePhase>none</rpmPackagePhase>
</properties>
</profile>
<profile>
<id>rpmPackage</id>
<activation>
<property>
<name>rpm.package</name>
<value>true</value>
</property>
</activation>
<properties>
<rpmPackagePhase>package</rpmPackagePhase>
</properties>
</profile>
</profiles>
Invocation:
mvn package -Drpm.package=true [...]

Maven profile not deploying to correct tomcat using maven-tomcat-plugin

So my Maven doesn't want to pickup the right profile, doesn't matter how I tell it:
mvn -Denv=dev tomcat7:deploy-only or mvn -P dev -Denv=dev tomcat7:deploy-only
I have two servers configured on my settings.xml and have my configuration below pointing to the correct ones.
Using mvn -X it seems that Maven is always picking up the last server in order it appears on the file, which means it's picking up the prod server.
Any clues anyone? Thank you!
Here's my <profiles>:
<profiles>
<profile>
<id>dev</id>
<activation>
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0-beta-1</version>
<configuration>
<server>dev</server>
<url>http://localhost:8080/manager/text</url>
<username>tomcat</username>
<password>tomcat</password>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>prod</id>
<activation>
<property>
<name>env</name>
<value>prod</value>
</property>
<jdk>1.6</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0-beta-1</version>
<configuration>
<server>prod</server>
<url>http://remote/manager/text</url>
<username>usr</username>
<password>pwd</password>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
You have an extra "activation" of <jdk>1.6</jdk> in your prod profile. Activations are an "or", not an "and". I'm guessing you're running maven with jdk6, and so prod is always active. Being the last one in the pom, its settings will win--a general rule in maven.

Resources