1 overview

Gradle builds faster and is more flexible than Maven. Therefore, many back-end applications are built using Gradle. However, Gradle multi-module projects are difficult to build, and Gradle updates quickly. This makes it difficult to build a multi-module Gradle project.

Based on this starting point, this article provides two forms of Spring Boot multi-module projects built using Gradle:

  • Java + Gradle
  • Kotlin + Gradle + Kotlin DSL

In order to reduce the probability of various errors, the steps are very detailed (multi-graph early warning), the end of the article also attached to the source code, let’s look at it together.

2 the environment

  • Gradle 6.8.2
  • Spring Boot 2.4.3
  • Kotlin 1.4.30
  • Open JDK 11

3 Java + Gradle

Main steps:

  • useSpring InitializerCreate a project
  • Modify thebuild.gradle
  • Create a module
  • Write modules
  • run
  • test

3.1 Creating a Project

To use the Spring Initializer provided by IDEA, choose Gradle:

Rely on:

After the build is complete, delete the SRC directory because the root directory belongs to the admin module directory and does not provide running applications:

3.2 to modifybuild.gradle

This is the most complicated step, and the Gradle version may have different steps. First add an empty subprojects at the bottom:

Move dependencies and test inside:

The final step is to add the plug-in Apply at the beginning of subprojects, adding each of the plugins that are initialized by default.

For example, three plug-ins are used by default:

Apply to subprojects:

3.3 Creating a Module

File -> New -> Module:

Enter the module name. This example creates two modules:

  • service
  • app

After creation, it will look like the following figure:

After creation, delete all of the two modules, only build.gradle repositories:

3.4 Writing modules

3.4.1 trackserviceThe module

Create the package first, according to the group in the root directory:

Then write an @service annotated class called TestService that contains a test method:

Also modify build.gradle of the service module and add bootJar and jar options:

bootJar{
    enabled = false
}

jar{
    enabled = true
}
Copy the code

3.4.2 appThe module

Create package based on root directory group:

Add a dependency on the service module to the app module build.gradle:

Create a startup class and a Controller:

The code is as follows:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) { SpringApplication.run(Application.class,args); }}Copy the code
package com.example.controller;

import com.example.service.TestService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestController {
    private final TestService service;
    @GetMapping("/test")
    public String test(a){
        returnservice.test(); }}Copy the code

3.5 run

To start running, click on the green triangle next to Application:

Or select Application from the run configuration to run (IDEA is automatically created, the original DemoApplication with a × because the startup file has been deleted, you can also delete the configuration) :

If there is no problem, it will run successfully:

Localhost :8080/test

3.6 test

Before creating the test class, you also need to create the package and make sure that the package name is the same as the package name of the launch class:

Create a test class:

package com.example;

import com.example.service.TestService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class JavaTest {
    @Autowired
    private TestService service;
    @Test
    public void test(a){ System.out.println(service.test()); }}Copy the code

Next test:

Building a multi-module Spring Boot project using Java+Gradle is successful.

4 Kotlin + Gradle + Kotlin DSL

The Kotlin DSL is an improvement on the original Gradle (Groovy DSL), but at the same time the syntax is a bit stranger and thus a bit more difficult, but that doesn’t faze me. The basic steps for building multiple modules are similar to above:

  • useSpring InitializerCreate a project
  • Modify thebuild.gradle.kts
  • Create a module
  • Write modules
  • run
  • test

4.1 Creating a Project

Choose the Kotlin + Gradle:

Rely on:

Also delete SRC:

4.2 to modifybuild.gradle.kts

Also add an empty subprojects at the end:

Move in dependencies and Tasks:

Finally, apply at the start of subprojects and apply according to the default plug-in:

The code is as follows:

apply{
    plugin("io.spring.dependency-management")
    plugin("org.springframework.boot")
    plugin("org.jetbrains.kotlin.plugin.spring")
    plugin("org.jetbrains.kotlin.jvm")}Copy the code

Kotlin in plugins is short for org.jetBrains. Kotlin, add it to subprjects.

4.3 Creating a Module

File -> New -> Module, check the necessary options:

Here we also create two modules:

  • app
  • service

Also remove **build.gradle. KTS from the two modules, leaving the rest of repositories** :

4.4 Writing Modules

4.4.1 serviceThe module

First create the package from the build.gradle. KTS directory:

Write TestService:

Tasks. bootJar and tasks.jar:

tasks.bootJar{
    enabled = false
}

tasks.jar{
    enabled = true
}
Copy the code

4.4.2 appThe module

Create package first:

Add a dependency on the service module:

Create a startup class and a Controller:

The code is as follows:

package com.example

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication
class Application

fun main(args:Array<String>) {
    SpringApplication.run(Application::class.java,*args)
}
Copy the code
package com.example.controller

import com.example.service.TestService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class TestController {
    @Autowired
    lateinit var service: TestService
    @GetMapping("/test")
    fun test(a) = service.test()
}
Copy the code

4.5 run

Click on the small green triangle next to main:

Running successfully:

You can also access localhost:8080/test:

4.6 test

Before writing the test, ensure that the test class and the startup class are in the same package, that is, create the package first:

Create a test class:

package com.example

import com.example.service.TestService
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
class KotlinTest {
    @Autowired
    lateinit var service: TestService
    @Test
    fun test(a){
        println(service.test())
    }
}
Copy the code

Click the triangulation test directly:

Test pass, so Kotlin+Gradle+Kotlin DSL multi-module Spring Boot project is created.

5 concludes

The author in the process of practice also met countless mistakes, such as can not find a class, or build the gradle/build gradle. KTS documents errors, fortunately, there are universal search engine, to help the author solved the error, finally success to write this article.

In general, Gradle is more difficult than Maven to create multi-module projects. Gradle updates quickly and syntax changes greatly. Maven is very stable in comparison.

If you really need to use Gradle, you should consider your team’s situation. After all, it is more difficult to get started than Maven. If you encounter some extremely difficult problems while creating multiple Gradle modules, switching to Maven is a good way to do it.

6 source

Attached are two examples of source code:

  • Github
  • Yards cloud
  • CODE CHINA