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

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>

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.

springboot maven running as init.d service on linux not working

I have maven spring-boot application running as an init.d service on ec2 instance - it is working fine in the lower-level environment but when I do the same on prod instance I am getting "Failed to load Main-Class manifest attribute from /path/application.war. I have tried all possible google solutions but nothing helped to resolve as it is working at the lower level and all configs and versions are the same on both instances. attaching maven build block below. when I unzip the war I see the main-class and start-class in manifest file.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<fork>true</fork>
<executable>true</executable>
<profiles>dev</profiles>
<jvmArguments>-Xdebug
-Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n</jvmArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>

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

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%

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>

Multiple Source Folders for Eclipse Java Project & Maven

Question/Problem:
How to add an additional source folder to a standard java console Maven project using Eclipse (Luna) so that Maven sees the path for jar build.
The expected result is to somehow configure pom.xml so that Maven plugins in Eclipse can be executed cleanly.
Assumptions - a successful add of an additional source folder via project (right click) -> new -> source folder.
To let Maven know about the new source folder for building a jar I had to add the following to my pom.xml:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<includes>
<include>[your source folder goes here]/**/*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>[your source folder goes here]</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
Be sure to add the pluginManagement tags around plugins as omitting this tag prevented the mojo plugin to recognize the executions tag.
Perhaps more later on the success of the actual jar construction...
Add generated sources into configuration of maven-compiler-plugin:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<generatedSourcesDirectory>[additional directory]</generatedSourcesDirectory>
</configuration>
</plugin>
or provide additional execution:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile-additional-sources</id>
<goals><goal>compile</goal></goals>
<configuration>
<source>[additional sources]</source>
</configuration>
</execution>
</executions>
</plugin>

Categories

Resources