Run Spring Boot Maven projects using Java 9 and Docker

Run Spring Boot Maven projects using Java 9 and Docker

Java 9 is about to release soon. Frameworks and tools continue to add support for this new Java 9 release. Spring framework has already provides support for Java 9. In this post, we are going to run Spring projects using Java 9 and Docker.

First of all, we create a new Spring Boot project using Spring Initializr. Here we add only the dependency Web and use the artifact name webdemo. Download and extract the created project into the local machine and we are good to go.

We'll continue to use the Docker image 3.5.0-jdk-9 with JDK 9 build 170 to run this project.

$ docker run -it --rm --name maven-java9 \
  -v "$PWD":/usr/src/java9 \
  --volumes-from maven_data \
  -w /usr/src/java9 maven:3.5-jdk-9 \
  mvn package

maven_data is the named data container we created to cache downloaded Maven artifacts.

$ docker run --name maven_data \
  -v /root/.m2 \
  maven:3.5-jdk-9 echo 'data for maven'

When we run mvn package in the container, it throws an error.

Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
	at org.codehaus.plexus.archiver.zip.AbstractZipArchiver.<clinit>(AbstractZipArchiver.java:116)
	... 88 more
[WARNING] Error injecting: org.apache.maven.plugin.jar.JarMojo
java.lang.ExceptionInInitializerError

It turns out that the 2.6 version of Maven Jar Plugin used in the Spring Boot project doesn't work with Java 9, so we need to upgrade it to 3.0.2 by updating the pom.xml.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.0.2</version>
    </plugin>
  </plugins>
</build>

Now we can run the mvn package successfully. Then we start the Spring Boot project using java.

$ docker run -it --rm --name maven-java9 \
  -v "$PWD":/usr/src/java9 \
  --volumes-from maven_data \
  -p 8080:8080 \
  -w /usr/src/java9 maven:3.5-jdk-9 \
  java -jar target/webdemo-0.0.1-SNAPSHOT.jar

Now we can navigate to http://localhost:8080 to see the default error page.

© 2023 VividCode