【问题标题】:How to build executable jar of a submodule with gradle + spring boot 1.5如何使用 gradle + spring boot 1.5 构建子模块的可执行 jar
【发布时间】:2019-01-09 17:16:08
【问题描述】:

我有一个多模块项目:

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

【问题讨论】:

  • 正如你所说,in the :common module I include all the dependencies, in the :api module, you don't have that。所以没有 libs 文件夹,因为那个。还有你想用它来服务什么目的,因为你可能在:api模块中不必要地添加了这些依赖项
  • @rdj7 我已经更新了问题,实际上我看到的问题是,当我运行 jar 时,它显示公共模块中的一个类的 NoClassFound。我将所有依赖项放在 common 模块中的原因是我实际上有两个模块都依赖于 common 模块而不仅仅是 api 模块。

标签: java spring-boot gradle


【解决方案1】:

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

【讨论】:

  • 上次我检查过,spring boot 1.X 与 gradle 4.X 不兼容 - 他们直到 Spring Boot 2.x 才对 spring boot gradle 插件进行版本升级,因为您仅限于 Gradle 3.x,这可以解释为什么实现不起作用而编译起作用。这篇文章更深入地解释了这些差异:stackoverflow.com/questions/44493378/…
  • 一年后,我终于可以解释发生了什么 - 请参阅下面的答案
【解决方案2】:

原因是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")
    }
}

【讨论】:

    猜你喜欢
    • 2018-04-06
    • 1970-01-01
    • 2020-01-06
    • 2017-08-10
    • 1970-01-01
    • 2014-04-18
    • 2014-11-27
    • 2017-02-19
    • 1970-01-01
    相关资源
    最近更新 更多