How to create a Spring Boot 2 multi-project using Gradle.
Java
openjdk 10.0.2 2018-07-17
Gradle
4.10
Spring Boot
2.0.4
|-settings.gradle
|-build.gradle
|
|-foo/
| |-build.gradle
| :
|
`-bar/
|-build.gradle
:
--The foo
project references the bar
project
--The implementation content is not relevant this time, so it is omitted.
/build.gradle
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
}
--Plug-ins etc. are set in common for the entire subproject (foo
, bar
).
--ʻIo.spring.dependency-managementis a guy who made it possible to use the mechanism to control the version of the dependent library using Maven's BOM in Gradle. --Reference: [If you want to use BOM with Gradle, it seems better to use the Dependency management plugin provided by the Spring team --Create something](http://create-something.hatenadiary.jp/entry/2015/05/08) / 063000) --You can omit the version specification of
dependencies related to Spring Boot. --At the time of 1.x, ʻorg.springframework.boot
was used internally, but in 2.x it seems to have been taken out.
/foo/build.gradle
dependencies {
compile project(":bar")
compile "org.springframework.boot:spring-boot-starter-web"
}
--Declare dependency on : bar
and dependency on spring-boot-starter-web
> gradle :foo:compileJava
> Task :foo:compileJava FAILED
...\Foo.java:12:error:Can't find symbol
return new Bar().bar();
^
symbol:Method bar()
place:Class Bar
1 error
FAILURE: Build failed with an exception.
bar
The project class cannot be resolved and an error occurs.
--Check the build result of the bar
project
> dir /b bar\build\
classes
tmp
--bar
Project jar has not been generated
--In 1.x, the jar file bar.jar
was output under bar \ build \ libs
, and I feel that the foo
project was supposed to refer to that jar.
--Somehow, it seems that from 2.x, the jar
plugin ʻenabled is set to
falseby default. - https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#packaging-executable-and-normal --It seems that the
jar task does not generate the jar file, but the
bootJar` task now generates it.
java - Spring Boot multi module project with Gradle doesn't build - Stack Overflow
--Here, the method of setting true
to jar.enabled
is introduced.
-Sure, if you set this, you will be able to build
――However, looking at the official GitHub Issue, the subproject (bar
project) on the dependent side is not a Spring Boot project in the first place, so even the subproject ʻorg.springframework.boot` It is explained that applying the plugin is wrong
――I think that's true, so I'll fix it this way.
/build.gradle
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
}
/foo/build.gradle
apply plugin: "org.springframework.boot"
dependencies {
compile project(":bar")
compile "org.springframework.boot:spring-boot-starter-web"
}
> gradle :foo:compileJava
BUILD SUCCESSFUL in 17s
/build.gradle
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"
}
}
...
}
--Stop setting ʻorg.springframework.bootfor all subprojects --And add
dependencyManagement` to load the Spring Boot BOM
/foo/build.gradle
apply plugin: "org.springframework.boot"
...
--Apply ʻorg.springframework.bootonly to the
foo` project
Recommended Posts