Is it possible to override executions in maven pluginManagement? - maven

In parent POM, I have:
<pluginManagement>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>execution 1</id>
...
</execution>
<execution>
<id>execution 2</id>
...
</execution>
<execution>
<id>execution 3</id>
...
</execution>
</executions>
</plugin>
<pluginManagement>
My questions are:
Is it possible to disable some <execution> in sub-projects, e.g, only run execution 3 and skip 1 and 2?
Is it possible to totally override the executions in sub-projects, e.g. I have an exection 4 in my sub-projects
and I want only run this execution and never run execution 1,2,3 in parent POM.

A quick option is to use <phase>none</phase> when overriding each execution. So for example to run execution 3 only you would do the following in your pom:
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>execution 1</id>
<phase>none</phase>
...
</execution>
<execution>
<id>execution 2</id>
<phase>none</phase>
...
</execution>
<execution>
<id>execution 3</id>
...
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
It should be noted that this is not an officially documented feature, so support for this could be removed at any time.
The recommend solution would probably be to define profiles which have activation sections defined:
<profile>
<id>execution3</id>
<activation>
<property>
<name>maven.resources.plugin.execution3</name>
<value>true</value>
</property>
</activation>
...
The in your sub project you would just set the required properties:
<properties>
<maven.resources.plugin.execution3>true</maven.resources.plugin.execution3>
</properties>
More details on profile activation can be found here:
http://maven.apache.org/settings.html#Activation

Related

Maven wildfly deployment with multiple servers (standalone)

in my environment I have two wildfly server where I want to deploy with the wildfly-maven-plugin.
The servers differ in the name dev01 and dev02 but have the same port 9993 and username and password.
My understanding is that the wildfly-maven-plugin support only single server deployment.
If the problem are not big enough we use a module/submodule structure where the war file will be build in a submodule.
I'm using two profiles wildfly-deploy-dev01 and wildfly-deploy-dev02.
<profiles>
<profile>
<id>wildfly-deploy-dev01</id>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>wildfly-deploy-dev02</id>
<build>
[...]
<profiles>
In the main module I skipped it.
In the war submodule:
<profiles>
<profile>
<id>wildfly-deploy-dev01</id>
<build>
<finalName>${project.artifactId}-v1.0</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
<id>wildfly-credentials<id>
<hostname>dev01.example.com</hostname>
<protocol>remote+https</protocol>
<port>9993</port>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>wildfly-deploy-dev01</id>
<build>
[same as above for hostname dev02.example.com]
</profiles>
First I was thinking everthing works fine but then I found out that only the last server will be deployed.
mvn wildfly:deploy -P wildfly-deploy-dev01,wildfly-deploy-dev02
I played around by setting the configration after the execution tag without success. It looks that the second profile overwrite the first one.
Futher I hardcoded the finalname because the parsedVersion is not parsed.
<finalName>${project.artifactId}-v${parsedVersion.majorVersion}.${parsedVersion.minorVersion}</finalName>
At the moment I'm lost with Maven. Has anybody an idea how I can deploy with the plugin on two servers?
Thanks,
Markus
Ways which I tried:
https://github.com/tsotzolas/wildfly_maven_plugins_examples/blob/master/deployToMultiplesServer/pom.xml
wildfly-maven-plugin not deploying when multiple profiles selected
Cannot access parsedVersion value in pom properties
You should be able to do this in a single profile with different executions. There shouldn't be a need to multiple profiles.
<profiles>
<profile>
<id>wildfly-deploy</id>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
<id>wildfly-credentials<id>
<protocol>remote+https</protocol>
<port>9993</port>
</configuration>
<executions>
<execution>
<id>deploy-dev1</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<hostname>dev01.example.com</hostname>
</configuration>
</execution>
<execution>
<id>deploy-dev2</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<hostname>dev02.example.com</hostname>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profiles>
With this you'd just have to do mvn clean install -Pwildfly-deploy.

How can I run two gmaven scripts in one pom.xml?

I want to have two scripts run from maven, one of which depends on an environment variable. I'm trying something like this:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
println "My script"
</source>
</configuration>
</execution>
</executions>
</plugin>
</build>
...
<profile>
<activation>
<property>
<name>env.MY_ENV_VAR</name>
<value>runStuff</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
println "My conditional script"
</source>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
When I run "mvn validate" to test this, I get "My script". When I set the env variable and run it again, I get "My conditional script" but not "My script". It seems that if the condition is satisfied and the second one runs, the first one will not.
I want to run the first one unconditionally and the second one only if the env variable is set. I thought of checking the env variable in the script itself but that seems problematic too, according to this question.
I'm new to maven so it's not unlikely there's a simple solution but I'm not seeing it.
I found the answer. Each execution must have a unique ID. If you don't specify an ID, you get 'default' for both. Once I gave the conditional one a non-default ID, they both run.
<build>
<plugins>
<plugin>
...
<executions>
<execution>
<id>Unconditional-script</id>
...
</execution>
</executions>
</plugin>
</build>
...
<profile>
...
<build>
<plugins>
<plugin>
...
<executions>
<execution>
<id>Conditional-script</id>
...
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

Maven: How to print the current profile on the console?

I'm trying to print the current profile that is active running a build of a Maven Project.
I'm using the maven-antrun-plugin in order to print messages on the console, in combination with a property that refers to the current profile.
I have tried the following properties:
${project.activeProfiles[0].id}
${project.profiles[0].id}
But in both cases it prints the "string" as it is written, without resolving the variable.
This is my test:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>current active profile: ${project.activeProfiles[0].id}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
But this is the result that I obtain:
main:
[echo] current active profile: ${project.activeProfiles[0].id}
Any suggestion will be appreciated.
Thanks.
The maven-help-plugin offers what you need. It has an active-profiles goal.
You can add it to your pom or even call it from the command line (include it in your maven build call). The How can I tell which profiles are in effect during a build? section of the Maven profile introduction page will show you how. In short:
mvn help:active-profiles
As this does not work for you (see comments) here is another solution:
I think the active profiles (there can be more than one!) are not propagated as available variables - but properties are.
So set a custom property in the profile section and use that, like
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<myProfile>default</myProfile>
</properties>
</profile>
<profile>
<id>debug</id>
<activation>
<property>
<name>debug</name>
</property>
</activation>
<properties>
<myProfile>debug</myProfile>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>current active profile: ${myProfile}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
you can add the maven-help-plugin in your pom to display always the active profile
<build>
<plugins>
<!-- display active profile in compile phase -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>show-profiles</id>
<phase>compile</phase>
<goals>
<goal>active-profiles</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
source: https://www.mkyong.com/maven/maven-profiles-example

How to trigger Maven SCM plugin to automatically switch goals based on existing directory?

I'm new to Maven and having an issue where I'm trying to automatically change the SCM plugin goal from checkout to update based on whether the source is already checked out.
Can anyone show me a code example to get this working?
This is the plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.9.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>checkout</goal>
</goals>
<configuration>
<connectionType>developerConnection</connectionType>
<scmVersion>master</scmVersion>
<scmVersionType>branch</scmVersionType>
<checkoutDirectory>${project.basedir}/src</checkoutDirectory>
<workingDirectory>${project.basedir}/src</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
change goal:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.9.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>update</goal>
</goals>
<configuration>
<connectionType>developerConnection</connectionType>
<scmVersion>master</scmVersion>
<scmVersionType>branch</scmVersionType>
<checkoutDirectory>${project.basedir}/src</checkoutDirectory>
<workingDirectory>${project.basedir}/src</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
Reference
https://maven.apache.org/scm/maven-scm-plugin/
https://maven.apache.org/scm/maven-scm-plugin/update-mojo.html
To change the goal of the SCM plugin was inspired by Đỗ Như Vý (above).
Approach was to
Place the goal in a property called scm.goal set to a default value
ie update.
Use a profile (bootstrap) to change the scm.goal property value from
'update' to 'checkout'.
Activate the bootstrap profile based on missing .gitignore file.
Place the property scm.goal in the SCM plugin
goal element.
Code:
<properties>
<scm.dest.path>${project.basedir}/src</scm.dest.path>
<scm.goal>update</scm.goal>
</properties>
<profiles>
<profile>
<id>bootstrap</id>
<activation>
<file>
<missing>./src/.gitignore</missing>
</file>
</activation>
<properties>
<scm.goal>checkout</scm.goal>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.9.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>${scm.goal}</goal>
</goals>
<configuration>
<connectionType>developerConnection</connectionType>
<scmVersion>master</scmVersion>
<scmVersionType>branch</scmVersionType>
<checkoutDirectory>${scm.dest.path}</checkoutDirectory>
<workingDirectory>${scm.dest.path}</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
...

Automatically activate parent plugin in Maven

Is it possible to have a plugin defined in the parent POM which is deactivated, and when the child inherits this plugin it gets automatically activated?
I guess you want to configure the plugin in your parent pom, but use it only in the inherited projects. Maven has a section for this - configure your plugins in pluginManagement, but bind them to a phase just when you needed it, e.g. omit the phase tag in pluginManagement, but specify it under in you inherited pom.
So 'siddhadev' is exactly correct. You can define the plugin configuration in the parent pom with a given id:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>child-caller</id>
<!-- 'phase' omitted -->
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="called from child!" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
And, in the child POM, you can explicitly list the phase where this should be called:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>child-caller</id>
<phase>compile</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
I've used this for targeting various JREs. Unfortunately, because you can't use the maven-compiler-plugin with different destination directories (which I consider a bug in the plugin), you must use Ant.
This isn't exactly what you're after, but I think it will work well enough for you.
If you declare the plugin in a pluginManagement tag in the parent, the configuration will be inherited by any child projects that declare that plugin.
For example, in the parent declare that the compile plugin uses Java 5 for test compilation.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
Then in a child, you simple declare the compiler plugin and the configuration from the parent will be inherited:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
You can declare a plugin at the top level pom and tell it to be skipped and then tell it to not be skipped at the child level. It's not quite automatic, but very minimal in the override verbosity.
Parent Pom, disabling the plugin, but declaring all the config:
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-surefire-plugin</artifactid>
<configuration>
<skip>true</skip>
...lots more config...
...lots more config...
...lots more config...
</configuration>
</plugin>
Child Pom, enabling the plugin:
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-surefire-plugin</artifactid>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
I went with the following solution:
Configure the plugin in the parent-pom in the pluginManagement-section. Bind the plugin to an existing phase.
Deactivate the plugin for the parent-pom by binding it to a nonexistent phase: Override the phase in the plugins-section.
Activate the plugin in each child-pom by including the plugin in the plugins section.
Example parent-pom:
<defaultGoal>install</defaultGoal>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>install-ejb-client</id>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>${ejb-client-file}</file>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<packaging>jar</packaging>
<classifier>client</classifier>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</pluginManagement>
<plugins>
<plugin>
<!-- deactivate the plugin for this project, only child-projects do generate ejb-clients -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<inherited>false</inherited>
<executions>
<execution>
<id>install-ejb-client</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
Example child-pom:
<build>
<plugins>
...
<plugin>
<!-- Install the generated client-jar. Property 'ejb-client-file' has to be set! Plugin configuration is in the parent pom -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
</plugin>
...
</plugins>
</build>
As far as I know, there is no generic solution for this. At least for the moment...
One idea (I didn't try it, but it may work) is to define, in the parent pom.xml an execution goal that does not exist, for example:
<executions>
<execution>
<goals>
<goal>noGoal</goal>
</goals>
</execution>
</executions>
and in every child, you redefine a correct goal.
The problem of this solution (if it works, of course ;) ) is that you must redefine the plugin configuration for every child. Otherwise, it will not be executed.

Resources