Compile error when doing Maven install - java

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.

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 install issue with Java 1.8 version

I have written code which can compile on Java 1.8 (try block). It is compiled and running for me when I run it directly. While I am trying to build it using Maven in my local, I am facing an issue which is saying to use -source 7 or later. I tried all possible ways to use java 8 and all places I have specified 8 only but Maven is trying to compile using java 1.6 it seems.
Can any one you please help me where can I correct it to pick my maven to use java 1.8.
Thanks,
Venkat
Following should work too:
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
You can specify that using the maven-compiler-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0<</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
Just remember to use the newest version of the plugin.

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.

org/codehaus/plexus/archiver/jar/JarArchiver (Unsupported major.minor version 49.0) - Maven build error

Afternoon all,
I am receiving the above error when trying to build my project. I'm pretty sure this has something to do with Maven's latest update being compiled using Java 1.6 and the project we are trying to build is a 1.4 project. The plugin prior to this worked without problems, so I have added the following to the POM.xml file to try to force the existing plugin to be used.
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-archiver</artifactId>
<version>1.2</version>
</dependency>
But it continues to fail.
Any help would be much appreciated
Thanks
Try to add the following plugin for maven. It works for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
The error you are experiencing means that org/codehaus/plexus/archiver/jar/JarArchiver was compiled against Java 1.5 whilst you are trying to load using older Java version.
1.2 version of plexus-archiver works under Java 1.4. However 2.0 requires Java 1.5. Are you sure you are using 1.2?
If this is a plugin, it should be defined under <plugins>.
Use:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
I was getting this error till I changed my maven-source-plugin version to 2.2.1. I was getting the error with 2.1.1.
Including the 'old' version of a plugin in the pom.xml is solving part of the problem. you also need to make sure you are using the right jvm to match.
For a current project i'm working on I had to set JAVA_HOME to java 1.4 with maven 2.0.8. Problem with maven is that it looks for updates in the local and remote repositories (if there is a remote repository set in the maven settings.xml), than tries use version 2.5 for the 'clean' and 'install' plugins for example causing the major.minor 49.0 error (clean and install plugins version 2.5 are compiled with java 1.5 or greater while I tried to execute them in a java 1.4 environment).
With adding in the plugin snippet in the pom.xml of the project forcing it to use version 2.2 combined with the old java version on my path:
(set path=c:\youroldjavadirectory\bin;c:\youroldmavendirectory\bin) everything started working.
Check versions of java before running the maven command:
java -version
mvn -v
According to Fred from the m2e-mailing list, this has been fixed with m2eclipse-mavenarchiver 0.17.0. You can install it from http://repo1.maven.org/maven2/.m2e/connectors/m2eclipse-mavenarchiver/0.17.0/N/LATEST/
Just add the repo as an update site, and then upgrade the mavenarchiver component.

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