maven with JDK11: javac: invalid flag: --release - java

I'm trying to set up a simple maven project with java 11. As I want to keep JAVA_HOME to be version 8, I'm using maven-toolchains-plugin to make maven use jdk11 for this project.
While maven successfully finds a matching toolchain for jdk-11.0.1, I keep getting " javac: invalid flag: --release". What am I doing wrong?
Here are the plugin configurations:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>11</version>
</jdk>
</toolchains>
</configuration>
</plugin>
The toolchain is defined as:
<toolchain>
<type>jdk</type>
<provides>
<version>11</version>
<id>JavaSE-1.11</id>
</provides>
<configuration>
<jdkHome>C:\Program Files\Java\jdk-11.0.1\bin</jdkHome>
</configuration>
<toolchain>

As I found out, the configuration is just fine. The problem was that jdkHome in toolchains.xml was pointing to the \jdk-11.0.1\bin direction instead of \jdk-11.0.1 directly..... Using <jdkHome>C:\Program Files\Java\jdk-11.0.1</jdkHome> solves the problem..

Changing the jdk version should fix the problem mostly. Replace
<version>1.11</version>
with
<version>11</version>
Do ensure though that your maven is configured with JDK-11 using the command mvn -version and confirming the Java version there. You can also verify the toolchains.xml JDK configured as well.
In case you're trying to compile using different versions of the compiler, you need to ensure executions under the maven-compiler-plugin as:
<executions>
<execution>
<id>java11</id>
<phase>none</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>11</release>
<jdkToolchain>
<version>11</version>
</jdkToolchain>
<compileSourceRoots>
<compileSourceRoot>${project.basedir}/src/main/java11</compileSourceRoot>
</compileSourceRoots>
<outputDirectory>${project.build.outputDirectory}/META-INF/versions/11</outputDirectory>
</configuration>
</execution>
</executions>
Here is the sample pom.xml referred for the above.

Related

Skip default jar creation in maven causes error

Im using maven 3.8.3. Tryig to avoid the step of the default jar creation, using the answers I found in topics like:
remove jar created by default in maven
What is the best way to avoid maven-jar?
What I tried:
1.
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.3</version>
<executions>
<execution>
<id>default-jar</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.3</version>
<executions>
<execution>
<id>default-jar</id>
<phase/>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>none</phase>
<configuration>
<finalName>unwanted</finalName>
<classifier>unwanted</classifier>
</configuration>
</execution>
</executions>
</plugin>
I also checked the solution of <packaging>pom</packaging> instead of jar. but I'm not sure this is the use case.
More details: This is a non-empty module, containing resources and generated unversioned java sources.
Solutins #1-#3 caused me the following error:
The packaging for this project did not assign a file to the build artifact.
Please assist, Thanks in advance.

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.

Maven JavaDoc listed classes twice

I am using the javadoc maven plugin and it creates the correct javadoc package, but all classes are created twice.
Maven dependency:
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.0</version>
</dependency>
My build code
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Can anyone help me please, what am I missing here?
command usage for doc generation
mvn clean install -Dresources="FirstProject/example_API"
I noticed the same problem and came upon a solution after enabling debug on the maven-javadoc-plugin maven plugin and seeing what it's doing. Specifically setting the sourcepath as shown below fixed the double listing problem for me and I've tried this on multiple version of Corretto 8 as well as Temurin 8. All had the double listing problem because it's an issue with the javadoc tool itself but setting the sourcepath manually fixed it for me.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<debug>true</debug>
<sourcepath>${basedir}/src/main/java</sourcepath>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
There's a bug in recent versions of the Maven Javadoc Plugin. The bug is known as MJAVADOC-700. It is dead easy to reproduce.
Downgrading to version 3.2.0 of the plugin fixes the problem. Setting the sourcepath explicitly is an alternative fix.

Is there a way to use an older version of Java with scala-maven-plugin

I would need to compile a legacy scala project (Scala version 2.8.0). The version is incompatible with JDK8. So I would like to ideally compile the project using JDK 7. I am looking for a configuration something like the following in maven-compiler-plugin.
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
Is there a similar configuration in scala-maven-plugin?
I have tried using "source" as suggested in the documentation of the plugin and javacargs as well. But both those options don't work.
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.4.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<recompileMode>incremental</recompileMode>
<source>1.7</source>
<target>1.7</target>
<javacArgs>
<javacArg>-source</javacArg>
<javacArg>1.7</javacArg>
<javacArg>-target</javacArg>
<javacArg>1.7</javacArg>
</javacArgs>
</configuration>
</plugin>
Does anyone know what I am doing wrong? I don't have the option of using JDK7

How to make apt-maven-plugin work with java 8

I am migrating from java 6 to java 8. I have a maven project and it has apt-maven-plugin which is not running on jdk 1.8. Do we have a workaround or an alternate to make it work with java 8? Is it possible to run apt-maven-plugin with java 8??
Please find below the maven entry:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.0-alpha-3</version>
<configuration>
<factory>
com.emc.tsg.common.xml.ABCGenerator
</factory>
<includes>
<include>**/model/*.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
and i have changed the maven tools.jar dependency for java 8 as:
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
I have changed the java version as:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<optimize>true</optimize>
<debug>false</debug>
</configuration>
</plugin>
When i am trying to run mvn compile, i am getting apt compiler not found error. I think in jdk tools.jar there is no apt class unlike java 6 hence wondering if at all its possible to achieve this in java 8?

Categories

Resources