override property value on pom.xml - maven

A part of code in my pom.xml looks like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${suite.xml}</suiteXmlFile>
</suiteXmlFiles>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter
</value>
</property>
</properties>
<testFailureIgnore>true</testFailureIgnore>
<systemProperties>
<property>
<name>org.uncommons.reportng.title</name>
<value>${title}</value>
</property>
</systemProperties>
<argLine>-Xms128m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=256m
-XX:+UseConcMarkSweepGC
</argLine>
</configuration>
</plugin>
I am sending -Dtitle from my jenkins job and it's working fine. Like wise I want to change the value in tag <testFailureIgnore>true</testFailureIgnore>. But the problem here is: I don't want to change the default value. I just want to parameterize this only in case if user provide "false" value from jenkins. If user do not provide anything then it should be "true".

Create a separate profile in pom where you can provide different values.
Plugin definition in pom will contain default value.
So if you run simple maven command : mvn install it will use default value
and if you run using profile that is mvn -Pprofile install then it will use value specified in profile.

Add one property <maven.test.failure.ignore>true</maven.test.failure.ignore> under <properties> tag of your pom.
Replace line testFailureIgnore line by <testFailureIgnore>${maven.test.failure.ignore}</testFailureIgnore>.
In case of "false" you need to pass one extra parameter -Dmaven.test.failure.ignore=false

It easy. First you have to add a shell comand to your job. In this command you could make a sed comand an replace this line on the other.
Example:
sed -i 's/ <maven.test.failure.ignore>true</maven.test.failure.ignore> /<maven.test.failure.ignore>false</maven.test.failure.ignore>/g' pom.xml

Related

How to pass parameter to maven test

I have One test suite running in two environment.
Sometimes, I would like to run tests in localhost:8080 and sometimes at localhost:8585.
Jenkins run the tests by "mvn test" command.
How could I pass the port by parameter? Something like "mvn test 8080".
I add a plugin on maven pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties>
<property>
<name>fileName</name>
<value>${fileName}</value>
</property>
</systemProperties>
</configuration>
</plugin>
And get the parameter in junit code with
String fileName = System.getProperty("fileName");
After, I run my tests with -DfileName argument
mvn clean test -DfileName="config-test.xml"
Now, I can put all configurations in xml file and load appropriate file with the corrects parameters.
mvn clean test -DfileName="config-test.xml"
or
mvn clean test -DfileName="config-homolog.xml"
I solved the problem with the tips from Sandra Sukarieh and http://syntx.io/how-to-pass-parameters-to-the-junit-tests-from-the-maven-surefire-plugin/
Thank you very much
try this:
mvn -Dtest=testName -Dargline="-Dport=portValue"
and portValue will be either 8080 or 8585, and while you have a "port" variable declared in your test code.
After doing some research, I found the below code:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties>
<property>
<suiteXmlFiles>
<suiteXmlFile>${testsuite}</suiteXmlFile>
</suiteXmlFiles>
</property>
</systemProperties>
</configuration>
</plugin>
Please run the maven command:
mvn test -Dtestsuite =yourxmlsuitepath

Mockito, jacoco and surefire causes out of memory

I am using mockito 1.8.3, jacoco 0.72 and maven 3.0.5 surefire plugin (2.12.4) to execute unit test and generating coverage report, it was working fine.
With more and more tests are added, it starts not working. I continuously encounter out of memory error during test execution, and cannot find out a way to figure out what is wrong.
I have about 1800+ test cases with mockito as the mocking tool. It is working fine if I do not run jacoco during maven test with "org.jacoco:jacoco-maven-plugin:prepare-agent " before test phase, but as long as I add jacoco agent, I get OOO issue with PermGen full.
I already added the PermGen to 2GB by modifying MAVEN_OPTS (which should not work since surefire will fork a new process) and surefire argline argument in pom, but it does not help a lot.
I try to get a core dump when OOO occurs by adding parameter to surefire plugin, but never saw a dump file in any folder. I am suspicious that my JVM setting does not work for the surefire plugin, but not sure what is wrong. Anyone could do me a favor? Thanks.
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<inherited>true</inherited>
<configuration>
<properties>
<property>
<name>argLine</name> <value>-server -ea -XX:-UseSplitVerifier -XX:MaxPermSize=2g -Xmx3g -XX:+HeapDumpOnOutOfMemoryError </value>
</property>
<property>
<name>forkMode</name>
<value>once</value>
</property>
<property>
<name>reportFormat</name>
<value>plain</value>
</property>
<property>
<name>skipTests</name>
<value>${maven.test.skip}</value>
</property>
</properties>
</configuration>
</plugin>
You need to set the memory for maven-surefire-plugin like the following:
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<forkCount>3</forkCount>
<reuseForks>true</reuseForks>
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
<systemPropertyVariables>
<databaseSchema>MY_TEST_SCHEMA_${surefire.forkNumber}</databaseSchema>
</systemPropertyVariables>
</configuration>
</plugin>
[...]
</plugins>
In case you have jacoco configured along with maven failsafe plugin, then you will need to pass memory parameters to that one too:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
</configuration>
</plugin>

How to pass environment variable to process started as mvn jetty:run?

The process started by maven jetty plugin seems ignoring any environment variables I specify.
So far I've tried adding variable through command line like:
set myvariable=1
Also I've tried adding something like "-Dmyvariable=1" to MAVEN_OPTS variable.
Nothing helps.
Just to be clear, I need to pass variable not to maven but to the resulting process, i.e. directly to jetty server.
You need to specify a systemProperties section in the plugin's configuration:
<project>
...
<plugins>
...
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
...
<systemProperties>
<systemProperty>
<name>myvariable</name>
<value>1</value>
</systemProperty>
...
</systemProperties>
</configuration>
</plugin>
</plugins>
</project>

Using custom reporters with the maven surefire plugin

I'm trying to use a custom reporter for TestNG with the maven surefire plugin. I already have a custom listener and that seems to get picked up. However, it doesn't look like the custom reporter is being used at all:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
<properties>
<property>
<name>listener</name>
<value>com.mystuff.test.TestNGListener</value>
</property>
<property>
<name>reporter</name>
<value>com.mystuff.test.MyEmailableReporter</value>
</property>
</properties>
</configuration>
</plugin>
Any idea why this is happening?
I figured this out. It looked like the following doesn't work at all:
<property>
<name>reporter</name>
<value>com.mystuff.test.MyEmailableReporter</value>
</property>
in spite of documentation to the contrary. In the TestNG class it appears that there is a method called TestNG#addListener(IReporter listener), which contrary to its name, accepts a report which implements IReporter. Maven Surefire (v2.12.1) calls this method to add listeners and reports. However, it doesn't look for reports under a property with name reporter. Instead, you have to add your custom reporter to the listener property:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
<properties>
<property>
<name>listener</name>
<value>com.mystuff.test.TestNGListener,com.mystuff.test.MyEmailableReporter</value>
</property>
</properties>
</configuration>
</plugin>
Not very intuitive.

set java system property during maven 2 compile?

I have a maven profile and want to set a property which is later on available per System.getProperty(..) in java:
<profile>
<id>local-dev</id>
<properties>
<my.comp.my.prop>myValue</my.comp.my.prop>
</properties>
</profile>
I want System.getProperty("my.comp.my.prop") to be "myValue" but it's null..
How do I set it correctly? :)
Thansk!
properties-maven-plugin plugin will help you to do exactly what you're looking for:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<goals>
<goal>set-system-properties</goal>
</goals>
<configuration>
<properties>
<property>
<name>my.property.name</name>
<value>my.property.value</value>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin>
maven cannot set a property which can be accessed by your application from the environment at runtime.
Instead, you can use maven to update a property file in your codebase during build time, which can then be read by your application at runtime. Different values of the property can be set based on the profile, thereby allowing your application to have different values as desired.
Alternately, you can invoke the application setting the desired property in the environment manually (outside maven).

Resources