I am using maven-surefire-plugin + Sonar together and I would like to add some extra value to argLine parameter of the maven-surefire-plugin.
So I did it:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<argLine>-DCRR.Webservice.isSimulated=true -D...</argLine>
</configuration>
</plugin>
...
</plugins>
</build>
But in this case I am overwriting the original value of the argLine parameter and Sonar does not generate jacoco.exec file.
I can see in the maven debug log (-X) that the value of argLine param without overwriting its value is -javaagent:/opt/jenkins/.../myproject-SONAR/.repository/org/jacoco/org.jacoco.agent/0.7.4.201502262128/org.jacoco.agent-0.7.4.201502262128-runtime.jar=destfile=/opt/jenkins/.../myproject-SONAR/target/jacoco.exec.
What is the proper way to APPEND the original value of this parameter (keep the original + add extra values)?
I am using Apache Maven 3.5.0, Java version: 1.8.0_131, vendor: Oracle Corporation.
The official documentation calls that late replacement.
If you do the following you will overwrite the value of the argLine parameter which is set by another plugins before, so DO NOT DO THIS:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-D... -D...</argLine>
</configuration>
</plugin>
The proper way to keep the existing values and add your configuration is to use #{...} syntax:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>#{argLine} -D... -D...</argLine>
</configuration>
</plugin>
OR you can set argLine as a property in your pom.xml file:
<properties>
<argLine>-DCRR.Webservice.isSimulated=true -D...</argLine>
</properties>
Both solutions above works properly.
Update for Apache Maven 3.8.3.
In my case only combination of both #zapee suggestions works, in other words it's important to add <argLine/> to <properties> and #{argLine} to configuration section. Example:
<properties>
<!-- This is required for later correct replacement of argline -->
<argLine/>
</properties>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>#{argLine} -D... -D...</argLine>
</configuration>
</plugin>
Hope, it helps somebody.
Thanks!
In my case it was:
<argLine>${tycho.testArgLine} -D...</argLine>
Related
Because I need to customize Host header in HTTP request, I need to start my Spring Boot Java app with following argument (available since JDK 12):
java -jar -Djdk.httpclient.allowRestrictedHeaders=host application.jar
but how to pass it into maven pom.xml file to be able to use this argument durring tests which are failing because of missing this flag?
I tried to use maven-compiler-plugin in following way:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>-Djdk.httpclient.allowRestrictedHeaders=host</arg>
</compilerArgs>
</configuration>
</plugin>
but it's wrong:
error: invalid flag: -Djdk.httpclient.allowRestrictedHeaders=host
Following examples are not working either:
-jdk.httpclient.allowRestrictedHeaders=host
jdk.httpclient.allowRestrictedHeaders=host
So i tried even with spring-boot-maven-plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Djdk.httpclient.allowRestrictedHeaders=host</jvmArguments>
</configuration>
</plugin>
but it's also not working because in that case this flag is ignored and I got restriction error when I run mvn test. Which is not happening when I run java with this flag.
You seem to be configuring the wrong plugin. You said you need to "be able to use this argument during tests" which means you should be configuring Maven Surefire Plugin.
Have a look at the example they have provided. May be you can use systemProperties:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<systemProperties>
<property>
<name>propertyName</name>
<value>propertyValue</value>
</property>
[...]
</systemProperties>
</configuration>
</plugin>
or the argLine approach:
<argLine>-Djava.endorsed.dirs=...</argLine>
I try to implement sonar+failsafe+jacoco plugins. I added theese to my pom.xml file
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-plugin-api</artifactId>
<version>7.5</version>
<scope>provided</scope>
</dependency>
and I added this plugins to the same file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<argLine>--add-modules java.base</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<propertyName>jacoco.agent.argLine</propertyName> <!-- default: argLine -->
<includes>
<include>**/*IT.java</include>
</includes>
<destFile>${project.build.directory}/jacoco-it.exec</destFile> <!-- agent -->
<dataFile>${project.build.directory}/jacoco-it.exec</dataFile> <!-- report -->
</configuration>
<executions>
<execution>
<id>agent</id>
<goals><goal>prepare-agent</goal></goals>
</execution>
</executions>
</plugin>
and It is already exists in my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>${it.skip}</skip>
<argLine>--add-modules java.base</argLine>
<forkCount>4</forkCount>
<reuseForks>true</reuseForks>
<includes>
<include>*IT</include>
</includes>
<systemPropertyVariables>
<doc.skip>${doc.skip}</doc.skip>
</systemPropertyVariables>
</configuration>
</plugin>
and I run mvn clean verify -P integration-test and then mvn sonar:sonar
but I still get a coverage is 0.0% Why did not get IT's reports results? Please help me.. What is my issue?
I saw a mistake, which I think is the cause of your problem:
In the configuration for jacoco-maven-plugin you overrid the parameter that would receive the jvm arguments set by jacoco to communicate with maven surefire, with <propertyName>jacoco.agent.argLine</propertyName>. That means that it will fill in this property instead of the default property argLine (which is fine but not necessary I guess).
In the configuration for maven-surefire-plugin, you overrid the <argLine>--add-modules java.base</argLine> (which by default would have taken the value ${argLine}) without refering to the property jacoco.agent.argLine.
You need something like <argLine>${jacoco.agent.argLine} --add-modules java.base</argLine> or <argLine>#{jacoco.agent.argLine} --add-modules java.base</argLine> (with an #) depending on your jacoco version.
If using $and not #, you may need to define an empty <jacoco.agent.argLine/> property on top of the pom file.
How do I set in the pom to not compile tests in Maven? I've tried:
<properties>
<skipTests>true</skipTests>
</properties>
but in that case, Maven compile the tests but don't run them. I need Maven don't compile my tests.
You have to define maven.test.skip to true.
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
http://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-test.html
In my case a solution was to put tests in a profile (e.g. runTests), so when I want to run these tests, I add the parameter -PrunTests. Thanks for the replies.
Configure maven-compiler-plugin to skip the compilation.
Once again, I do not recommend it.
<project>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<skip>${maven.test.skip}</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
If you are using the surefire-plugin for executing tests, you can configure it to skip them based on a naming pattern:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14</version>
<configuration>
<includes>
<include>%regex[.*[Cat|Dog].*Test.*]</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
This, however, requires the tests file names to conform to the desired pattern(s). At work we are using this approach, and have our tests end with ..UnitTest or ..IntegrationTest, so that we can easily turn each of them off by modifying the regex in the corresponding build profile.
Take a look at Apache's documentation on the surefire plugin. You may find something more useful or better suited for your case.
What incantation do I put into pom.xml which will be equivalent to
export JAVA_TOOL_OPTIONS='-Dfile.encoding=UTF-8'
in .profile?
I tried
<configuration>
<file.encoding>UTF-8</file.encoding>
</configuration>
in maven-compiler-plugin and
<properties>
<file.encoding>UTF-8</file.encoding>
</properties>
in top level and it did not work.
The main idea is to set this from pom.xml, NOT from the environment
(I have no control over what environment this will be run under).
Again, I am not interested in any solution which modifies .profile et al.
The default solution is to use the following:
<project>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
...
</project>
and the maven-compiler-plugin has a different configuration and in particular uses the above property.
Try the following:
<configuration>
...
<systemProperties>
<systemProperty>
<name>propertyName</name>
<value>propertyValue</value>
</systemProperty>
...
</systemProperties>
</configuration>
This should work. At least it works for me with other maven plugins.
If you want to specify the encoding on unit test level you can use the following solution.
This is set on startup of the plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>${skip.unit.tests}</skipTests>
<enableAssertions>true</enableAssertions>
<argLine>${surefireArgLine} -Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
I have projects that need to be build with a specific version of the JDK.
The problem isn't in the source and target parameters but in the jars of the runtime used during compilation.
In some cases I get a compilation error if I try to compile with the wrong JDK, but sometimes the build is successful and I get runtime errors when using the jars.
For example in eclipse I have the ability to establish the execution enviroment for the project in the .classpath file.
Is there a way to handle such situation in maven?
What I would like to have is the ability to handle JRE dependency like other dependencies of the project in the POM file.
UPDATE:
The accepted solution was the best one when I asked this question, so I won't change it. Meanwhile a new solution to this kind of problems has been introduced: Maven Toolchain. Follow the link for further details.
I've found this article:
http://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable>${JAVA_1_4_HOME}/bin/javac</executable>
<compilerVersion>1.3</compilerVersion>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
I have projects that need to be build with a specific version of the JDK.
You can use the Maven Enforcer plugin to enforce the use of a particular version of the JDK:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>1.5</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
But I'm not sure I really understood the question. If this is not what you want, maybe you could declare your JDK specific dependencies in profiles and use an activation trigger based on the JDK version. For example:
<profiles>
<profile>
<activation>
<jdk>1.5</jdk>
</activation>
...
</profile>
</profiles>
This configuration will trigger the profile when the JDK's version starts with "1.5".
I believe that this can be solved with following plugin in your pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
Here you target version 1.6 , or write your own version