skip to Main Content

I am using Gitlab to build a Java tool using ant

The tool requires JDK 17, but ant JDK version is 11, and I’m trying to change it.
So I tried a lot of solutions using a remote repository or remote download site, but after some tries I found out that the VM used to build the tool is not connected to internet (trying to ping google or my IP address doesn’t work).

So I tried to upload in the same package with the tool source code the JDK 17 (openjdk-17_linux-x64_bin.tar.gz) and install it there.
Here is the problem, I am not sure how to do this since I don’t work with linux, but I tried almost everything on the internet.

Every of these commands are used in a .gitlab-ci.yml file, used for gitlab pipeline.

Here are some examples of what I’ve tried so far:


    - sudo cp /builds/project/openjdk-17_linux-x64_bin.tar.gz /usr/lib/jvm
    - sudo tar zxvf "/usr/lib/jvm/openjdk-17_linux-x64_bin.tar.gz" -C /usr/lib/jvm
    - echo "JAVA_HOME=/usr/lib/jvm/jdk-17" | sudo tee -a /etc/profile
    - echo "PATH=${PATH}:${HOME}/bin:${JAVA_HOME}/bin" | sudo tee -a /etc/profile
    - echo "export JAVA_HOME" | sudo tee -a /etc/profile
    - echo "export JRE_HOME" | sudo tee -a /etc/profile
    - echo "export PATH" | sudo tee -a /etc/profile
    - sudo cat /etc/profile
    - echo "JAVA_HOME=/usr/lib/jvm/jdk-17" | sudo tee -a /.bashrc
    - echo "PATH=${PATH}:${JAVA_HOME}/bin" | sudo tee -a /.bashrc
    - echo "JAVA_HOME='/usr/lib/jvm/jdk-17' | sudo tee -a /etc/environment"
    
    - export JAVA_HOME=/usr/lib/jvm/jdk-17
    - export PATH=$PATH:$JAVA_HOME/bin

After a lot of combinations of these commands the output of sudo update-alternatives --config java is still:

openjdk version "11.0.12" 2021-07-20
OpenJDK Runtime Environment (build 11.0.12+7-post-Debian-2deb10u1)
OpenJDK 64-Bit Server VM (build 11.0.12+7-post-Debian-2deb10u1, mixed mode, sharing)

But if I try /usr/lib/jvm/jdk-17/bin/java -version it prints 17.
What would be the solution of making the default Java version to be 17. (Also a solution for ant to use the JDK-17 without installing it would be great too, since I need the JDK-17 for ant)

2

Answers


  1. Chosen as BEST ANSWER

    I found a solution.

    
        - sudo cp jdk-17-linux-x64.tar.gz /usr/lib/jvm
        - sudo tar zxvf "/usr/lib/jvm/jdk-17-linux-x64.tar.gz" -C /usr/lib/jvm
        - sudo cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/java-1.11.0-openjdk-amd64
        - sudo cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/default-java
        - sudo cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/java-11-openjdk-amd64
        - sudo cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/openjdk-11
        - sudo update-alternatives --remove-all java
        - sudo update-alternatives --remove-all javac
        - sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17/bin/java 1
    



    What I did here was to copy the JDK-17 content to all folders from /usr/lib/jvm folder. So even though the docker image uses JDK-11, I'm rewriting it using JDK-17 uploaded with the source code, and now the tool is built using JKD-17.
    PS: I know this is slower and not professional, but in my case, it's easier and more convinient than trying to get help from those who setup the docker container.


  2. Since you’ve already found a way to change the jdk on-the-go, you may really want to consider change the base image of your CI to save yourself a lot of time. This step will boost your CI speed. The steps to do that is fairly simple too.

    1. Compose your own Dockerfile

    This following is just a pesudo code. You may look into the description of dockerfile builder

    FROM your-original-image. This is what you have in your image tag in the gitlab-ci file.
    
    COPY jdk-17-linux-x64.tar.gz /usr/lib/jvm
    RUN sudo tar zxvf "/usr/lib/jvm/jdk-17-linux-x64.tar.gz" -C /usr/lib/jvm 
        && sudo cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/java-1.11.0-openjdk-amd64 
        && sudo cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/default-java 
        && sudo cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/java-11-openjdk-amd64 
        && sudo cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/openjdk-11 
        && sudo update-alternatives --remove-all java 
        && sudo update-alternatives --remove-all javac 
        && sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17/bin/java 1
    
    1. Build the docker image
    • If you are using docker hub, then you need to login to docker and get a dockerId which matches the dockerId in the snippet.
    • If you are using a private repo like harbor or artifactory, you may need the permission to push to it.
    docker build . -t dockerId/Name-of-your-image-you-want:latest
    
    1. Upload the docker image using docker push
    docker push dockerId/Name-of-your-image-you-want:latest
    
    1. change the image tag in your gitlab-ci.yaml to dockerId/Name-of-your-image-you-want:latest
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search