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.

1
2
3
4
5
$ 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.

1
2
3
$ 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.

1
2
3
4
5
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
trueat org.codehaus.plexus.archiver.zip.AbstractZipArchiver.<clinit>(AbstractZipArchiver.java:116)
true... 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.

1
2
3
4
5
6
7
8
9
<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.

1
2
3
4
5
6
$ 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.

Exploring Java 9

Check out my book Exploring Java 9 for new features in Java 9.

Comments