【问题标题】:Exclude a specific dependency from springBoots `bootJar` gradle task从 springBoots `bootJar` gradle 任务中排除特定依赖项
【发布时间】:2018-12-05 12:21:32
【问题描述】:

我需要从 springBoots bootJar gradle 任务中排除特定的依赖项(类似于 maven 中提供的范围)。

我尝试了自定义配置,但 dependency-which-should-not-be-in-bootJar 仍包含在生成的 jar 中。

configurations{
    provided
    implementation.extendsFrom provided
}

dependencies {
    // ...
    provided "dependency-which-should-not-be-in-bootJar"
}

jar {
    from configurations.compile - configurations.provided
    from configurations.runtime
}

bootJar {
    from configurations.compile - configurations.provided
    from configurations.runtime
    launchScript()
}

【问题讨论】:

  • bootJar 默认包含运行时类路径。它继承自 implementation,您已将其配置为从 provided 扩展。因此,运行时类路径包含您提供的依赖项,因此它包含在 fat jar 中。
  • 我还尝试将我的provided 更改为providedRuntime 并让它从runtime 扩展,之后我尝试配置bootJar 以使用from configurations.runtime - configurations.providedRuntime 覆盖运行时包含,我认为应该这样做工作,但它没有。您能否指出正确的方向,如何正确排除依赖项与 jar 一起打包。

标签: spring-boot gradle dependency-management executable-jar


【解决方案1】:

您实际上可以使用 compileOnly 作为您对 gradle > 2.12 的依赖项

dependencies {
     // ...
     compileOnly "dependency-which-should-not-be-in-bootJar"
}

您仍然可以将它用于测试 + 运行时,但不会在最终构建的 jar 中。

【讨论】:

  • 在我的情况下,我还必须添加testRuntime,否则它工作得很好,谢谢。
【解决方案2】:

我还在 spring boot gitter 频道中从 Andy Wilkinson 那里得到了答案,它的工作方式略有不同,但设法实现了相似。

configurations {
    custom
    runtime.extendsFrom custom
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    custom 'com.h2database:h2'
}

bootJar {
    exclude {
        configurations.custom.resolvedConfiguration.files.contains(it.file)
    }
}

谢谢你,安迪 =)

【讨论】:

  • 它不适用于类路径文件夹:resolvedConfiguration.files 仍然包含文件夹 java.io.File 本身,而 it.file 是该文件夹的子文件夹
  • 这对我有用:configurations.custom.resolvedConfiguration.files.find { customEntry -> it.file.toPath().startsWith(customEntry.toPath()) } != null
猜你喜欢
  • 1970-01-01
  • 2021-12-25
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2017-09-08
相关资源
最近更新 更多