How do I set JDK version for gmaven plugin? - java

Is it possible to force the gmaven plugin to use a different JDK than the one specified in JAVA_HOME? We need to build a specific project using Java 7, but most developers will have JAVA_HOME set to a Java 6 install as all our other projects are still on Java 6.
The error message we get is:
[ERROR] Failed to execute goal org.codehaus.gmaven:gmaven-plugin:1.4:generateStubs (default) on project XYZ: Execution default of goal org.codehaus.gmaven:gmaven-plugin:1.4:generateStubs failed: An API incompatibility was encountered while executing org.codehaus.gmaven:gmaven-plugin:1.4:generateStubs: java.lang.UnsupportedClassVersionError: <snip> : Unsupported major.minor version 51.0
Thanks!

The Maven Enforcer plugin is an easy way to require a specific JDK version.

you can specify JAVA_HOME for maven task in ide or bat/sh file for example
Intellige Idea
and no need to change system environment variables

Example configuration from maven-compiler-plugin
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
The maven by default uses Java 1.4, not the Java set in the JAVA_HOME variable.

You can set an alternate jdk in the maven-complier-plugin configuration. See here

Related

Method introduced in java 9 is compiled using compiler target java 8

I'm experiencing an odd behavior in maven as well as in eclipse itself.
Even though i configured my project to be compiled in Java 1.8, I can compile and run (eclipse) a piece of code that was introduced in Java 9
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
The code line in question:
LocalTime.ofInstant(cal.toInstant(), cal.getTimeZone().toZoneId());
I'm using Oracle's JDK 11 locally for compiling and running in eclipse without any errors. When i package it into a docker container using openjdk:8-jdk-alpine it will boot up, but throw the following Exception when I call the method:
java.lang.NoSuchMethodError: java.time.LocalTime.ofInstant(Ljava/time/Instant;Ljava/time/ZoneId;)Ljava/time/LocalTime
How can I avoid and identify these situations before they go to testing? Am I doing something wrong or is it a bug in the build system or in JDK11?
thanks in advance
The source option specifies that the source code must be compatible with Java 8, the target option that the classes should be compatible with Java 8. However, you will still compile with the Java 11 class library if you build with Java 11 and then you can get errors like the one you have.
There are two good solutions. One is to use the Maven toolchains plugin and build with Java 8. Then you can have multiple Java versions installed and Maven will use the configured one on a per-project basis.
The other is to use the new release and testRelease options. They will build with API classes from the given release. Just add <release>1.8</release>.
If you are using JDK 11, configure your maven pom.xml like that:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</build>

maven-compiler-plugin broken on linux?

I have a maven project. In the pom.xml file the following is stated:
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
...
</plugins>
</build>
...
AFAIK this is correct, it should build against JDK 7.
I run a Debian based Linux dist and when I do mvn clean install it seems to always build against the javac version I have set in my os.
I've tried reading up on what the plugin exactly does https://maven.apache.org/plugins/maven-compiler-plugin/, but it doesn't really state how.
An example is I have javac 8 running on my os. When I invoke mvn clean install, the project compiles against JDK 8 and not JDK 7 as stated in the pom.xml. Why is this?
By default the maven-compiler-plugin uses %JAVA_HOME%/bin/javac to compile, unless:
you set the executable-parameter to a different location
you use Toolchains, which seems to match your requirements, i.e a different Java Runtime for Maven compared to the JDK for the maven-compiler-plugin.
Source and target settings are just passed to the javac compiler as parameters. The javac installed on the machine is used.

Compile error when doing Maven install

I was trying to install the dependency packages into my location maven repository with running mvn clean install from my maven project.
But unfortunately. I got a compile error for some source files which said :annotations are not supported in -source 1.3(use -source 5 or higher to enable annotations)#override.
After researching. I found I need to specify the source version of my maven project.
So I add the following configuration in the POM.
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
Actually. It does work fine. But what makes me confused is where does the JDK1.6 comes from and make the Maven compile work ? I didn't have the JDK1.6 install in my computer(Currently, the available JDK installed in my computer is JDK1.8), Thanks.
Later versions of Java allow you to compile your program as earlier versions. Since you have Java 8 installed you can compile your program as a Java 6 one that will run on a machine with only the 1.6 JRE. The limitation is that you can't use any of the language features added in the Java 7 and 8 releases.

error compiling java7 project with maven (heroku)

I'm trying to upload on heroku simple servlet with maven. Locally my servlet is working just fine but when i use:
git push heroku master
I get "BUILD FAILURE" with error message:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:
3.1:compile (default-compile) on project HelloServlet: Fatal error compiling:
invalid target release: 1.7 -> [Help 1]
I changed everything to Java 1.7 in system variables, maven is running Java 1.7, javac version is 1.7?
Am I missing something here?
edit: my JAVA_HOME and error screenshot
By default heroku apps run on OpenJDK 6.You have to add additional properties to make your app use Open JDK 7 on heroku.
Refer : https://devcenter.heroku.com/articles/add-java-version-to-an-existing-maven-app
Did you set source and target configuration parameters of maven-compiler-plugin correctly?
http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
In your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

setting -source to 1.5, it is set to 1.3 apparently

I am using eclipse, with maven2 plugin.
I am trying to setup a simple annotations based spring 3 mvc web application.
So I went to RunAs and clicked on 'maven build', I set the goal as 'compile'.
When it compiles, I get the error message:
E:\dev\eclipse\springmvc2\src\main\java\web\HomeController.java:[5,1] annotations are not supported in -source 1.3
(use -source 5 or higher to enable annotations)
#Controller
so far I modified the eclipse.ini to use the jdk. I also made sure under preferences, it is at java 1.6.
Not sure where else to change the java version?
(I am assuming source 1.3 means java 1.3 and that I need it to be at least version 1.5 compatible)
You should also set a proper source version in pom.xml (because maven can make builds without Eclipse, so it can't use Eclipse preferences):
<project ...>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
...
</project>

Categories

Resources