It appears that maven overrides Java 8 with Java 7. Consider the following Dockerfile:
FROM java:8
RUN java -version && ls -l /usr/bin/java
RUN apt-get update -y && apt-get install maven -y
RUN java -version && ls -l /usr/bin/java
Line two will report the java version is 1.8 but line 4 will report java version is 1.7. In both cases the /usr/bin/java
symlink points to /etc/alternatives/java
Besides re-installing Java 8 (which is why I started with Java:8 in the first place), how can I undo these side-effects of installing maven when building a docker image?
You can use the Java 11 image from hub.docker.com by typing the command :docker pull openjdk:tag on your machines terminal, where the tag is version of your intended java version. Or you can simply specify the image on your Dockerfile where FROM attribute must be the version of java.
Docker belongs to "Virtual Machine Platforms & Containers" category of the tech stack, while Apache Maven can be primarily classified under "Java Build Tools". Some of the features offered by Docker are: Integrated developer tools. open, portable images.
I found a minimal-delta solution although the point about not using apt-get for maven installs is noted. Here is the solution as the code
FROM java:8
# preserve Java 8 from the maven install.
RUN mv /etc/alternatives/java /etc/alternatives/java8
RUN apt-get update -y && apt-get install maven -y
# Restore Java 8
RUN mv -f /etc/alternatives/java8 /etc/alternatives/java
RUN ls -l /usr/bin/java && java -version
Obviously, the last line is unnecessary but does confirm that the result is java 8.
Your problem isn't Maven, it's some dumb decision made by the person who packaged Maven into a .deb for APT. Do not use Maven from a .deb. The Apache Maven project doesn't make these, doesn't know what's in them, and does not support them very much. Download the genuine tar.gz from maven.apache.org, it will happily work with whatever version of Java you've got.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With