It’s time! Migrating to Java 11

Why you should migrate to next Java now!

Leonardo Zanivan
Jul 31, 2018 · 7 min read
Duke jumping to Java 11

TL;DR;

Motivation

Source: Oracle

Will Java be paid?

Migration Steps

1. Run an existing Java application with JDK 11

Why upgrade from JDK 8 to JDK 11?

Running your application

2. Compile the application with Java 11

Why upgrade source to Java 11?

Steps

git clone git@github.com:spring-projects/spring-petclinic.git
<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.23.1-GA</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.20.0</version>
<scope>test</scope>
</dependency>
<plugin>
...
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.20.0</version>
</dependency>

...
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<release>${java.version}</release>
</configuration>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>6.2</version>
</dependency>

</dependencies>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>6.2</version>
</dependency>

</dependencies>
</plugin>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.4.0-b180608.0325</version>
</dependency>
<repositories>
<repository>
<id>jvnet-nexus-staging</id>
<url>http://maven.java.net/content/repositories/staging/</url>
<layout>default</layout>
</repository>
</repositories>
./mvnw spring-boot:run
./mvnw clean package
java -jar target/spring-petclinic-2.0.0.BUILD-SNAPSHOT.jar

3. Modularize the application to use Module System

Why migrate to Module System?

Steps

module spring.petclinic {
}
Error:(19, 27) java: package org.springframework.boot is not visible
(package org.springframework.boot is declared in module spring.boot, but module spring.petclinic does not read it)
./mvnw compile org.apache.maven.plugins:maven-dependency-plugin:3.1.1:resolve
open module spring.petclinic {
requires cache.api;

requires java.activation;
requires java.instrument;
requires java.persistence;
requires java.sql;
requires java.transaction;
requires java.validation;
requires java.xml.bind;

requires org.hibernate.validator;

requires spring.beans;
requires spring.boot;
requires spring.boot.autoconfigure;
requires spring.context;
requires spring.core;
requires spring.data.commons;
requires spring.data.jpa;
requires spring.tx;
requires spring.web;
requires spring.webmvc;

requires jdk.unsupported;
}
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<outputDirectory>
${project.build.directory}/modules
</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/modules
</outputDirectory>
<includeScope>runtime</includeScope>
<excludeArtifactIds>
spring-boot-devtools
</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.2_spec</artifactId>
<version>1.1.1.Final</version>
</dependency>
<exclusions>
<exclusion>
<artifactId>javax.transaction-api</artifactId>
<groupId>javax.transaction</groupId>
</exclusion>
</exclusions>
<configuration>
<forkCount>0</forkCount>
</configuration>
./mvnw clean package
java --add-opens java.base/java.lang=spring.core,javassist \--module-path target/modules \--module spring.petclinic/org.springframework.samples.petclinic.PetClinicApplication
jar --update \
--file=target/modules/spring-petclinic-2.0.0.BUILD-SNAPSHOT.jar \
--main-class=org.springframework.samples.petclinic.PetClinicApplication
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>module-main-class</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>jar</executable>
<arguments>
<argument>
--update
</argument>
<argument>
--file=${project.build.directory}/modules/${project.build.finalName}.jar
</argument>
<argument>
--main-class=org.springframework.samples.petclinic.PetClinicApplication
</argument>
<argument>
--module-version=${project.version}
</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
./mvnw clean packagejava --add-opens java.base/java.lang=spring.core,javassist \--module-path=target/modules \--module spring.petclinic

Congratulations!

criciumadev

Comunidade de desenvolvedores do Sul de Santa Catarina