Jenkins: how to set default JDK for all Piplines - java

we have configured a couple JDKs in our Jenkins, which are: JDK8, JDK9, JDK11 etc. The Jenkins itself is running under JDK8. Now, we want to get Jenkins to use JDK9 in all Pipelins, if the "tools" statement isn't in pipeline's definition. Is there a way to do that?
Thank You very much!

Defining a JAVA_HOME environment variable in the Jenkins environment will be enough:
export JAVA_HOME = /path/to/JDK9
if you run Jenkins in a container, just define JAVA_HOME in the docker run command:
docker run -d -p 8080:8080 -p 50000:50000 -v /path/to/JDK9:/internal/path/to/JDK9 --env JAVA_HOME=/internal/path/to/JDK9 jenkins:jenkins

Related

java-8-openjdk-amd64 does not exist in container

I keep receiving the below error when trying to run this hadoop docker container
docker run -it --name psu-hadoop-container -p 2222:22 -p 8042:8042 -p 9864:9864 -p
9870:9870 -p 8088:8088 -p 10000:10000 -p 19888:19888 psu-hadoop
ERROR: JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64 does not exist.
ERROR: JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64 does not exist.
ERROR: JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64 does not exist.
ERROR: JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64 does not exist.
ERROR: JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64 does not exist.
I'm working on a Mac. The DockerFile references the path java-8-openjdk-amd64 but I don't have that specific software, just jdk-19.jdk.
The Dockerfile is from a class and the class primarily uses PC.
Do I need to modify my JAVA_HOME? Do I need to modify the path in the Dockerfile?
The Java version on your host doesn't matter.
If you have an M1/M2 Mac, then they won't run amd64 images unless you emulate it using --platform=linux/amd64.
Sounds like you'll also need to rebuild the Dockerfile with the same.
https://docs.docker.com/build/building/multi-platform/

JAVA_HOME should point to a JDK not a JRE + JAVA_HOME is not defined correctly

I've edited my .bashrc file on my server, with sudo nano ~/.bashrc
export JAVA_HOME=/usr/bin/java
export PATH=${PATH}:${JAVA_HOME}/bin
I've re-logged in, and here is all the path as output:
root#ubuntu-s-1vcpu-1gb-blr1-01:~/maifee/backend-spring# echo $JAVA_HOME
/usr/bin/java
root#ubuntu-s-1vcpu-1gb-blr1-01:~/maifee/backend-spring# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/bin/java/bin
But when I try to run my spring boot server, it gives me:
root#ubuntu-s-1vcpu-1gb-blr1-01:~/maifee/backend-spring# ./mvnw spring-boot:run
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/bin/java/bin/java
And when I try to do a mvn install, I get:
root#ubuntu-s-1vcpu-1gb-blr1-01:~/maifee/backend-spring# mvn install
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE
But here is the java configuration list:
root#ubuntu-s-1vcpu-1gb-blr1-01:~/maifee/backend-spring# sudo update-alternatives --list java
/usr/lib/jvm/java-11-openjdk-amd64/bin/java
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
And when I try to run(just tried, without installing dependencies), I get:
root#ubuntu-s-1vcpu-1gb-blr1-01:~/maifee/backend-spring# ./mvnw spring-boot:run
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/bin/java/bin/java
How can I set up my environment to correctly, run my Spring Boot server? I directly have source code there, I don't even need compiling and stuffs.
JAVA_HOME sould point to the base installation dir of java:
/usr/lib/jvm/java-11-openjdk-amd64/ not /usr/bin/java which probably is a symbolic link to /usr/lib/jvm/java-11-openjdk-amd64/bin/java
then in PATH variable you append $JAVA_HOME/bin
So it should look something like this:
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/
PATH=$JAVA_HOME/bin:$PATH
FYI update-alternatives is only responsible for changing the symlink of java to point it to different versions of the executable,
think of it as a simple ln -s /usr/lib/jvm/java-11-openjdk-amd64/bin/java /usr/bin/java

SonarScanner in Docker cannot find java

I am trying to integrate SonarQube into my CI process. I tried to install SonarScanner in Docker, but it appears it cannot find java tho it can be run
Dockerfile
# Docker, AWS CLI, Terraform, NodeJS
FROM docker:18-dind
RUN echo "1"
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
RUN apk --no-cache add openjdk11 --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community
RUN apk update
RUN apk add awscli nodejs nodejs-npm wget unzip jq openjdk11
RUN wget https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip
RUN unzip terraform_0.11.13_linux_amd64.zip
RUN mv terraform /usr/local/bin
RUN npm i -g sequelize sequelize-cli mysql2
RUN wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.2.0.1873.zip
RUN unzip sonar-scanner-cli-4.2.0.1873.zip
RUN ls sonar-scanner-4.2.0.1873
ENV PATH="/sonar-scanner-4.2.0.1873/bin:${PATH}"
RUN which java
ENV JAVA_HOME=/usr/bin
RUN echo $PATH
RUN java --version
RUN ls /sonar-scanner-4.2.0.1873/bin
RUN sonar-scanner --help
The logs
Step 15/20 : RUN which java
---> Running in 96b487f5b44f
/usr/bin/java
Removing intermediate container 96b487f5b44f
---> 5026de427a57
Step 16/20 : ENV JAVA_HOME=/usr/bin
---> Running in 4d7eee221e41
Removing intermediate container 4d7eee221e41
---> 76b9a8b9d803
Step 17/20 : RUN echo $PATH
---> Running in 4278142f7153
/sonar-scanner-4.2.0.1873/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Removing intermediate container 4278142f7153
---> c3d528bdaa7e
Step 18/20 : RUN java --version
---> Running in 7df7e2d8a441
openjdk 11.0.5 2019-10-15
OpenJDK Runtime Environment (build 11.0.5+10-alpine-r0)
OpenJDK 64-Bit Server VM (build 11.0.5+10-alpine-r0, mixed mode)
Removing intermediate container 7df7e2d8a441
---> 0f7162dae25d
Step 19/20 : RUN ls /sonar-scanner-4.2.0.1873/bin
---> Running in b7efb86cd86e
sonar-scanner
sonar-scanner-debug
sonar-scanner-debug.bat
sonar-scanner.bat
Removing intermediate container b7efb86cd86e
---> ff640a70876f
Step 20/20 : RUN sonar-scanner --help
---> Running in 9164ac2e8369
Could not find 'java' executable in JAVA_HOME or PATH.
The command '/bin/sh -c sonar-scanner --help' returned a non-zero code: 1
Notice which java gives /usr/bin/java. So I set JAVA_HOME=/usr/bin. java can run. But sonar-scanner cannot appear to find java ... why is that?
Add JAVA_HOME ENV according to version, as mentioned #cricket_007 the path in alpine for JDK is /usr/lib/jvm/{VERSION}
ENV JAVA_HOME=/usr/lib/jvm/java-11-openjdk
ENV PATH="$JAVA_HOME/bin:${PATH}"
RUN echo $PATH
run docker
docker run --privileged -it --rm sonar-s sonar-scanner --version
output
INFO: Scanner configuration file: /sonar-scanner-4.2.0.1873/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarQube Scanner 4.2.0.1873
INFO: Java 11.0.5 Alpine (64-bit)
INFO: Linux 4.14.70-67.55.amzn1.x86_64 amd64

Why there are two java path in my PC when i use the "mvn -v"

When I use mvn spring-boot:run to run a Spring Boot project, I find if use the command in Cmd, it works. but if I use it in Git Bash, it can't work.
So I use java -version and mvn -v to view the Java and maven version.
java -version give me the same result, but mvn -v result is different, as the picture shows:
And when I see my java path, I'm sure it's C:\Program Files\Java\jdk1.8.0_151
So I want to know, why there are two Java paths in my PC when I use mvn -v?
Both the java executable and Maven by default use the JDK that is specified in the $JAVA_HOME environment variable.
You are executing java and Maven (mvn) in different environments (cmd.exe in one case and Git Bash in the other case), so the most likely explanation is that $JAVA_HOME is set to different JDKs in the respective environments.
You can check the content of $JAVA_HOME with echo $JAVA_HOME.

How do you make sure that each RUN command installment is working inside a Dockerfile?

I am trying to create a Dockerfile/Image which has all softwares necessary to set up a Java environment.
As per work requirement, I have to use CentOS6 as my base image and Java6, Tomcat6, Apache2.2, Maven 3.2.5 and Eclipse.
Currently I have below as the Dockerfile:
#centos6
FROM centos:6
#Java
RUN yum -y install java-1.6.0-openjdk-devel.x86_64
VOLUME ["/var/www/thml", "/etc/httpd/conf", "/etc/httpd/conf.d", "/tmp/applications", "/usr/local/tomcat", "/usr/local/maven"]
EXPOSE 80 443
ENTRYPOINT [ "/usr/sbin/httpd","-k","start", "-D", "FOREGROUND" ]
# apache
RUN yum -y install httpd
RUN cp -fp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.ORG
RUN sed -i -e 's/\#ServerName www.example.com:80/ServerName example-web-server.example.com/g' /etc/httpd/conf/httpd.conf
I am not certain how to make sure that each RUN command is working correctly as in whether a Java6 environment is set correctly, then an Apache..etc.
Use
RUN echo $JAVA_HOME
And while building the image using
docker build -t $YOUR_IMAGE_NAME
You can see the echo output in the build stdout which is printed on the console.

Categories

Resources