如何使用Gradle + Spring Boot 1.5构建子模块的可执行jar

时间:2018-08-02 03:27:44

标签: java spring-boot gradle

我有一个多模块项目:

Root project 'platform'
+--- Project ':api'
+--- Project ':common'

:common模块中,我包括所有依赖项,在:api模块中,我只有

apply(plugin = "org.springframework.boot")
dependencies {
  implement(project(":common"))
}

问题是,当我构建:api模块时,从jar文件中看不到jar文件中的任何依赖项,只有BOOT-INF/libs/没有BOOT-INF/classes/。 当我使用java -jar运行jar时,它对:common模块中的类之一说NoClassFound。

gradle :api:bootRun工作正常。

我还有其他配置吗? 我正在使用Gradle 4.9 Kotlin DSL和Spring Boot插件1.5.15.RELEASE

2 个答案:

答案 0 :(得分:0)

原来是由于spring-boot-plugin 1.5.x某种原因无法识别implement(project(:common)),通过更改为compile(project(:common))可以正常工作。

答案 1 :(得分:0)

原因是spring boot 1.5 gradle插件仅针对gradle 2和3,不支持implementation配置(在gradle 4中引入)。

基于this documentation,它描述了默认情况下仅包含compileruntime配置。话虽如此,可以包含一个自定义配置以使其工作。

如果查看gradle 5 Illustrated here的gradle配置层次结构,runtimeClasspath是实现的基础,因此具有可运行jar所需的所有依赖项。

这意味着对于Spring Boot 1.5,您可以将其指向自定义配置,以使其正确构建可运行的jar:

build.gradle:

bootRepackage {
    customConfiguration = 'runtimeClasspath'
}

build.gradle.kts:

import org.springframework.boot.gradle.repackage.RepackageTask

// more of the build file

tasks {
    "bootRepackage"(RepackageTask::class) {
        setCustomConfiguration("runtimeClasspath")
    }
}