Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-33966

JAVA_HOME is not set when Maven is automatically installed

    • Icon: Bug Bug
    • Resolution: Not A Defect
    • Icon: Minor Minor
    • pipeline
    • Windows 10, Jenkins 2.0-beta-1, JDK 1.8.0_77

      I run Jenkins 2.0 beta binary under Windows 10.
      I declared a Maven installation "M3" under Global Tool Configuration.
      I created the pipeline job with the following script:

      node {
        git url: 'https://github.com/jglick/simple-maven-project-with-tests.git'
        def mvnHome = tool 'M3'
        bat "${mvnHome}\\bin\\mvn -B verify"
      }
      

      I get the following error when installing Maven:

      Started by user Administrateur
      [Pipeline] Allocate node : Start
      Running on master in C:\Program Files (x86)\Jenkins\workspace\myPipeline
      [Pipeline] node {
      [Pipeline] git
      Cloning the remote Git repository
      remote: Counting objects
      Receiving objects
      Resolving deltas
      Updating references
      Checking out Revision 7dac409d4bd2fec35a4ccde0cb424042cba671df (refs/remotes/origin/master)
      First time build. Skipping changelog.
      [Pipeline] tool
      Unpacking http://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip to C:\Program Files (x86)\Jenkins\tools\hudson.tasks.Maven_MavenInstallation\M3 on Jenkins
      [Pipeline] bat
      [myPipeline] Running batch script
      
      C:\Program Files (x86)\Jenkins\workspace\myPipeline>"C:\Program Files (x86)\Jenkins\tools\hudson.tasks.Maven_MavenInstallation\M3"\bin\mvn -B verify 
      
      Error: JAVA_HOME not found in your environment. 
      Please set the JAVA_HOME variable in your environment to match the 
      location of your Java installation. 
      
      [Pipeline] } //node
      [Pipeline] Allocate node : End
      [Pipeline] End of Pipeline
      ERROR: script returned exit code 1
      Finished: FAILURE
      

      Workaround: I have to declare explicitely JAVA_HOME environment variable in Manage Jenkins -> Configure System -> Global properties section. I set it to "C:\Program Files\Java\jdk1.8.0_77"
      After that the build works correctly

      Clearly I think on Global Tool Configuration, a Maven installation should be bind to a JDK installation to avoid such issue. Concretely in Maven widget a combobox should be present to select a JDK installation already configured above.

          [JENKINS-33966] JAVA_HOME is not set when Maven is automatically installed

          Daniel Beck added a comment -

          Why not have Jenkins also install Java?

          Is Java on your PATH?

          Daniel Beck added a comment - Why not have Jenkins also install Java? Is Java on your PATH?

          Daniel Beck added a comment -

          Straightforward workaround present, therefore reducing priority.

          Also, could probably define JDK installations (with a defined path, doesn't need to be auto-installed), and reference those in the pipeline.

          Daniel Beck added a comment - Straightforward workaround present, therefore reducing priority. Also, could probably define JDK installations (with a defined path, doesn't need to be auto-installed), and reference those in the pipeline.

          Laurent TOURREAU added a comment - - edited

          On Global Tool Configuration, I add a new JDK installation called "JDK-1.8".
          I set the JAVA_HOME field under installation name to C:\Program Files\Java\jdk1.8.0_77 and run the job again -> same result JAVA_HOME is not found.

          Laurent TOURREAU added a comment - - edited On Global Tool Configuration, I add a new JDK installation called "JDK-1.8". I set the JAVA_HOME field under installation name to C:\Program Files\Java\jdk1.8.0_77 and run the job again -> same result JAVA_HOME is not found.

          Daniel Beck added a comment -

          What happens when you call tool 'JDK-1.8' in your pipeline?

          Daniel Beck added a comment - What happens when you call tool 'JDK-1.8' in your pipeline?

          Laurent TOURREAU added a comment - - edited

          I did this as it :

          node {
            git url: 'https://github.com/jglick/simple-maven-project-with-tests.git'
            def mvnHome = tool 'M3'
            tool 'JDK-1.8'
            bat "\"${mvnHome}\"\\bin\\mvn -B verify"
          }
          

          I still get the error:

          [Pipeline] Allocate node : Start
          Running on master in C:\Program Files (x86)\Jenkins\workspace\myPipeline
          [Pipeline] node {
          [Pipeline] git
          Fetching changes from the remote Git repository
          Checking out Revision 7dac409d4bd2fec35a4ccde0cb424042cba671df (refs/remotes/origin/master)
          [Pipeline] tool
          [Pipeline] tool
          [Pipeline] bat
          [myPipeline] Running batch script
          
          C:\Program Files (x86)\Jenkins\workspace\myPipeline>"C:\Program Files (x86)\Jenkins\tools\hudson.tasks.Maven_MavenInstallation\M3"\bin\mvn -B verify 
          
          Error: JAVA_HOME not found in your environment. 
          Please set the JAVA_HOME variable in your environment to match the 
          location of your Java installation. 
          
          [Pipeline] } //node
          [Pipeline] Allocate node : End
          [Pipeline] End of Pipeline
          ERROR: script returned exit code 1
          Finished: FAILURE
          

          If i explicitely set JAVA_HOME, it works

          node {
            git url: 'https://github.com/jglick/simple-maven-project-with-tests.git'
            def mvnHome = tool 'M3'
            env.JAVA_HOME = tool 'JDK-1.8'
            bat "\"${mvnHome}\"\\bin\\mvn -B verify"
          }
          

          Laurent TOURREAU added a comment - - edited I did this as it : node { git url: 'https: //github.com/jglick/simple-maven-project-with-tests.git' def mvnHome = tool 'M3' tool 'JDK-1.8' bat "\" ${mvnHome}\ "\\bin\\mvn -B verify" } I still get the error: [Pipeline] Allocate node : Start Running on master in C:\Program Files (x86)\Jenkins\workspace\myPipeline [Pipeline] node { [Pipeline] git Fetching changes from the remote Git repository Checking out Revision 7dac409d4bd2fec35a4ccde0cb424042cba671df (refs/remotes/origin/master) [Pipeline] tool [Pipeline] tool [Pipeline] bat [myPipeline] Running batch script C:\Program Files (x86)\Jenkins\workspace\myPipeline> "C:\Program Files (x86)\Jenkins\tools\hudson.tasks.Maven_MavenInstallation\M3" \bin\mvn -B verify Error: JAVA_HOME not found in your environment. Please set the JAVA_HOME variable in your environment to match the location of your Java installation. [Pipeline] } //node [Pipeline] Allocate node : End [Pipeline] End of Pipeline ERROR: script returned exit code 1 Finished: FAILURE If i explicitely set JAVA_HOME, it works node { git url: 'https: //github.com/jglick/simple-maven-project-with-tests.git' def mvnHome = tool 'M3' env.JAVA_HOME = tool 'JDK-1.8' bat "\" ${mvnHome}\ "\\bin\\mvn -B verify" }

          Daniel Beck added a comment -

          Not sure that's how it's supposed to work. jglick?

          Daniel Beck added a comment - Not sure that's how it's supposed to work. jglick ?

          Jesse Glick added a comment -

          See discussion in JENKINS-34012.

          Jesse Glick added a comment - See discussion in JENKINS-34012 .

          To be clearer in this ticket, I quote your comment in JENKINS-34012:
          "The tool step merely ensures that the tool is present. Its return value is the path. If you want to set environment variables, that is up to you. JENKINS-28718 would help automate it."

          Laurent TOURREAU added a comment - To be clearer in this ticket, I quote your comment in JENKINS-34012 : " The tool step merely ensures that the tool is present. Its return value is the path. If you want to set environment variables, that is up to you. JENKINS-28718 would help automate it. "

            Unassigned Unassigned
            lautou Laurent TOURREAU
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: