Maven doesn't use Java 7 - java

I want to package a maven-(multi)module, the parent POM includes:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
I'm using Java 1.7 and the properties are specified as follows:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<slf4j.version>1.6.1</slf4j.version>
</properties>
The Maven version is 2.2.1:
johannes#luna:~/workspace/treetank/bundles/treetank-core$ mvn -version
Apache Maven 2.2.1 (rdebian-6)
Java version: 1.7.0
Java home: /usr/lib/jvm/jdk1.7.0/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux" version: "3.0.0-14-generic" arch: "amd64" Family: "unix"
I have no clue why it doesn't use Java version 1.7. When invoking mvn package I get the error (use -source 7 or higher to enable diamond operator) for instance. Do you know why it tries using 1.6?
The effective POM is:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</execution>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</execution>
</executions>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

This may not work in maven 2.2.1, but with Maven 3.0.4, simply adding the two properties to the pom's properties enables Java 7 for me:
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

perfectly nice explanation on the compatibility issue of jdk 1.7 with maven 2.2.1 given by Mark Peters
Maven "could not parse error message" (Java 7 + Maven 2)

Related

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?

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 with JDK11: javac: invalid flag: --release

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.

How to set the generated sources as target bytecode 1.7 when using jooq-codegen-maven

I use JOOQ to implement MySQL DAO layer. And my part of pom.xml is as below,
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>3.9.1</version>
<!-- The plugin should hook into the generate goal -->
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies />
<configuration>
<jdbc>
<driver>${jdbc.driver}</driver>
<url>${jdbc.url}</url>
<user>${jdbc.user}</user>
<password>${jdbc.password}</password>
</jdbc>
<generator>
<database>
</database>
<target>
</target>
</generator>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass />
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
I got the jar file by mvn package. But when I run the jar file, the error occurs:
java.lang.UnsupportedClassVersionError: org/jooq/Table : Unsupported major.minor version 52.0
I learned that I could update my running JDK version to a higher one, or compile the jooq generated classes with a lower version. Here I have to choose the latter method. But the result didn't meet my expectation after I set the target of maven-compiler-plugin to 1.7. I am confused since I still get this error. So how can I achieve my goal?
The jOOQ Open Source Edition version 3.7+ requires Java 8 to run. If you need Java 7 support, that is provided by the jOOQ Professional Edition and jOOQ Enterprise Edition. See version support here:
https://www.jooq.org/download/versions
In particular, you have defined Maven properties that point to the JDK 8:
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
But you're not using them in your compiler plugin configuration. You should switch this:
<source>1.7</source>
<target>1.7</target>
To this:
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
version 52.0 is java 8, therefore i think you need to change the value in
< maven.compiler.source>1.8< /maven.compiler.source>
< maven.compiler.target>1.8< /maven.compiler.target>
or perhaps you could find a newer version of JOOQ that supports java 1.8?

Categories

Resources