1

We're trying to upgrade all our systems to jdk1.8.0_66, including maven (running on jenkins).

[jenkins@zldinfra1 jvm]$ mvn -version
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 14:51:28+0100)
Maven home: /opt/apache-maven-3.0.5
Java version: 1.6.0_24, vendor: Sun Microsystems Inc.
Java home: /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-279.5.2.el6.centos.plus.x86_64", arch: "amd64", family: "unix"
[jenkins@zldinfra1 jvm]$ echo $JAVA_HOME
/opt/java
[jenkins@zldinfra1 jvm]$ file /opt/java
/opt/java: symbolic link to `jdk1.8.0_66'

All a quick google led to is that I should change my JAVA_HOME to the desired Java version. As you can see above, it's set to jdk1.8.0_66, but Maven is using a completely different path.

Question: How do I change maven's JAVA_HOME?

Edit:

Following @Rottens answer, I manged to get maven running with jdk1.8.0_66, maven's JAVA_HOME being set to /opt/jdk1.8.0_66/jre.

However, I'm still getting the following error upon trying to Deploy (building works fine):

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project deployTest: Fatal error compiling: invalid target release: 1.7

In the POM the compile plugin is set up as follows:

<plugin>
    <inherited>true</inherited>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven.compiler.plugin.version}</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
    </configuration>
</plugin>

In my Jenkins Project config I specified it to use the default JDK and Maven Version 3.2.1.

Just noticed:

The Maven Version 3.0.5 (which I configured to use 1.8) does not appear in Jenkins. The Versions that appear in Jenkins are 3.2.1, 3.1.1 and 3.0.4.

Marius Schär
  • 336
  • 2
  • 17
  • Have you checked `/private/etc/mavenrc` and `~/.mavenrc` for overriding JAVA_HOME settings? – Gergely Bacso Nov 27 '15 at 08:11
  • What is the output of `mvn --v`? – Patrick Nov 27 '15 at 08:11
  • Does Maven also uses the same user `jenkins`? Good chance that `JAVA_HOME` is set differently for your Maven user. – nobeh Nov 27 '15 at 08:12
  • @Patrick `mvn --v` outputs the same thing as `mvn -version` – Marius Schär Nov 27 '15 at 08:13
  • @GergelyBacso I will look at that right now – Marius Schär Nov 27 '15 at 08:13
  • @Martin if its shown in your picture then its ok. I have no permission to see pictures at my current location. sorry – Patrick Nov 27 '15 at 08:15
  • @Patrick I exchanged the picture for text – Marius Schär Nov 27 '15 at 08:25
  • @GergelyBacso I cannot seem to find mavenrc anywhere – Marius Schär Nov 27 '15 at 09:10
  • That is an optional config. If you don't have it, that means it is not the one causing your problem. – Gergely Bacso Nov 27 '15 at 09:22
  • Could you check under your project structure which java version is it using? Could be possible you have multiple java version installed on your machine and the project might be using the lower version while building. – Naman Nov 27 '15 at 09:26
  • @nullpointer There are multiple java versions installed, and it's most definitely using a lower version than 1.8 (which the JAVA_HOME) points to. In Jenkins it's configured to use the "Default" JDK and in the POM I specified it to compile for 1.7. – Marius Schär Nov 27 '15 at 09:33
  • @Martin : Since the POM uses the java version 1.7 and compiles the project using the same which might not be present in the system. You must look into this once. http://stackoverflow.com/a/14043545/1746118 – Naman Nov 27 '15 at 09:39
  • @nullpointer I've seen this question and looked, but as far as I can tell, the script just uses the `JAVA_HOME` variable. – Marius Schär Nov 27 '15 at 09:44
  • 1. _mvn script_ sample : `#!/bin/bash JAVA_HOME="${JAVA_HOME:-$(/usr/libexec/java_home)}" exec "/usr/local/Cellar/maven/3.3.3/libexec/bin/mvn" "$@"` 2. update the maven plugin and change to `maven-compiler-plugin **"newer version"**` 3. the pom reads `true` and I hope the parent specifications are not different..are they? – Naman Nov 27 '15 at 09:57

3 Answers3

0

Be sure to check your $PATH environment variable, I always set my JAVA_HOME in my .profile and then use it at the beginning of my PATH variable so I'm always sure that I'm using my jdk version of choice. somethign like this :

JAVA_HOME=/opt/java/jdk/jdk1.8_65
export JAVA_HOME

PATH=$JAVA_HOME/bin:$PATH
export PATH
Michael-O
  • 18,123
  • 6
  • 55
  • 121
Rotten
  • 1
  • 1
0

Update the maven version of your system from Apache Maven 3.0.5 to Apache Maven 3.3.x

OR

In your project pom make changes to adapt to system as :

<plugin>
    <inherited>true</inherited> **//default set to true**
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.0.5</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
    </configuration>
</plugin>
Naman
  • 27,789
  • 26
  • 218
  • 353
  • Having upgraded to `3.3.9`, using the "default" option for JDK yields an `UnsupportedClassVersionError`. If I explicitly use "1.8.0" though, the release succeeds. – Marius Schär Nov 27 '15 at 10:20
  • explicitly as? @Martin – Naman Nov 27 '15 at 13:09
  • Our jenkins has (or rather had) jdk1.7 and jdk1.8 installed. When using "default" it used 1.8, but failed. If, however you use "1.8" it still used that same jdk, but succeeded. – Marius Schär Nov 27 '15 at 13:46
0

My problem was resolved by uninstalling jdk1.7.0 from Jenkins, only leaving jdk1.8.0.

Configuring each project explicitly to use jdk1.8.0 did also work, but that's for maintenance reasons not what we wanted to do.

I don't know why letting Jenkins automatically choose jdk1.8.0 to compile did not work, but that seemed to be the issue.

In addition I upgraded Maven itself to 3.3.9 and all the used maven plugins to their respective newest version. This did not affect the Maven build. It did however break our ant build.

I also upgraded Jenkins to run on Java 1.8 instead of 1.7, which didn't have any effect (except for making compile times marginally shorter).

Marius Schär
  • 336
  • 2
  • 17