关于java:SpringBootGradle构建多模块项目

6次阅读

共计 4676 个字符,预计需要花费 12 分钟才能阅读完成。

1 概述

Gradle因为构建速度比 Maven 快,且比 Maven 灵便,因而很多后端的利用都应用了 Gradle 进行构建,但一个问题是,Gradle的多模块我的项目比拟难构建,再加上 Gradle 的更新十分快,这就给构建一个多模块 Gradle 我的项目造成了不少的艰难。

基于此出发点,本文提供了两种模式的应用 Gradle 构建的 Spring Boot 多模块我的项目:

  • Java + Gradle
  • Kotlin + Gradle + Kotlin DSL

为了缩小呈现各种谬误的概率,步骤做得十分具体(多图预警),文末也附上了源码,上面就一起来看看吧。

2 环境

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

3 Java + Gradle

次要步骤:

  • 应用 Spring Initializer 创立我的项目
  • 批改build.gradle
  • 创立模块
  • 编写模块
  • 运行
  • 测试

3.1 创立我的项目

间接应用 IDEA 提供的 Spring Initializer 即可,构建工具抉择Gradle

依赖:

构建实现后删除 src 目录,因为根目录属于治理模块目录不提供运行的利用:

3.2 批改build.gradle

这是最简单的一步,并且 Gradle 版本更新的话步骤可能会不一样,首先在底部增加一个空的subprojects

接着 dependencies以及 test 挪动进去

最初一步是,subprojects 结尾,增加插件apply,依据默认初始化创立的plugins,逐个增加

比方这里默认应用了三个插件:

applysubprojects 中:

3.3 创立模块

File -> New -> Module

输出模块名即可,这里的例子是创立两个模块:

  • service
  • app

创立好后如图所示:

实现创立之后,把两个模块中的 build.gradle 除了 repositories 之外的全副删去,仅保留repositories

3.4 编写模块

3.4.1 service模块

首先 创立包 ,依据根目录中的group 创立:

接着编写一个叫 TestService 的带 @Service 注解的类,外面蕴含一个 test 办法:

同时批改 service 模块的 build.gradle 增加 bootJar 以及 jar 选项

bootJar{enabled = false}

jar{enabled = true}

3.4.2 app模块

同样先依据根目录的 group 创立包

接着在 app 模块的 build.gradle 增加 service 模块的依赖

再创立启动类以及一个Controller

代码如下:

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);
    }
}
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(){return service.test();
    }
}

3.5 运行

接下来就能够运行了,能够间接点击 Application 旁边的绿色小三角:

或者从运行配置中抉择 Application 运行(IDEA主动创立的,原来的那个 DemoApplication 带一个×是因为启动文件曾经删除了,能够顺便把该配置删除):

没问题的话就能够胜利运行了:

同时浏览器拜访 localhost:8080/test 会呈现 test 字样:

3.6 测试

在创立测试类之前,也须要 先创立包,且须要确保包名与启动类的包名统一

再创立测试类:

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(){System.out.println(service.test());
    }
}

接着进行测试:

这样应用 Java+Gradle 构建一个多模块的 Spring Boot 我的项目就胜利了。

4 Kotlin + Gradle + Kotlin DSL

Kotlin DSL在原生GradleGroovy DSL)的根底上进行改良,但同时语法也变得更加生疏,难度因而也加大了不少,不过这并没有难倒笔者。构建多模块的根本步骤与下面相似:

  • 应用 Spring Initializer 创立我的项目
  • 批改build.gradle.kts
  • 创立模块
  • 编写模块
  • 运行
  • 测试

4.1 创立我的项目

抉择Kotlin+Gradle

依赖:

同样 删除src

4.2 批改build.gradle.kts

同样在尾部 增加一个空的subprojects

dependencies 以及 tasks 挪动进去

最初 subprojects开始处 apply 插件 依据默认的插件进行apply

代码如下:

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

plugins中的 kotlinorg.jetbrains.kotlin的简写 ,在subprjects 中留神加上即可。

4.3 创立模块

File -> New -> Module,把一些必要选项勾选上:

这里同样创立两个模块:

  • app
  • service

同样把两个模块中的 build.gradle.kts 删除其余局部留下repositories

4.4 编写模块

4.4.1 service模块

首先依据根目录的 build.gradle.kts 创立包

编写TestService

最初批改 build.gradle.kts 加上 tasks.bootJartasks.jar

tasks.bootJar{enabled = false}

tasks.jar{enabled = true}

4.4.2 app模块

创立包

增加对 service 模块的依赖

再创立一个启动类以及一个Controller

代码如下:

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)
}
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() = service.test()
}

4.5 运行

点击 main 旁边的绿色小三角即可:

运行胜利:

同样能够拜访localhost:8080/test

4.6 测试

留神在编写测试之前须要保障测试类与启动类在同一个包下,也就是须要 先创立包

再创立测试类:

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(){println(service.test())
    }
}

间接点击小三角测试即可:

测试通过,这样 Kotlin+Gradle+Kotlin DSL 的多模块 Spring Boot 我的项目就算创立实现了。

5 总结

笔者在实际的过程中也遇到了有数的谬误,比方找不到类,或者 build.gradle/build.gradle.kts 文件谬误,幸好有万能的搜索引擎,帮笔者解决了谬误,最初才胜利写下这篇文章。

总的来说,Gradle创立多模块我的项目要比 Maven 要难,而且 Gradle 的更新速度很快,语法变动较大,相比之下 Maven 十分稳固,最新的 Maven 3.6.3 还是 19 年 11 月公布的,然而 Gradle 都筹备 7.0 了:

笔者倡议,如果是真的须要应用 Gradle,须要考虑一下团队的情况,毕竟上手难度要大于Maven,如果在Gradle 创立多模块的过程中遇到一些极其难以解决的问题,转为 Maven 不失为一个好方法。

6 源码

附上两个例子的源码:

  • Github
  • 码云
  • CODE CHINA
正文完
 0