spring-boot
Java10

Java 10でSpring Boot 2.0 アプリケーションを開発するときの初歩的な注意点

概要

SPRING Initializrで生成したSpring Boot 2.0のアプリケーションのひな型をJava 10でコンパイル、実行するために必要な環境設定などのメモです。
ひな型をベースにしているのでこの記事で取り上げているのは初歩的なものです。プロダクトの複雑なアプリケーションの場合はもっと多くの注意点が出てくるかもしれません。

環境

  • Windows 10 Professional
  • Oracle JDK 10.0.1
  • Spring Boot 2.0.2
  • Maven 3.5.3

java

> java -version
java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)

maven

> mvn.cmd -version
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T04:49:05+09:00)
Maven home: D:\dev\apache-maven-3.5.3\bin\..
Java version: 10.0.1, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk-10.0.1
Default locale: ja_JP, platform encoding: MS932
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

参考

Java 10のサポートについて

まず、Spring Boot 2.0で開発したアプリケーションはJava 10で動作するのかという点ですが、Spring Boot with Java 9 and aboveのRequirementsに記載がある通り、Spring Boot 2.0.1でJava 10での動作はサポートされています。

Requirements

Spring Boot 2 is the first version to support Java 9 (Java 8 is also supported). If you are using 1.5 and wish to use Java 9 you should upgrade to 2.0 as we have no plans to support Java 9 on Spring Boot 1.5.x.

Java 10 is supported as of Spring Boot 2.0.1.RELEASE. We intend to support Java 11 as of Spring Boot 2.1.x

注意点について

Java 10 (および9)では、Java 1.8で必要がなかったいくつかの注意点があります。

Illegal reflective access

アプリケーションを起動すると、起動時のログに下記のワーニングが出力されます。
Java 9から発生していたことですが、Java 10でも同様に発生します。

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

Spring Frameworkはリフレクションを使用しているので、このワーニングは想定内(不正ではない)のようです。(また、将来的に改善されるようです。MethodHandles.Lookup.defineClass for CGLIB class definition purposes
たんにワーニングの出力を抑制したい場合は、JVM Optionに下記のオプションを追加します。

--illegal-access=deny

jar実行時

> java --illegal-access=deny -jar demo.jar

JAXB

プロジェクトでJPA (Hibernate)を利用する場合は依存関係にJAXBを追加する必要があります。

Hibernate typically requires JAXB that’s no longer provided by default.

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
</dependency>

Lombokのバージョン指定

Spring Boot 2.0.2のデフォルトではlombokのバージョンは1.16.20が利用されますが、Java 10ではlombokのバージョンは1.16.22 (2018年6月時点)を指定する必要があります。

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
    <version>1.16.22</version>
</dependency>

2018年6月5日にlombok 1.18.0がリリースされました(changelog)が、こちらのPR(Update Lombok to 1.18.0)によれば、Spring Bootが正式に採用するのはSpring Boot 2.1からのようです。

ちなみに、Spring Bootの依存管理からlombokを除外するかどうかを検討するIssues "Consider removing lombok from dependency management"があります。若しかすると将来のバージョンではlombokを利用する場合はバージョンを明示しなければならないかもしれません。

maven-surefire-pluginのダウングレードは不要

Java 9ではmaven-surefire-pluginをバージョン2.20.0へダウングレードする必要がありましたが、バージョン2.21.0で問題が解決されたのでダウングレードする必要はなくなりました。
また、Spring Boot 2.0.2ではバージョン2.21.0がデフォルトになったのでバージョンを明示する必要もありません。

Outdated wiki reference on Spring Boot with Java 9

Open中のIssues

補足

OpenJDK 9 and 10のバグ

サポート状況

Spring Bootの依存関係管理に含まれるライブラリ、フレームワーク等のサポート状況です。Spring Boot 2.0.1からJava 10をサポートしているので、依存関係管理に含まれるライブラリ、フレームワークもJava 10で動作すると思いますが、それぞれのサイトでJava 10のサポートが明文化されているか、制約があるか、動作保証されている最低バージョンなどを確認しています。

  • 確認する範囲が広いのでごく簡単にざっくりした感じで調べています。また一度に調べ切れないので逐次更新していく予定です。

quartz

TODO

lombok

TODO

シリアライズ / デシリアライズ ライブラリ

Jackson

Jackson Release 2.9

No changes from 2.8: Java 7 features available for all modules (meaning, need to compile on Java 7)

GSON

Change Log

Version 2.8.5

Fixed issue https://github.com/google/gson/issues/1310 by supporting Debian Java 9

データベース関連

Hibernate ORM

Getting started with Hibernate ORM

Hibernate 5.2 and later versions require at least Java 1.8 and JDBC 4.2.
Hibernate 5.1 and older versions require at least Java 1.6 and JDBC 4.0.

JOOQ

TODO

MySQL Connector/J

Chapter 2 Connector/J Versions, and the MySQL and Java Versions They Support

Table 2.1 Summary of Connector/J Versions

Connector/J version JDBC version MySQL Server version JRE Supported JDK Required for Compilation Status
8.0 4.2 5.5, 5.6, 5.7, 8.0 1.8.x 1.8.x General Availability
5.1 3.0, 4.0, 4.1, 4.2 5.5, 5.6*, 5.7*, 8.0* 1.5.x, 1.6.x, 1.7.x, 1.8.x* 1.5.x and 1.8.x General Availability
  • JRE 1.8.x is required for Connector/J 5.1 to connect to MySQL 5.6, 5.7, and 8.0 with SSL/TLS when using some cipher suites.

PostgreSQL JDBC Driver

PostgreSQL JDBC Download

Current Version 42.2.2

  • If you are using Java 8 or newer then you should use the JDBC 4.2 version.
  • If you are using Java 7 then you should use the JDBC 4.1 version.
  • If you are using Java 6 then you should use the JDBC 4.0 version.
  • If you are using a Java version older than 6 then you will need to use a JDBC3 version of the driver, which will by necessity not be current, found in Other Versions.

データベースマイグレーション

flyway

Release Notes

Flyway 5.1.0 (2018-05-24)

Issue 2019 Java 10 support

Flyway 5.0.7 (2018-01-30)

Issue 1912 Flyway now requires the Java 9 compiler to build. Java runtime compatibility is unchanged.

liquibase

TODO

APPサーバー

Tomcat

Apache Tomcat Versions

Apache Tomcat Version Latest Released Version Supported Java Versions
9.0.x 9.0.8 8 and later
8.5.x 8.5.31 7 and later

Undertow

TODO

Jetty

What Version Do I Use?

Version JVM
9.4 1.8
9.3 1.8

ロギングフレームワーク

Log4J

Apache Log4j 2

Log4j 2.4 and greater requires Java 7, versions 2.0-alpha1 to 2.3 required Java 6. Some features require optional dependencies; the documentation for these features specifies the dependencies.

Logback

Dependencies per module

As of version 1.1.3, logback requires JDK 1.6.

SLF4J

Frequently Asked Questions about SLF4J

What are SLF4J's requirements?

As of version 1.7.0, SLF4J requires JDK 1.5 or later. Earlier SLF4J versions, namely SLF4J 1.4, 1.5. and 1.6, required JDK 1.4.

テスティングフレームワーク、モック、アサーション

JUnit

TODO

Mockito

What's new in Mockito 2

full-blown Java 8 support, and Java 9 support (depending on the release date)

AssertJ

TODO

ビルドツール

Maven

Downloading Apache Maven 3.5.3

Maven 3.3+ require JDK 1.7 or above to execute - they still allows you to build against 1.3 and other JDK versions by Using Toolchains

Gradle

Gradle 4.7 Release Notes

Java enthusiasts will be happy to read that this release supports running Gradle builds with JDK 10.

Java 11

Java 11 support

IDE

IDEで確認するバージョンは下記の2点です。

  • 開発、コンパイルで使用するバージョン (コードアシストや実行、コンパイルで使用する)
  • IDEの実行環境のバージョン (IDE自体が使用する)

Eclipse

Eclipse Oxygen.3a IDE Improvements: Java 10

Three weeks after the Oxygen.3 and the Java 10 release, Oxygen.3a now adds official support for Java 10 which was previously offered only as a pre-release via the Eclipse Marketplace.

実行環境のバージョン

環境変数JAVA_HOMEに設定したJREがEclipseの実行環境となります。

> echo %JAVA_HOME%
D:\openjdk\jdk-10.0.1

> java -version
openjdk version "10.0.1" 2018-04-17
OpenJDK Runtime Environment (build 10.0.1+10)
OpenJDK 64-Bit Server VM (build 10.0.1+10, mixed mode)

確認するにはメニューのHelp → About Eclipse → Installation Details → Configurationの-vmの次の行を見ます。
JAVA_HOMEにOpenJDK 10.0を指定した場合は下記のようになります。

-vm
D:\openjdk\jdk-10.0.1\bin\server\jvm.dll

任意のJREを指定する場合はeclipse.iniファイルの-vmargsの直前に下記の2行を追加します。この例ではOpenJDK 9.0.4を指定しています。

//... 省略
-vm                                          // 追加
D:\openjdk\jdk-9.0.4\bin\server\jvm.dll      // 追加
-vmargs
//... 省略

IntelliJ IDEA

実行環境のバージョン

Install and set up IntelliJ IDEA

JRE 1.8 is bundled with the IntelliJ IDEA distribution. You do not need to install Java on your computer to run IntelliJ IDEA.

A standalone JDK is required for Java development.

Boot JDKを切り替えることができますが、通常はバンドルされているJREのバージョンを変える必要はありません。

Ctrl + Shift + Aでアクションサーチから"switch"と入力し、サジェストで"Switch Boot JDK..."を選びます。

b.png

試しにOpenJDK 10.0に切り替えたところ、イベントログに下記のメッセージが出力されました。

Please consider switching to the bundled Java runtime that is better suited for the IDE (your current Java runtime is 10.0.1+10 by Oracle Corporation).