Are parent default plugins being executed when using a child profile in maven? - maven

I get the following error. Is it because the install_path wasn't set? If so, does it mean that when using a profile, the default plugins aren't being executed (the one that sets the install_path)?
Execution:
mvn clean install site -Pfull
Error:
Failed to execute goal
org.apache.maven.plugins:maven-clean-plugin:2.5:clean
(clean-deploy-folder) on project bo-full: Missing base directory for
file set: null (included: [], excluded: [])
Parent:
<project>
<plugins>
<plugin>
<!-- Workaround maven not being able to set a property conditionally based on environment variable -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<exportAntProperties>true</exportAntProperties>
<target>
<property environment="env"/>
<condition property="install.path" value="${env.SERVER_HOME}" else="C:\MY_SERVER">
<isset property="env.SERVER_HOME" />
</condition>
<echo message="${install.path}"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
...
Child:
<project>
<profiles>
<profile>
<id>full</id>
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<executions>
<execution>
<id>clean-deploy-folder</id>
<phase>pre-site</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
<filesets>
<fileset>
<directory>${install.path}</directory>
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>
...

1) Default plugins should be executed even when using profile. Please verify this is happening by following build log - every plugin execution is logged by maven, even if the plugin itself doesn't log anything.
2) You should keep cleanup execution in the same Maven project/module as the execution that creates the property. One reason is that your child module can then be built separately (it'll use parent pom.xml from local/remote repository, if available). It's also possible that property isn't properly propagated within reactor build for whatever reason.
3) If the problem is indeed property propagation and antrun plugin is at fault, you can replace your antrun execution with a Maven profile. It should go something like this:
<properties>
<!-- default value goes here: -->
<install.path>C:\MY_SERVER</install.path>
</properties>
<profiles>
<profile>
<id>env</id>
<activation>
<property>
<!-- activate this profile when property is specified: -->
<name>env.SERVER_HOME</name>
</property>
</activation>
<properties>
<!-- override default property value: -->
<install.path>${env.SERVER_HOME}</install.path>
</properties>
</profile>
</profiles>

Related

injecting new argument/property value to maven profile in module

i have main pom.xml
i like to change from the main mvn command line cli which I'm using and change the :
<argument>${docker.image}</argument>
argument in only in the submodule :
module_y profile NOT module_x
this is the command I'm executing now :
mvn clean install -Ddocker_build=build
<artifactId>foo</artifactId>
<version>b1</version>
<packaging>pom</packaging>
<properties>
<docker.image>www.repo.org:8000/${project.artifactId}:${project.version}</docker.image>
</properties>
<modules>
<module>module_x</module>
<module>module_y</module>
</modules>
this is the section in the module_x and module_y
<profiles>
<profile>
<activation>
<property>
<name>docker_build</name>
<value>build</value>
</property>
<file>
<exists>Dockerfile</exists>
</file>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<executable>docker</executable>
<arguments>
<argument>build</argument>
<argument>-f</argument>
<argument>${project.basedir}/Dockerfile</argument>
<argument>-t</argument>
<argument>${docker.image}</argument>
<argument>.</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
in short, how do i change only the property value ${docker.image} in profile docker_build in module_y from main mvn run?
If you cannot change the POMs, this cannot be done.
The only possible approach would be to build the modules separately (by using -pl module_x -am or something like that) and use different command line parameters in both cases.

Maven: war generation and integration tests with different profile

I am trying to generate the war file of my application for production environment after all integration-tests have passed.
This is my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- Profile configuration -->
<profiles>
<!-- The configuration of the development profile -->
<profile>
<id>dev</id>
<properties>
<!--
Specifies the build.profile.id property that must be equal than the name of
the directory that contains the profile specific configuration file.
Because the name of the directory that contains the configuration file of the
development profile is dev, we must set the value of the build.profile.id
property to dev.
-->
<build.profile.id>dev</build.profile.id>
<!--
Only unit tests are run when the development profile is active
-->
<skip.integration.tests>true</skip.integration.tests>
<skip.unit.tests>false</skip.unit.tests>
</properties>
</profile>
<!-- The configuration of the production profile -->
<profile>
<id>prod</id>
<properties>
<build.profile.id>prod</build.profile.id>
</properties>
</profile>
<!-- The configuration of the test profile -->
<profile>
<id>integration-test</id>
<properties>
<build.profile.id>integration-test</build.profile.id>
<!--
Only integration tests are run when the integration-test profile is active
-->
<skip.integration.tests>false</skip.integration.tests>
<skip.unit.tests>true</skip.unit.tests>
</properties>
</profile>
</profiles>
<build>
<filters>
<!--
Ensures that the config.properties file is always loaded from the
configuration directory of the active Maven profile.
-->
<filter>utils/profiles/${build.profile.id}/config.properties</filter>
</filters>
<resources>
<!--
Placeholders that are found from the files located in the configured resource
directories are replaced with the property values found from the profile
specific configuration file.
-->
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.xlt</exclude> <!-- maven corrupt template files otherwise -->
</excludes>
</resource>
<resource>
<filtering>false</filtering>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xlt</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<!--
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
-->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<packagingExcludes>%regex[css/(?!styles).*.css]</packagingExcludes> <!-- Include, separated by comma, with JS minified: app/**/*.js -->
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<!-- Plugin to execute unit tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<!-- Skips unit tests if the value of skip.unit.tests property is true -->
<skipTests>${skip.unit.tests}</skipTests>
<!-- Excludes integration tests when unit tests are run -->
<excludes>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
<!-- Plugin to execute integration tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<executions>
<!--
Invokes both the integration-test and the verify goals of the
Failsafe Maven plugin
-->
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<!--
Skips integration tests if the value of skip.integration.tests
property is true
-->
<skipTests>${skip.integration.tests}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
<!-- Plugin to add extra directories to search during the execution of tests -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-integration-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<!-- Configures the source directory of our integration tests -->
<sources>
<source>src/integration-test/java</source>
</sources>
</configuration>
</execution>
<!-- Add a new resource directory to our build -->
<execution>
<id>add-integration-test-resources</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<!-- Configures the resource directory of our integration tests -->
<resources>
<!--
Placeholders that are found from the files located in the configured resource
directories are replaced with the property values found from the profile
specific configuration file.
-->
<resource>
<filtering>true</filtering>
<directory>src/integration-test/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
As you can see I use two different profiles: 'prod' and 'integration-test'
If I execute: mvn verify -Pintegration-test then the whole process is executed, integration tests are triggered and the war file is generated. The problem is that, as I use 'integration-test' profile (because I use a different config.properties to execute the integration tests), the final war file contains the properties files with the wrong values, they have the 'integration-test' properties instead of 'production' properties.
If I manually activate the 'production' profile (with Eclipse) and execute: mvn war:war then the war file for production is successfully generated, but integration tests are not triggered.
What I'm trying to do is combine these 2 steps in one single step, I mean, execute the whole process to perform the integration tests with 'integration-test' profile and have the war file generated with the 'production' profile.
I feel I am missing something about Maven and hope you can guide me.
Thank you.
You could insert <skip.integration.tests>false</skip.integration.tests> in your prod profile. If you don't want the integration-tests to be executed every time you build with your prod profile, you can manually set this property on the command line with
mvn install -Pprod -Dskip.integration.tests=false

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

Maven - skip plugin if property is empty/null

I want to obtain the following behavior: when I specify a value for the property "my.prop", I want the dependency and clean plugins to be executed. If a value is not specified for that property, I want them to be skipped.
I created "my.prop" like this:
<properties>
<my.prop></my.prop>
</properties>
Then I read that profile activation works only for system properties, so I deleted the above and used the surefire plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<systemPropertyVariables>
<my.prop></my.prop>
</systemPropertyVariables>
</configuration>
</plugin>
I tried using profiles, like this:
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<skipDependecyAndCleanPlugins>false</skipDependecyAndCleanPlugins>
</properties>
</profile>
<profile>
<id>skip-dependency-and-clean-plugins</id>
<activation>
<property>
<name>my.prop</name>
<value></value>
<!-- I also tried: <value>null</value> without success.-->
</property>
</activation>
<properties>
<skipDependecyAndCleanPlugins>true</skipDependecyAndCleanPlugins>
</properties>
</profile>
</profiles>
Later, for each plugin I do something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version>
<configuration>
<skip>${skipDependecyAndCleanPlugins}</skip>
</configuration>
....
</plugin>
But the plugins are still executed...
How can I determine Maven to skip the executions of the plugins when "my.prop" is empty/null?
The simplest solution would be to use the activation in the following form:
<profiles>
<profile>
<activation>
<property>
<name>debug</name>
</property>
</activation>
...
</profile>
</profiles>
The above means you can define any value for debug which means -Ddebug is enough.
An empty value can't be defined a pom file cause <value></value> is equivalent to <value/> which means the same as not defined.
Update:
I would suggest to use a profile and NOT a property. So you can simply define on command line mvn -Pxyz install or leave it.
You can use my.prop property in plugin's configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version>
<configuration>
<skip>${my.prop}</skip>
</configuration>
....
</plugin>
Now when you execute:
mvn ... -Dmy.prop=true
then plugin will be skipped
You were very close. You can achieve what you described by using the !my.prop syntax in profile activation.
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<configuration>
<skip>${skipDependecyAndCleanPlugins}</skip>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>skip-dependency-and-clean-plugins</id>
<activation>
<property>
<name>!my.prop</name>
</property>
</activation>
<properties>
<skipDependecyAndCleanPlugins>true</skipDependecyAndCleanPlugins>
</properties>
</profile>
</profiles>
According to Maven documentation, the skip-dependency-and-clean-plugins profile will be activated when the system property my.prop is not defined at all.
Here is a solution that directly addresses the OP's original request: the ability to skip a plugin's execution if a POM property (not a system property) my.prop is not defined. This solution relies on the Apache Maven Help Plugin. It is a kludge, but given Maven's paucity of expression prowess, this is about the best you're gonna get. At least it relies on a well-known, hopefully-maintained plugin, and should work 100% of the time. Oh, and it may make your head explode. Or make you cry. Or both. You've been warned.
First declare the latest version of the Maven Help Plugin in the <build><pluginManagement> section:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
</plugin>
Then add this "secret sauce" in the <build><plugins> section, which will check to see if my.prop is defined:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>set-is-skip-true-or-prefixed</id>
<phase>validate</phase>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>is-skip</name>
<value>_${my.prop}</value>
<regex>_\$\{my.prop\}</regex>
<replacement>true</replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
<execution>
<id>set-is-skip</id>
<phase>initialize</phase>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>is-skip</name>
<value>${is-skip}</value>
<regex>_.*</regex>
<replacement>false</replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
Now you have a POM property (not a system property) named is-skip, which you can use in any later phase to disable a plugin—provided that plugin has a <skip> or similar option taking a Boolean value. If my.prop is not defined at all, is-skip will be set to true; otherwise is-skip will be set to false.
<plugin>
<groupId>org.example</groupId>
<artifactId>foobar-plugin</artifactId>
<executions>
<execution>
<id>foo</id>
<phase>package</phase>
<goals>
<goal>foo</goal>
</goals>
<configuration>
<skip>${is-skip}</skip>
</configuration>
</execution>
</executions>
</plugin>
There is one caveat: don't define my.prop at all, even to the empty string, or this solution will consider it defined and set is-skip to false. Setting is-skip to true if my.prop is set but empty would require an additional regex evaluation clause above. For my use case I didn't need it, as an empty my.prop isn't valid and I didn't define my.prop at all in the parent POM.
I'll leave it to you to understand how it works (my head exploded already when I was writing it), but I'll be happy to answer any questions.
In step #2 above, be sure and put the two regex evaluations in different phases, such as validate and initialize as used here. The reason is because Maven gets confused if you mix in the same plugins in in the same phase a child POM, and may scramble the execution order. (See MNG-5987.) My kludge of a solution relies on the order of evaluation.

Maven not detecting existing file

I'm writing a pom file to conditionally checkout or update a subdirectory from git. However, it always does a clean checkout. I'm doing this to wrap CI scripts around existing projects without having to change them.
Here's the code (slightly censored, and with the update ommitted):
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>standard-php-project</artifactId>
<version>1.0</version>
<properties>
<git.project>Test/Project</git.project>
<git.project.checkout.directory>${basedir}/src/php/main/${git.project}</git.project.checkout.directory>
<git.project.checkout.exists.file>${git.project.checkout.directory}/.git/index</git.project.checkout.exists.file>
</properties>
<scm>
<connection>scm:git:ssh://server/git/${git.project}</connection>
</scm>
<profiles>
<profile>
<id>scm-checkout</id>
<activation>
<file>
<missing>${git.project.checkout.exists.file}</missing>
</file>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>maven-echo-plugin</artifactId>
<version>0.1</version>
<executions>
<execution>
<id>echo-missing-file</id>
<phase>generate-sources</phase>
<goals>
<goal>echo</goal>
</goals>
<configuration>
<echos>
<echo>Couldn't find ${git.project.checkout.exists.file}</echo>
</echos>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.8.1</version>
<executions>
<execution>
<id>scm-generate-sources-phase</id>
<phase>generate-sources</phase>
<goals>
<goal>checkout</goal>
</goals>
<configuration>
<checkoutDirectory>${git.project.checkout.directory}</checkoutDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<!-- And another profile for when the file exists, not shown for brevity -->
</profiles>
</project>
I've run mvn compile which tells me the file it tests for, done ls -l on the file to verify it exists, and then run again. For some reason, the test fails.
Help!
Profiles are determined prior to applying properties from the pom
<missing>${git.project.checkout.exists.file}</missing> won't work from the value in your pom.xml
If it was provided on commandline then I believe it would work
Otherwise you need to include the value directly
<missing>/src/php/main/Test/Project/.git/index</missing>
See also Maven profile by user defined property

Resources