Upgrading to Java 11, sonarqube, jacoco with maven causing errors - java

After upgrading Java from 8 to 11. sonar:sonar is not working getting exception
in the end.
Java 1.8 is working fine with Sonarqube 7.7, now with java 11 and sonarqube 7.8 it is failing with exception
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar (default-cli) on project xxxx: Unable to read ....\target\jacoco.exec to determine JaCoCo binary format.: EOFException -> [Help 1]
Using below properties for java 11
<sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
<sonar.sources>src/main</sonar.sources>
<sonar.tests>src/test</sonar.tests>
<runSuite>**/*Test.class</runSuite>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis
<sonar.jacoco.reportPaths>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPaths>
<sonar.language>java</sonar.language>
Below plugins using jacoco
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>${java.version}</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>--illegal-access=permit</argLine>
<includes>
<include>${runSuite}</include>
</includes>
<forkCount>0</forkCount>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<configuration>
<destFile>${sonar.jacoco.reportPaths}</destFile>
<append>true</append>
</configuration>
<executions>
<execution>
<id>agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
Am I missing any configuration in pom file to run sonarqube using jacoco

Removing sonar.coverage.jacoco.xmlReportPaths and maven-surefire-plugin gave the code coverage. Issue is fixed.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>--illegal-access=permit</argLine>
<includes>
<include>${runSuite}</include>
</includes>
<forkCount>0</forkCount>
</configuration>
</plugin>
Useful links:
https://52.213.100.93/browse/MMF-1651
https://github.com/SonarSource/sonar-scanning-examples/tree/master/sonarqube-scanner-maven

The issue was with property need to include sonar.coverage.jacoco.xmlReportPaths
<sonar.coverage.jacoco.xmlReportPaths>${project.basedir}/../target/jacoco.exec</sonar.coverage.jacoco.xmlReportPaths>
After changing the mvn sonar:sonar is successful but the code coverage is 0%

Related

how to apply semantic versioning to the java maven project to automatically increment the version inside pom.xml

Can anyone please tell me how to apply the semver to the java maven project? I tried many ways, but I didn't find any useful resources to automatically increase the version when I push the code to the branch. I'm using Github action workflow to deploy the project into GitHub.
Thank you.
My first approach is to use the command line but you have to configuration the following in your pom file before. You can of course directly use the command line and put everything on the plain command without this setup but it's very inconvenient
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.9.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<executions>
<execution>
<id>major</id>
<goals>
<goal>set</goal>
</goals>
<configuration>
<generateBackupPoms>false</generateBackupPoms>
<newVersion>${parsedVersion.nextMajorVersion}.0.0-SNAPSHOT</newVersion>
</configuration>
</execution>
<execution>
<id>minor</id>
<goals>
<goal>set</goal>
</goals>
<configuration>
<generateBackupPoms>false</generateBackupPoms>
<newVersion>${parsedVersion.majorVersion}.${parsedVersion.nextMinorVersion}.0-SNAPSHOT</newVersion>
</configuration>
</execution>
<execution>
<id>patch</id>
<goals>
<goal>set</goal>
</goals>
<configuration>
<generateBackupPoms>false</generateBackupPoms>
<newVersion>${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion}-SNAPSHOT</newVersion>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>parse-version</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
By using the above configuration you can change/update the version of your project like this:
mvn build-helper:parse-version versions:set#major
This will increment the major version and set minor and patch version to 0.
mvn build-helper:parse-version versions:set#minor
This will increment the minor version and set patch version to zero.
mvn build-helper:parse-version versions:set#patch
this will increment the patch version. Afterwards you have to commit your changed back into your version control system (for example git).
I recommend to define this kind of setup into a parent pom and reuse it for multiple projects. A detail explanation why and how this works can be found here https://blog.soebes.de/blog/2021/04/05/maven-plugin-configuration/
Using the maven-release-plugin is also an option. It will make also the tags in your version control.

Execute a Maven plugin with --enable-preview in POM

I have a custom Maven plugin which makes use of JDK 12 preview features. I compile the plugin setting --enable-preview as compiler arg, i.e.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<compilerArg>--enable-preview</compilerArg>
</compilerArgs>
</configuration>
</plugin>
When I want to execute the plugin, I add the plugin like this in the POM:
<plugin>
<groupId>my.group</groupId>
<artifactId>my-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>my-goal</goal>
</goals>
</execution>
</executions>
</plugin>
But this fails with:
Preview features are not enabled for MyPluginMojo. Try running with '--enable-preview'
How can I enable preview features in a plugin execution?
For me, I had to add a config file to my build directory at:
.mvn/jvm.config
containing:
--enable-preview
This will make sure that Maven passes the correct parameters to JVM
You made a mistake in your pom. <compilerArgs> takes nested <arg>, like so:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>
For JDK 17, this works for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>

Maven exec:java goal is not running after test failures

I have 2 maven goals
1) Clean Install
2) exec:java
When ever I ran the goal "mvn clean install", It is getting executed and after It triggers the exec:java goal directly, if the build got success. but If maven build fails, it is not triggering the "exec:java" job. I want to run the java job irrespective of any results.
My POM:
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18</version>
<configuration>
<forkCount>0</forkCount>
<!-- <systemPropertyVariables>
<environment>${TestSuiteName}</environment>
</systemPropertyVariables> -->
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>send-report</id>
<phase>package</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.acxsys.ocp.api.emt.ks.report.KSSendEmail</mainClass>
</configuration>
</plugin>
Try to run your job with mvn -fn clean install
-fn means fail-never and this will always continue the goal(s) execution.

How to set Java/Scala tests' system properties through maven plugin?

I would like to run my test with 2 different profiles, each set a Java property that results in my scala test code being executed differently.
I tried configuring both maven-surefire and maven-scalatest plugins:
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<systemProperties>
<spark.master>local</spark.master>
</systemProperties>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<systemPropertyVariables>
<spark.master>local</spark.master>
</systemPropertyVariables>
</configuration>
</plugin>
but seems like non of them works, when executing System.getProperty("spark.master") the result is still null. What should I do to enable this setting?
//-----------------------------------------------
Response to the first answer:
I've changed my surefire config into the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
<systemPropertyVariables>
<spark.master>${spark.master}</spark.master>
</systemPropertyVariables>
</execution>
</executions>
<configuration>
<forkCount>1</forkCount>
</configuration>
</plugin>
but apparently the is in the wrong place. Maven gave me this error:
[ERROR] Malformed POM /home/peng/git/datapassport/pom.xml: Unrecognised tag: 'systemPropertyVariables' (position: START_TAG seen ...</goals>\n <systemPropertyVariables>... #170:50) # /home/peng/git/datapassport/pom.xml, line 170, column 50 -> [Help 2]
(a) if you use JUnit - upgrade its version to 4.7 or higher and specify explicit provider:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.18.1</version>
</dependency>
</dependencies>
(b) in surefire plugin configuration specify
<forkCount>1</forkCount>
(c) use plugun's executions maven mechanism to run two different profiles
<executions>
<execution>
<id>tests-1</id>
<goals><goal>test</goal></goals>
<configuration>
<systemProperyVariables ... />
</configuration>
</execution>
<execution>
<id>tests-2</id>
<goals><goal>test</goal></goals>
<configuration>
<systemProperyVariables ... />
</configuration>
</execution>

Maven Surefire plugin "Error occured in starting fork, check output in log"

I get the following error
BUILD ERROR
Error occured in starting fork, check output in log
when using Maven 2.2.1 and Surefire plugin 2.11 while running junit test cases.
How can I fix it?
You need to setup surefire plugin to use <forkMode>once</forkMode> like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skipTests>false</skipTests>
<testFailureIgnore>true</testFailureIgnore>
<forkMode>once</forkMode>
</configuration>
</plugin>
I was facing the same issue on my local with maven-surefire-plugin plugin.
After adding <forkCount>0</forkCount> to maven-surefire-plugin plugin it worked for me.
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xmx1024m -XX:MaxPermSize=256m ${surefireArgLine}</argLine>
<includes>
<include>**/*Test.java</include>
</includes>
<forkCount>0</forkCount>
</configuration>
</plugin>
encountered the same issue
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
set "useSystemClassLoader as false"

Categories

Resources