使用Gradle构建Spring Boot 2的多模块项目
使用Gradle创建Spring Boot 2的多模块项目的方法。
保護很重要。
Java (only need one option) 爪哇
2018年7月17日发布的OpenJDK 10.0.2
Gradle
四点十分
春季引导程序
2.0.4可以用中文翻译为“二点零点四”。
目标的多项目管理
|-settings.gradle
|-build.gradle
|
|-foo/
| |-build.gradle
| :
|
`-bar/
|-build.gradle
:
foo プロジェクトが bar プロジェクトを参照している
実装内容は今回関係ないので省略
无法设置的模式
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
subprojects {
apply plugin: "java"
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
repositories {
mavenCentral()
}
sourceCompatibility = 10
targetCompatibility = 10
}
-
- サブプロジェクト全体 (foo, bar) に共通でプラグインなどを設定している。
io.spring.dependency-management は、 Maven の BOM を利用して依存するライブラリのバージョンを制御する仕組みを Gradle でも使えるようにしたやつ
参考:Gradle で BOM を使いたいときには Spring チームの出している Dependency management plugin を使うのがよさそう – なにか作る
Spring Boot 関係の dependencies のバージョン指定を省略できるようになる
1.x の頃は org.springframework.boot が内部で利用していたが、 2.x では外に出されたっぽい
dependencies {
compile project(":bar")
compile "org.springframework.boot:spring-boot-starter-web"
}
:bar への依存と、 spring-boot-starter-web への依存を宣言
编译结果
> gradle :foo:compileJava
> Task :foo:compileJava FAILED
...\Foo.java:12: エラー: シンボルを見つけられません
return new Bar().bar();
^
シンボル: メソッド bar()
場所: クラス Bar
エラー1個
FAILURE: Build failed with an exception.
由于无法解决 bar 项目的类而出现错误。
解释
bar プロジェクトのビルド結果を確認してみる
> dir /b bar\build\
classes
tmp
bar プロジェクトの jar が生成されていない
1.x なら、 bar\build\libs の下に bar.jar という jar ファイルが出力されており、 foo プロジェクトはその jar を参照するようになっていた気がする
なんか、 2.x からはデフォルトで jar プラグインの enabled が false に設定されるようになったらしい
https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#packaging-executable-and-normal
jar タスクでは jar ファイルは生成されず、 bootJar タスクで生成されるようになったらしい
处理方法 (Duì chǔ
在Stack Overflow上,Java – Spring Boot多模块项目与Gradle构建失败。
-
- ここでは、 jar.enabled に true を設定すればええでという方法が紹介されている
- たしかに、これを設定すればビルドできるようになる
Gradle 多项目构建中,无法解析子项目的依赖关系 · 问题 #11594 · spring-projects/spring-boot
-
- ただ、公式の GitHub Issue の方を見ると、そもそも依存されている側のサブプロジェクト(bar プロジェクト)の方は Spring Boot プロジェクトではないのだから、サブプロジェクトにまで org.springframework.boot プラグインを適用することが間違っていると説明されている
- 確かにその通りだと思うので、こっちの方法で修正する
好的模式
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
subprojects {
apply plugin: "java"
apply plugin: "io.spring.dependency-management"
repositories {
mavenCentral()
}
dependencyManagement {
imports {
mavenBom "org.springframework.boot:spring-boot-dependencies:$springBootVersion"
}
}
sourceCompatibility = 10
targetCompatibility = 10
}
apply plugin: "org.springframework.boot"
dependencies {
compile project(":bar")
compile "org.springframework.boot:spring-boot-starter-web"
}
编译结果
> gradle :foo:compileJava
BUILD SUCCESSFUL in 17s
解释
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
...
}
subprojects {
apply plugin: "java"
apply plugin: "io.spring.dependency-management"
...
dependencyManagement {
imports {
mavenBom "org.springframework.boot:spring-boot-dependencies:$springBootVersion"
}
}
...
}
-
- 全てのサブプロジェクトに org.springframework.boot を設定するのをやめる
- そして、 dependencyManagement を追加して Spring Boot の BOM を読み込む
apply plugin: "org.springframework.boot"
...
foo プロジェクトのほうにだけ org.springframework.boot を適用する