Using Java 1.7 without maven-compiler-plugin - java

Is there any way I can force maven to compile using java 1.7 standard without using the maven-compiler-plugin? I have read about the JAVA_HOME being not set properly, but I don't think this is the case.
I have added the following line in the mvn script:
echo $JAVA_HOME
Below is the output of mvn --version
Apache Maven 3.0.4
Maven home: /usr/share/maven
Java version: 1.7.0_45, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-7-oracle/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.11.0-12-generic", arch: "i386", family: "unix"
One problem would be that $JAVA_HOME is set to /usr/lib/jvn/java-7-oracle while manven reports it to be $JAVA_HOME/jre.
The question is is there a way to configure maven tu use java 1.7 (similar to javac -target 1.7) ?

The question is is there a way to configure maven tu use java 1.7
(similar to javac -target 1.7) ?
There is a way, and that is by configuring maven-compiler-plugin. That is what Maven uses for compiling, so that's what you need to configure.
If you insist on using javac, see what maven compiler plugin manual says:
Since 3.0, the default compiler is javax.tools.JavaCompiler (if you
are using java 1.6) and is used to compile Java sources. If you want
to force the plugin using javac, you must configure the plugin option
forceJavacCompilerUse.
but even in that case it would be also about configuring the maven-compiler-plugin.

You may configure the compiler plugin by passing appropriate -D... arguments when invoking maven. In your case this might be
mvn -Dmaven.compiler.target=1.7 compile
But i'd consider this an antipattern, as this makes your build dependent on additional parameters which must be documented. It is way better to define a proper configuration in some parent pom.
See compiler:compile for complete description of the maven-compiler-plugin

Based on http://maven.apache.org/plugins/maven-compiler-plugin/testCompile-mojo.html, it looks like you can set these properties can be set without "poluting" your pom.xml file by setting in the command line the properties maven.compiler.target and maven.compiler.source.
So, a command like below should solve your problem.
mvn install -Dmaven.compiler.target=1.7 -Dmaven.compiler.source=1.7
The default version for these atributes is 1.5.
You can also modify the mvn script and add in the exec line these two atributes.

Related

Maven is using wrong java version

I am trying to build a maven project on centOS 8. I want maven to use Java version 15. When I run mvn package I get the following error:
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile
(default-compile) on project systembrett-backend: Fatal error compiling: invalid target release: 15 -> [Help 1]
So I suspect that maven is using the wrong Java version, because when i do mvn package -X for debug logs it start with:
Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 1.8.0_275, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-1.el8_3.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-240.1.1.el8_3.x86_64", arch: "amd64", family: "unix"
so it looks like, maven is using Java version 1.8.
But mvn -version says:
Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 15.0.2, vendor: AdoptOpenJDK, runtime: /opt/jdk-15.0.2+7
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-240.1.1.el8_3.x86_64", arch: "amd64", family: "unix"
JAVA_HOME is /opt/jdk-15.0.2+7 and PATH is /opt/jdk-15.0.2+7/bin:/home/username/.local/bin:/home/username/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin.
I thought maven is choosing the java version by checking JAVA_HOME, but apparently it is using an other version for the build. Does anyone know how to tell maven the correct version?
Thanks!
Following is a list of steps I use to troubleshoot this kind of issues:
Linux usually works with alternatives to ensure proper default Java environment is used. Similar to:
$ alternatives --config java
There are 2 programs which provide 'java'.
Selection Command
-----------------------------------------------
+ 1 java-1.7.0-openjdk.x86_64 (/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.191-2.6.15.4.el7_5.x86_64/jre/bin/java)
* 2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre/bin/java)
Try also for javac as it may not be configured the same way:
$ alternatives --config javac
For your maven instance, JAVA_HOME should be enough.
Your output of mvn -version testifies that you have configured it correctly. Remove your Java from your PATH to ensure JAVA_HOME and mvn will find the correct one.
The pom.xml can also configure the required compiler, similar to:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Another thing may be that you're not using the same ENV (different terminals may not have the same environment variables exported - I presume you being on CentOS, you already encountered this). You have to exit the terminal and get back in to allow the default variables to take effect.
You encounter this usually when different JRE vs JDK are used ( more: maven installation has runtime as JRE instead of JDK )
I had the same issue on RHEL 9.1. The solution was to look at /etc/java/maven.conf and set the correct JAVA_HOME here in this file.

Maven: Fatal error compiling: invalid target release: 1.8

We just upgraded the project from jdk 1.6 to jdk 1.8. While building the project in my machine, i'm getting following error.
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-compiler-plugin:3.3:compile
(default-compile) on project exception: Fatal error compiling: invalid
target release: 1.8 -> [Help 1]
Here is the maven compiler plugin which is been used
<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>
I looked at the many post and most of them were related with not having the correct version configured for java_home. I verified all of these aspects and couldn't find any of them alarming.
Java version -
qadeersmsiphone:main pdubey$ java -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)
Maven Version -
qadeersmsiphone:main pdubey$ mvn -version
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 08:22:22-0700)
Maven home: /usr/share/maven
Java version: 1.8.0_51, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.5", arch: "x86_64", family: "mac"
and I checked that while building the project maven uses jdk 1.8
qadeersmsiphone:main pdubey$ mvn install -debug
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 08:22:22-0700)
Maven home: /usr/share/maven
Java version: 1.8.0_51, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.5", arch: "x86_64", family: "mac"
[INFO] Error stacktraces are turned on.
[DEBUG] Reading global settings from /usr/share/maven/conf/settings.xml
[DEBUG] Reading user settings from /Users/pdubey/.m2/settings.xml
[DEBUG] Using local repository at /Users/pdubey/.m2/repository
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for /Users/pdubey/.m2/repository
[INFO] Scanning for projects...
UPDATE: I could get it working by removing the
maven-compiler-plugin in pom file (Ideally, i don't want to do this). And i also observed that even if i
remove this plugin, maven by default downloads the 3.3 version of
compiler plugin in my local repo. I'm not sure what's wrong in this
plugin although source and target appears to be correct.
Check the java and Javac version - My Java version was 1.8.0_51
$ java -version
java version "1.8.0_51"
But javac version was pointing to old jdk 1.7.0_67
$ javac -version
javac 1.7.0_67
There are couple of thing which we should try -
Ensure that symbolic link is pointing to correct jdk. If not, remove existing link and create new one for your jdk
cd /System/Library/Frameworks/JavaVM.framework/Versions/
rm CurrentJDK
ln -s /Library/Java/JavaVirtualMachines/<jdk version>.jdk/Contents/ CurrentJDK
In /Library/Java/Extension, i had a tools.jar. I believe, this was from old jdk.
cd /Library/Java/Extensions
-rw-r--r-- 1 root admin 15286304 Jan 14 2015 tools.jar
Remove this
sudo rm -rf /Library/Java/Extensions
point 2 did the trick for me :)
Sometimes this problem is related to a JAVA_HOME used by maven.
In my particular case I tried to start an install target and had an exact the subj message.
Then I just tried an mvn -version command and I realized that it uses jdk7
Then I run in a console set JAVA_HOME=C:\Java\jdk8 (my jdk path, so your own could be different)
Then typed mvn clean install test and it worked just fine at last
Good luck!
Same issue here, but I was using run configurations inside Eclipse when maven kept throwing this error. Fixed by setting the right JDK version here:
I had the same issue:
> java -version
java version "1.8.0_111"
>javac -version
javac 1.7.0.6
I checked the Path in System variables, and the jdk was not updated to jdk 8.
After I updated the path with the right jdk it worked.
In cent os we can use alternatives to switch the version.
alternatives --config javac
alternatives --config java
But MAVEN uses JAVA_HOME.
And this JAVA_HOME can be possible to a link.
Either we have to point the JAVA_HOME manually to new jdk folder, OR manually remove/delete the link and create a new same link again from above answers.
From alternatives we can get the path of jdk
First of all, it is definitely versions issue.
In my situation:
java -version showed the result
java version "1.8.0_172"
and javac -version was showing
javac 1.8.0_172
BUT: Maven uses JAVA_HOME environment variable to get your JDK version to build a project and run it's a plugin. So the way how we have to check it is
echo %JAVA_HOME%
Which in my situation was
jdk1.7.0_172
So simply change the path to the variable will solve the issue.
Hope it will save time to someone.

`-Dmaven.multiModuleProjectDirectory not set` issue with Maven and IntelliJ

I've spent quite some time trying to figure out what I'm doing wrong and hoping someone may be able to help.
My current setup is running Maven 3.3.3 (via homebrew) and Java 1.8. IntelliJ 13 is the IDE of choice.
Running mvn -version results in the following:
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-
22T12:57:37+01:00)
Maven home: /usr/local/Cellar/maven/3.3.3/libexec
Java version: 1.8.0_45, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.10.2", arch: "x86_64", family: "mac"
which I believe means everything is OK, but when I go to my IDE and try to run a Maven project I get this error:
/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin/java -Dmaven.home=/usr/local/Cellar/maven/3.3.3/libexec -Dclassworlds.conf=/usr/local/Cellar/maven/3.3.3/libexec/bin/m2.conf -Didea.launcher.port=7539 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 13.app/bin" -Dfile.encoding=UTF-8 -classpath "/usr/local/Cellar/maven/3.3.3/libexec/boot/plexus-classworlds-2.5.2.jar:/Applications/IntelliJ IDEA 13.app/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=13.1.3
-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
Process finished with exit code 1
My M2_HOME var is set to /usr/local/Cellar/maven/3.3.3/libexec, the same directory where homebrew installed it, and this also matches the IDE settings.
One search lead me to believe it may be something to do with Maven version and the IDE but I'm not convinced. Can someone please shed some light on this please?
Just in case you have that error again go to Settings -> Maven -> Runner and add
-Dmaven.multiModuleProjectDirectory=$M2_HOME
to VM Options.
I had this wired issue while trying to build CAS Overlay Template using Maven. I could resolve it by adding the following environment variable:
MAVEN_OPTS = -Dmaven.multiModuleProjectDirectory
From the original question, the OP has solved their issue through this approach.
I upgraded to IDEA 14, imported settings, created a new Spring project and it worked out of the box.

${java.home} changes when running Maven on a OS X

This appears to be a problem I do not see on linux or Windows machines. I have my environment setup with a JAVA_HOME derived from /usr/libexec/java_home. This is in my .bashrc file:
export JAVA_HOME=$(/usr/libexec/java_home -version 1.7)
which results in:
~/dev/Endeavor/endeavor-sdk$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home
However when I run Maven on my project, I get an error from the native2ascii plugin:
[ERROR] Failed to execute goal org.codehaus.mojo:native2ascii-maven-plugin:1.0-alpha-2-SNAPSHOT:native2ascii (default) on project dataAccessServices: Error starting Sun's native2ascii: -> [Help 1]
My investigation points to that I am using a JRE instead of a JDK. That's odd since I am clearly pointing to the JDK in my JAVA_HOME variable. However, here is the output of mvn -version:
~/dev/Endeavor/endeavor-sdk$ mvn -version
Apache Maven 3.0.4 (r1232337; 2012-01-17 03:44:56-0500)
Maven home: /usr/share/maven
Java version: 1.7.0_17, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.7.5", arch: "x86_64", family: "mac"
Notice the "Java home:" line indicates that the ${java.home} variable has been moved to the JRE instead of the JDK that the JAVA_HOME environment variable points to.
I have tried setting the JAVA_HOME in numerous places but I can't seem to find a way to get maven to use the JDK as the home and not the JRE.
UPDATE:
I found this that appears to indicate that this is expected behavior: Java_home in Maven
But that does not explain why the native2ascii plugin cannot find the JDK tool it needs.
I have removed the use of native2ascii since it was largely unnecessary. So, I am avoiding the problem at this point. However, if anyone has an idea as to why this behaves differently on different systems, please let me know.
Have you tried running mvm help:system that should print all environment variable values that are relevant to maven?
There has been lots of discussions on this matter and here Java_home in Maven you can find a real good answer.
In a nutshell: your configuration it's just right, it is just a matter of semantics. You can read further here: http://javahowto.blogspot.com/2006/05/javahome-vs-javahome.html

Netbeans Maven project: JAVA_HOME is not defined correctly

I have a NetBeans Platform project build with Maven2. When I try to create a new platform module I get this strange error:
cd ~/src; JAVA_HOME=/usr/lib/jvm/jdk1.7.0/bin/java ~/netbeans-7.3/java/maven/bin/mvn -DarchetypeGroupId=org.codehaus.mojo.archetypes...
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/lib/jvm/jdk1.7.0/bin/java/bin/java
In ~/.bashrc I have definition of JAVA_HOME:
export JAVA_HOME="/usr/lib/jvm/jdk1.7.0"
export JDK_HOME="/usr/lib/jvm/jdk1.7.0"
There's no slash at the end, however maven appends /bin/java/bin/java to this path. I can build already existing module with Maven without problems, just when creating a new one it fails. Where does Maven get this JAVA_HOME?
EDIT:
maven settings:
$ mvn -version
Apache Maven 2.2.1 (r801777; 2009-08-06 21:16:01+0200)
Java version: 1.7.0_17
Java home: /usr/lib/jvm/java-7-oracle/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux" version: "3.2.0-39-generic" arch: "amd64" Family: "unix"
You're redefining JAVA_HOME on the command line to point to /usr/lib/jvm/jdk1.7.0/bin/java, which is wrong:
cd ~/src; JAVA_HOME=/usr/lib/jvm/jdk1.7.0/bin/java ...
Remove the JAVA_HOME=/usr/lib/jvm/jdk1.7.0/bin/java from the command line:
cd ~/src; ~/netbeans-7.3/java/maven/bin/mvn -DarchetypeGroupId=org.codehaus.mojo.archetypes...
netbeans takes the JAVA_HOME from the definition of the Java Platform in the IDE (See Tools/Java Platforms)
unless you select a custom platform, IDE uses the default one which is the one the IDE is running on.
All platforms need to be defined in the Tools/Java Platforms dialog and validation checks are in place to select the right folder.
Maybe something changed in time for you and the definition there is not correct?
the problem was with incorrect definition of JAVA_HOME in ~/.bash_profile
NetBeans integration of maven prefers:
System.getenv("JAVA_HOME")
before:
System.getProperty("java.home")
for linux find the mvn executalble file under netbeans's java/maven/bin folder and add
from="/bin/bin/java"
to="/bin/java"
JAVACMD="${JAVACMD/$from/$to}"
before the lines
if [ ! -x "$JAVACMD" ] ; then
echo "Error: JAVA_HOME is not defined correctly."
cheers

Categories

Resources