[JAVA] Spring Boot 2 multi-project in Gradle

How to create a Spring Boot 2 multi-project using Gradle.

environment

Java

openjdk 10.0.2 2018-07-17

Gradle

4.10

Spring Boot

2.0.4

Target multi-project

|-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.

Bad pattern setting

/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 ofdependencies 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

Compile result

> 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.

Description

--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 thejar task does not generate the jar file, but the bootJar` task now generates it.

Workaround

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

Gradle multi-project build dependencies of subprojects cannot be resolved · Issue #11594 · spring-projects/spring-boot

――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.

OK pattern

/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"
}

Compile result

> gradle :foo:compileJava
BUILD SUCCESSFUL in 17s

Description

/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 adddependencyManagement` to load the Spring Boot BOM

/foo/build.gradle


apply plugin: "org.springframework.boot"

...

--Apply ʻorg.springframework.bootonly to thefoo` project

Recommended Posts

Spring Boot 2 multi-project in Gradle
Implement Spring Boot application in Gradle
Build Spring Boot + Docker image in Gradle
Java tips-Create a Spring Boot project in Gradle
I wanted to gradle spring boot with multi-project
View the Gradle task in the Spring Boot project
Set context-param in Spring Boot
Major changes in Spring Boot 1.5
NoHttpResponseException in Spring Boot + WireMock
Spring Boot Hello World in Eclipse
Spring Boot application development in Eclipse
◆ Spring Boot + gradle environment construction memo
Write test code in Spring Boot
Make multi-project executable JAR in Gradle
Implement REST API in Spring Boot
What is @Autowired in Spring boot?
Thymeleaf usage notes in Spring Boot
Spring Boot gradle build with Docker
Launch (old) Spring Boot project in IntelliJ
Static file access priority in Spring boot
Output Spring Boot log in json format
Local file download memorandum in Spring Boot
Challenge Spring Boot
Create Java Spring Boot project in IntelliJ
Loosen Thymeleaf syntax checking in Spring Boot
[Practice! ] Display Hello World in Spring Boot
Use DynamoDB query method in Spring Boot
Spring Boot Form
Configure a multi-project with subdirectories in Gradle
Spring Boot, Doma2, Gradle initial setting summary
Spring Boot Memorandum
gae + spring boot
DI SessionScope Bean in Spring Boot 2 Filter
(IntelliJ + gradle) Hello World with Spring Boot
Add spring boot and gradle to eclipse
Change session timeout time in Spring Boot
Create a website with Spring Boot + Gradle (jdk1.8.x)
SameSite cookie in Spring Boot (Spring Web MVC + Tomcat)
Test controller with Mock MVC in Spring Boot
Asynchronous processing with regular execution in Spring Boot
Run a Spring Boot project in VS Code
Run Scala applications with Spring Boot through Gradle
Use Servlet filter in Spring Boot [Spring Boot 1.x, 2.x compatible]
How to add a classpath in Spring Boot
Build Spring Boot project by environment with Gradle
How to bind to property file in Spring Boot
Annotations used in Spring Boot task management tool
Inject Logger in Spring
SPRING BOOT learning record 01
Spring Boot + Heroku Postgres
Spring boot memo writing (1)
SPRING BOOT learning record 02
Spring Boot2 cheat sheet
Spring Boot exception handling
Spring Boot Servlet mapping
Spring boot development-development environment-
Spring Boot learning procedure
Use Interceptor in Spring
Learning Spring Boot [Beginning]
Microservices in Spring Cloud
Spring boot memo writing (2)