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.
Related
This question already has an answer here:
Compiling Scala Using Maven
(1 answer)
Closed 2 years ago.
When we compile a Maven project, by default the Java compiler is used to compile the source files. But nowhere this is mentioned in the POM file to instruct the compile phase to use the Java compiler.
Can someone please help how Maven automatically uses the Java compiler?
Suppose that we use maven to build a Scala programs, then how we can we instruct Maven to use the Scala compiler instead of Java?
Can someone please help how maven automatically uses java compiler?
Maven is designed to build and manage dependencies for a Java project. By default it will use a Java compiler.
If mvn is used to compile Scala programs instead of Java, then you'll need to use a maven plugin to compile Scala code. This plugin will work. Then code that is in src/main/scala will get compiled with the Scala compiler used by the plugin.
By default Maven uses Java compiler included in the JDK. As stated here the default is the class javax.tools.JavaCompiler.
If you want to override parameters for the compiler you can include your own plugin section:
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<compilerArgument>-Xlint:all</compilerArgument>
</configuration>
</plugin>
...
</plugins>
</build>
and set the version of the compiler in properties:
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
If you want to use Scala, there are instructions on how to do this. You'll still need a Java compiler available.
There is maven-compile plugin which is active by default when your pom packaging is jar:
https://maven.apache.org/plugins/maven-compiler-plugin/index.html
Usually you do not have to tweak it at all
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>
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.
Installed java version is 1.8, while selecting this version in pom.xml and updating the maven project,it automatically jumps from 1.8 to 1.4, due to which I am unable to have Lambda expression specific code.
I am using eclipse kepler.
Any idea what is happening here ?
Add something similar to this to your pom. You might have to tweak the plug in version.
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Apologies I'm on mobile and the editor sucks for code formatting.
Edit: As noted in the accepted answer you also need to enable Java 8 support in Kepler.
With the help of Hogler's comment above I am able to reswolve this issue
Just did this :
https://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler
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.