30

I have a number of projects running on a Hudson slave. I'd like one of them to run Ant under Java6, rather than the default (which is Java5 in my environment).

In the project configuration view, I was hoping to find either:

  • An explicit option allowing me to set a custom JDK location to use for this project.
  • A way to set custom environment variables for this project, which would allow me to set JAVA_HOME to the JDK6 location. The would make Ant pick up and run on Java6 as desired.

Is there a way to do either of the above? If one of those facilities is available, I can't see how to access it. I'm running on Hudson 1.285.

I would rather avoid using an "execute shell" operation instead of the "invoke Ant" operation if possible: my slave is on z/OS and Hudson doesn't seem to create the temporary shell scripts properly on this platform (probably an encoding issue).

rewbs
  • 1,958
  • 4
  • 22
  • 34
  • 1
    Might want to toss this one over to the Hudson users mailing list: https://hudson.dev.java.net/mailing-lists.html Configuring slaves might not be very well documented / widely understood here – matt b Apr 14 '09 at 17:13
  • Thanks, I have done so: http://is.gd/szcr . Meanwhile, my hacky workaround is to modify the main Ant script ($ANT_HOME/bin/ant) on the slave to check the JOB_NAME env var and set JAVA_HOME accordingly (the JOB_NAME env var is automatically set by Hudson). – rewbs Apr 15 '09 at 14:32

9 Answers9

27

We have both Java 5 and Java 6 configured for use in our Hudson instance.

Under Manage Hudson -> Configuration System you can add a number of JDKs and specify the path for JAVA_HOME. In the configuration for each job you then selected which JDK you would like that job to run on.

Mark
  • 28,783
  • 8
  • 63
  • 92
  • See second comment on previous answer - it seems this option allows for multiple JDKs on the master, but not on slaves/nodes. – rewbs Apr 14 '09 at 15:14
  • 1
    Missed that it was a slave. I have only ever used Hudson as a master so can't offer any insight there. – Mark Apr 14 '09 at 15:37
  • Also, you don't have a choice of which JDK to use with an ANT task in Hudson. – Volker Stolz Apr 11 '13 at 12:18
  • This is the correct answer but has less points than wrong answer earlier. Here is a thread that touches on this topic: https://groups.google.com/d/msg/jenkinsci-users/p7mUidJfNYQ/MLn9Z_tPZasJ Note JDK selection in job configuration is not available if you have only 1 JDK configured. In such cases the wrong JDK (Default JDK) may be used due to a current Jenkins issue. – Farrukh Najmi Sep 20 '13 at 17:21
25

It turns out that if you make the build parametrised, any string parameters you add become environment variables. With this approach, it is possible to set any environment variable for the build, including JAVA_HOME, which is picked up by Ant.

So the best solution for me was:

  1. In the job configuration page Tick "This build is parameterized"
  2. Add an new String parameter called JAVA_HOME and with the default value set to the JDK location

It's not obvious that build string parameters become environment variables, but once you know that they do, it's easy to set the JDK this way.

The developers on the Hudson mailing list recommended another approach using the master JDK configurations and overrides in the node configurations... but just setting the JAVA_HOME env var seems way easier to me.

Martin Dürrmeier
  • 1,653
  • 5
  • 18
  • 35
rewbs
  • 1,958
  • 4
  • 22
  • 34
  • This answer seems to be wrong. See next answer below for correct answer. – Farrukh Najmi Sep 20 '13 at 17:22
  • 1
    If you are here because you just want to configure a different JDK for some Hudson builds, see the other answer by Mark. However, for the question rewbs asked, configuring a different JDK for builds on a slave, this is the correct answer. – skiphoppy Jul 21 '14 at 19:01
2

We managed this problem by using two different Java Hudson Nodes. One for Java 6 and one for Java 7. Then we assigned the Jobs to the different Nodes according to there needs.

Ralph
  • 118,862
  • 56
  • 287
  • 383
2

Individual slave agents can be configured to use specific JDKs on the system you run them on.

Manage Nodes > Slave > Configure > Environment Variables

For example

Name: JAVA_HOME Value: C:\Program Files (x86)\Java\jdk1.6.0_45\

Obaid
  • 75
  • 2
  • 9
1

The best way is,

  1. Install all required JDKs to your system.
  2. Add those to jenkins under JDK title in configuration page.
  3. Install Maven Info plugin to jenkins
  4. Restart jenkins
  5. Go to your job configuration page.
  6. Select required JDK from the JDK combo box appear under Maven Info Plugin Configuration
  7. Build it
  8. Enjoy!!!
javatar
  • 1,332
  • 1
  • 17
  • 24
0

you can use like this.

in batch command window intially u can set the variable name JAVA_HOME, Assign this custom variable name to PATH variable. then u can call an ant script to choose specific file.

Example:

set JAVA_HOME=C:/java/jdk1.6.1

PATH=%JAVA_HOME%/bin;%PATH%

ant build.xml

0

Have a look at the Setenv Plugin. There you can set Variables like JAVA_HOME=C:/java/jdk1.6.1 PATH=%JAVA_HOME%/bin;%PATH%

0

I had a problem where the installed JDK was JDK 8 whereas I wanted Jenkins to use JDK 7 . So , after installing JDK 7 on the Jenkins build box ,

I added the JDK path to jenkins Configurations :

Jenkins -> Manage Jenkins -> Configure system -> JDK -> Add JDK name and Path

Also , Edit jenkins-runner.sh: Add the line:

export JAVA_HOME=""

Change the last two lines to read:

echo "/bin/java" $javaArgs -jar "$war" $args

exec "/bin/java" $javaArgs -jar "$war" $args

diptia
  • 2,135
  • 21
  • 21
0

A way to set custom environment variables for this project, which would allow me to set JAVA_HOME to the JDK6 location. The would make Ant pick up and run on Java6 as desired.

When configuring the Build steps for Ant, under "Invoke Ant", if you click "Advanced", you can set custom Java options. The on-screen help says:

If your build requires a custom ANT_OPTS, specify it here. Typically this may be used to specify java memory limits to use, for example -Xmx512m. Note that other Ant options (such as -lib) should go to the "Ant targets" field.

I have a feeling this won't work for the JDK to run under, however.

Have you configured multiple JDK installations for this Hudson instance under Manage Hudson / Configure System?

matt b
  • 138,234
  • 66
  • 282
  • 345
  • Your feeling is correct: ANT_OPTS if for custom JVM options to pass to the JDK, and does not affect which JDK to use. However, I did not know about the Configure System view which allows multiple JDK installations. Not sure how this will work for slaves but I'll take a look - thanks! – rewbs Apr 14 '09 at 15:02
  • 2
    Having had a closer look, it seems the JDKs under Manage Hudson -> Configuration System are for JDKs on the master only, not the slaves. Am I missing something - is there a way to set multiple JDKs on a slave? There's no JDK option in the "Configure Node" view on Hudson 1.285. – rewbs Apr 14 '09 at 15:13