How to override Maven 3.0 parent profile properties from child pom? - maven-3

How to override Maven 3.0 parent profile properties from child pom?
I want to be override profile properties from a parent pom. I've used help:effective-pom -Pjmeter and can see that the child properties are not being picked up and tried many various permutations all without success. I expect the parents properties to be overridden by the child properties.
Parent pom profile:
<profile>
<id>jmeter</id>
<properties combine.self="override">
<maven.jmeter.phase>verify</maven.jmeter.phase>
<maven.jmeter.goal>jmeter</maven.jmeter.goal>
<!-- We use the ${basedir} to avoid NullPointer errors when src/test/jmeter doesn't exist -->
<!-- when running jmeter test the default to set in the child pom is ${basedir}/src/test/jmeter -->
<maven.jmeter.testFilesDirectory>${basedir}</maven.jmeter.testFilesDirectory>
<maven.jmeter.jMeterTestFile>**/*.jmx</maven.jmeter.jMeterTestFile>
<maven.jmeter.excludeJmeterTestFile>NOT_NULL</maven.jmeter.excludeJmeterTestFile>
<maven.jmeter.testResultsTimestamp>false</maven.jmeter.testResultsTimestamp>
<maven.jmeter.server>localhost</maven.jmeter.server>
<maven.jmeter.port>8080</maven.jmeter.port>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>${jmeter.version}</version>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>${maven.jmeter.phase}</phase>
<goals>
<goal>${maven.jmeter.goal}</goal>
</goals>
<configuration>
<testFilesDirectory>${maven.jmeter.testFilesDirectory}</testFilesDirectory>
<testFilesIncluded>
<jMeterTestFile>${maven.jmeter.jMeterTestFile}</jMeterTestFile>
</testFilesIncluded>
<testFilesExcluded>
<excludeJmeterTestFile>${maven.jmeter.excludeJmeterTestFile}</excludeJmeterTestFile>
</testFilesExcluded>
<testResultsTimestamp>${maven.jmeter.testResultsTimestamp}</testResultsTimestamp>
<propertiesUser>
<!-- server and port must be defined (and used) in the JMeter jmx file as User Defined Variables
${__P(server,localhost)}
${__P(port,80)}
See http://jmeter.apache.org/usermanual/component_reference.html#User_Defined_Variables
http://jmeter.apache.org/usermanual/test_plan.html#using_variables
http://jmeter.apache.org/usermanual/functions.html#__P
http://jmeter.apache.org/usermanual/best-practices.html#parameterising_tests
-->
<server>${maven.jmeter.server}</server>
<port>${maven.jmeter.port}</port>
</propertiesUser>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
and then in the child pom:
<profile>
<id>jmeter</id>
<activation><activeByDefault>false</activeByDefault></activation>
<properties>
<maven.jmeter.testFilesDirectory>${project.basedir}/src/test/jmeter</maven.jmeter.testFilesDirectory>
<!-- csv based JMeter tests result in one graph in Jenkins, we want a graph per test -->
<maven.jmeter.excludeJmeterTestFile>**/KRAD.jmx</maven.jmeter.excludeJmeterTestFile>
</properties>
</profile>

I'm not sure properties are overridden that way. Either way, the plugin will not run as you have it, since you only define <pluginManagement> and no straight direct <plugins> child under <build>. If you don't want your plugin to run in the parent, just define the <plugins> tags in the children where you do want this running as such:
<profile>
<id>jmeter</id>
<activation><activeByDefault>false</activeByDefault></activation>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<configuration>
<testFilesDirectory>${maven.jmeter.jMeterTestFile}</testFilesDirectory>
<!-- csv based JMeter tests result in one graph in Jenkins, we want a graph per test -->
<testFilesExcluded>
<excludeJmeterTestFile>${maven.jmeter.excludeJmeterTestFile}</excludeJmeterTestFile>
</testFilesExcluded>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Hope this helps.

Related

Donut report (donut-maven-plugin) is not getting generated when some of the tests fails

I am using Donut report (donut-maven-plugin) for aggregating a report for parallel cucumber tests ran using cucumber-jvm-parallel-plugin from github.temyers team.
Here is my POM snippet:
<profiles>
<profile>
<id>TestSuite1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<app.config>//src//test//java//envConfig//localGridConfig.properties</app.config>
<test.tag>#android</test.tag>
<dummy.tag>#dummy</dummy.tag>
<device.name>browserstack.ios</device.name>
<!--<target.env>browserStackEnv</target.env>-->
</properties>
</profile>
<profile>
<id>TestSuite2</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<app.config>//src//test//java//envConfig//sauceLabsGridConfigAndroid.properties</app.config>
</properties>
</profile>
<profile>
<id>TestSuite3</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<app.config>//src//test//java//envConfig//sauceLabsGridConfigIOS.properties</app.config>
</properties>
</profile>
</profiles>
<build>
<!--<sourceDirectory>src</sourceDirectory>-->
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20</version>
<configuration>
<parallel>methods</parallel>
<threadCount>5</threadCount>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<forkCount>5</forkCount>
<reuseForks>true</reuseForks>
<includes>
<include>**/Parallel*IT.class</include>
</includes>
<systemPropertyVariables>
<deviceName>${device.name}</deviceName>
<targetEnv>${target.env}</targetEnv>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>com.github.temyers</groupId>
<artifactId>cucumber-jvm-parallel-plugin</artifactId>
<version>4.2.0</version>
<executions>
<execution>
<id>generateRunners</id>
<phase>generate-test-sources</phase>
<!--<phase>validate</phase>-->
<goals>
<goal>generateRunners</goal>
</goals>
<configuration>
<!-- Mandatory -->
<!-- List of package names to scan for glue code. -->
<glue>
<package>stepDefs</package>
</glue>
<!-- These are optional, with the default values -->
<!-- Where to output the generated tests -->
<outputDirectory>${project.build.directory}/cucumber-parallel/html</outputDirectory>
<!--<outputDirectory>${project.build.directory}/generated-test-sources/cucumber</outputDirectory>-->
<!-- The directory, which must be in the root of the runtime classpath, containing your feature files. -->
<featuresDirectory>src/main/resources/features/</featuresDirectory>
<!-- Directory where the cucumber report files shall be written -->
<!--<cucumberOutputDir>target/cucumber-parallel</cucumberOutputDir>-->
<cucumberOutputDir>target/cucumber-parallel</cucumberOutputDir>
<!-- List of cucumber plugins. When none are provided the json formatter is used. For more
advanced usage see section about configuring cucumber plugins -->
<format>json,html,rerun</format>
<plugins>
<plugin>
<name>json</name>
<extension>json</extension>
<!--Optional output directory. Overrides cucumberOutputDirectory. Usefull when different
plugins create files with the same extension-->
<outputDirectory>${project.build.directory}/cucumber-parallel/json</outputDirectory>
</plugin>
<!--<plugin>-->
<!--<name>com.example.CustomHtmlFormatter</name>-->
<!--<extension>html</extension>-->
<!--</plugin>-->
<plugin>
<name>com.cucumber.listener.ExtentCucumberFormatter</name>
<extension>html</extension>
</plugin>
</plugins>
<customVmTemplate>
src/main/resources/cucumber-extents-report-runner.java.vm
</customVmTemplate>
<!-- CucumberOptions.strict property -->
<strict>true</strict>
<!-- CucumberOptions.monochrome property -->
<monochrome>true</monochrome>
<!-- The tags to run, maps to CucumberOptions.tags property. Default is no tags. -->
<tags>
<tag>
<!--${dummy.tag}-->
</tag>
</tags>
<!-- Generate TestNG runners instead of JUnit ones. -->
<useTestNG>false</useTestNG>
<!-- The naming scheme to use for the generated test classes. One of 'simple' or 'feature-title' -->
<namingScheme>simple</namingScheme>
<!-- The class naming pattern to use. Only required/used if naming scheme is 'pattern'.-->
<!--<namingPattern>**/Parallel*IT.class</namingPattern>-->
<namingPattern>Parallel{c}IT</namingPattern>
<!-- One of [SCENARIO, FEATURE]. SCENARIO generates one runner per scenario. FEATURE generates a runner per feature. -->
<parallelScheme>SCENARIO</parallelScheme>
<!--<parallelScheme>FEATURE</parallelScheme> <!–Using Feature for accomodating Scenario Outline –>-->
<!-- Specify a custom template for the generated sources (this is a path relative to the project base directory) -->
<!--<customVmTemplate>src/test/resources/cucumber-custom-runner.vm</customVmTemplate>-->
<!-- Specify a custom package name for generated sources. Default is no package.-->
<packageName>
</packageName>
</configuration>
</execution>
</executions>
</plugin>
<!-- Cucumber report merger
-->
<plugin>
<groupId>report.donut</groupId>
<artifactId>donut-maven-plugin</artifactId>
<version>0.0.6</version>
<executions>
<execution>
<id>execution</id>
<phase>post-integration-test</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<sourceDirectory>${project.build.directory}/cucumber-parallel</sourceDirectory>
<outputDirectory>${project.build.directory}/TrackMergeReport</outputDirectory>
<timestamp>${maven.build.timestamp}</timestamp>
<template>default</template>
<projectName>NativeAppsAutomation</projectName>
<!-- optional -->
<customAttributes>
<customAttribute>
<name>App Name</name>
<!--<value>${app.name}</value>-->
<value>smartphone.editor.beta</value>
</customAttribute>
<customAttribute>
<name>Device Name</name>
<!--<value>${app.name}</value>-->
<value>${device.name}</value>
</customAttribute>
<customAttribute>
<name>Target Env</name>
<value>${target.env}</value>
</customAttribute>
</customAttributes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Note:
- There's no trace/mention about attempt to generate Donut report in maven run log (when one of the tests fail):
- I use mvn clean verify or mvn clean integration-test to run the framework
Run 1: PASS
Run 2: PASS
Run 3: Can't locate an element by this strategy: Locator map:
- native content: "By.chained({By.xpath: //*[contains(#text,'rints')]})"
- html content: "By.cssSelector: div[data-id='print'] > .caption"
Tests run: 51, Failures: 0, Errors: 9, Skipped: 5
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13:49 min
[INFO] Finished at: 2018-01-05T14:54:50+01:00
[INFO] Final Memory: 27M/306M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test
(default-test) on project NativeAppsAutomation-project: There are test
failures.
[ERROR]
[ERROR] Please refer to /../target/surefire-reports
for the individual test results.
[ERROR] -> [Help 1]
Noticed that - individual plain Cucumber reports (json, Html, xml) are getting generated in the target/cucumber-parallel folder as expected for both All pass & runs with failures or all failures.
Whereas , when all tests pass then Donut aggregate report is getting generated,

filter maven properties using profile-dependent properties file

I'm using the properties-maven-plugin to load properties from a custom properties file. The properties file name depends on the selected build profile.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>src/main/vpn/vpn-${environment}.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
Each vpn-${environment}.properties file contains two properties, e.g. for dev environment:
vpn.username=dev_user
vpn.password=dev_password
Two profiles default and dev have been defined:
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<environment>default</environment>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<environment>dev</environment>
</properties>
</profile>
</profiles>
What I'm trying to do now is to use the maven-resources-plugin to replace the vpn property placeholders used within a unix template file with the property values of the corresponding active profile and to copy this file to another location.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions> <execution>
<id>copy-script</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>src/main/vpn/bin</outputDirectory>
<resources>
<resource>
<directory>${basedir}/unix-template</directory>
<filtering>true</filtering>
<includes>
<include>customUnixTemplate</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
The customUnixTemplate file contains something like vpn-connect ${vpn.username} ${vpn.password}.
After I build the project with mvn clean install -Pdev and open the output file in the src/main/vpn/bin directory I can see for a split second that the properties were taken from the correct properties file, i.e. vpn-dev.properties, but the file gets overwritten by the default properties file and I can't figure out why. I suspect that it has something to do with the build phases/orders in which the plugins are executed. Can anybody help?
Edit in response to Andrei's comment:
The maven resource plugin uses a second execution:
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.artifactId}/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>log4j2.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>

maven-dependency-plugin:unpack Error

I'm trying to extract some .exe files from a dependency jar file and put them under ${project.build.directory}/classes/.
But when I execute:
mvn clean compile dependency:unpack
I get:
Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.10:unpack (default-cli) on project simple: Either artifact or artifactItems is required -> [Help 1
I have verified that the dependencies are available in my local repository.
In my example pom below I've used junit as an example, but no matter which dependency I list, I get the same error.
pom.xml:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/classes/externaltools</outputDirectory>
<includes>**/*.txt</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
The issue is due to you cannot use mvn clean compile dependency:unpack and <executions> tags together.
In documentation Maven Depdendency Plugin at the bottom part of the page you can read:
If you intend to configure this mojo for execution on the command line using: mvn dependency:unpack you must not put the configuration inside the executions tag. Your configuration should look like this:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<configuration>
<artifactItems>
<artifactItem>
<groupId>[ groupId ]</groupId>
<artifactId>[ artifactId ]</artifactId>
<version>[ version ]</version>
<type>[ packaging ]</type>
<classifier> [classifier - optional] </classifier>
<overWrite>[ true or false ]</overWrite>
<outputDirectory>[ output directory ]</outputDirectory>
<destFileName>[ filename ]</destFileName>
<includes>[ comma separated list of file filters ]</includes>
<excludes>[ comma separated list of file filters ]</excludes>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
I have tried removing the <execution> tags and works perfectly!

Maven Javadoc Plugin During Site Goal: javadoc: error - java.lang.OutOfMemoryError: Please increase memory

I have been struggling to increase the memory of my javadoc plugin via my pom file. For some reason my Mac build slave fails during the site goal with an OutOfMemoryError. I tried adjusting the maxmemory of the javadoc plugin via my pluginManagement section of the pom (as per the maven-javadoc-plugin documentation):
...
<build>
...
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<maxmemory>512m</maxmemory>
</configuration>
</plugin>
</plugins>
<pluginManagement>
...
</build>
...
This didn't seem to help anything, my build still failed with an out of memory error.
So I decided to put this directly in my plugin declaration instead (see the build-plugins-plugin section below:
...
<build>
...
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8.1</version>
</plugin>
...
</plugins>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<maxmemory>512m</maxmemory>
</configuration>
</plugin>
...
</plugins>
...
</build>
...
...and yet this is still not working, the Mac build slave still fails during the site goal.
It turned out I was being an idiot. The site goal uses the plugin defintions declared in the reporting section of the pom. Therefore I had to add my max memory configuration here instead, see below:
...
<build>
...
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8.1</version>
</plugin>
</plugins>
<pluginManagement>
...
</build>
...
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<maxmemory>512m</maxmemory>
</configuration>
</plugin>
</plugins>
</reporting>
...
Based on this result, it is my assumption that Maven will not pick up configuration details defined in the build-pluginManagement section of the pom for plugins defined in the reporting section. Cheers.

Maven3 profile with custom enforcer rule, is it possible?

I wrote and tested a custom enforcer rule to do distribution specific builds on various Linux distributions. It tests well with the mvn enforcer:enforce command, with the provided pom.xml build snippet.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.0-beta-1</version>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>maven-enforcer-rule-redhat6x</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<configuration>
<rules>
<redhat6x implementation="org.example.enforcer.Redhat6x">
<compatible>true</compatible>
</redhat6x>
</rules>
</configuration>
<executions>
<execution>
<id>enforce</id>
</execution>
</executions>
<goals>
<goal>enforce</goal>
</goals>
</plugin>
</plugins>
After racking my brain and doing a lot of experimental testing, I cannot seem to find how to use this custom enforcer rule as a profile activation selector.
<profiles>
<profile>
<id>RedHat6x</id>
<activation>
<!-- Something goes here, but what? This doesn't work -->
<redhat6x implementation="com.cisco.tesdw.enforcer.Redhat6x">
<compatible>true</compatible>
</redhat6x>
</activation>
</profile>
</profiles>
There are some hints that profile activation uses maven-enforcer-rules as detailed in the Introduction to Profiles page under the section "How can I tell which profiles are in effect during a build" section. Namely, every profile activation which has multiple string values (os name, etc) is referred to the corresponding maven enforcer rule. However, it seems that direct inclusion of a custom profile activator isn't obvious in the pom.xml, and adding such would likely require a pom version update.
Maven3 is also extensible in ways that are very flexible, is it possible to hook inclusion of my enforcer rule by the maven extensions mechanism? There is documentation on how to include custom lifecycle participants; but, I fear that profile activation may have already occurred by the time the build starts. Documentation is sparse, but the javadoc indicates that AbstractMavenLifecycleParticipant.afterProjectsRead(MavenSession session) is called "after all MavenProject instances have been created". This leaves some doubt in my mind whether it is called is before of after profile activation. I suspect after, or how would one get a properly configured MavenProject?
Could someone tell me if profile activation customization is even remotely possible?
You just need to move plugin configuration under profile:
<profiles>
<profile>
<id>RedHat6x</id>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.0-beta-1</version>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>maven-enforcer-rule-redhat6x</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<configuration>
<rules>
<redhat6x implementation="org.example.enforcer.Redhat6x">
<compatible>true</compatible>
</redhat6x>
</rules>
</configuration>
<executions>
<execution>
<id>redhat-enforce</id>
</execution>
</executions>
<goals>
<goal>enforce</goal>
</goals>
</plugin>
</plugins>
</profile>
</profiles>

Resources