Gradle 7 如何创建一个 fat jar。

8
我需要从我的依赖项中创建一个 fat jar,该 jar 不包含作用域为 compileOnly 的依赖项。
dependencies {
   api 'org.slf4j:slf4j-api:1.7.26' // this must be in the jar
   compileOnly 'it.unimi.dsi:fastutil:8.2.1' // this must not be in the jar

    jar {
        from {
            configurations.compileClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
        }
    }
}

当我构建项目时,两个依赖项都包含在最终的jar文件中。我该如何从fat jar中排除fastutil?
我尝试使用runtimeOnly。
runtimeOnly 'it.unimi.dsi:fastutil:8.2.1' // this must not be in the jar

但这会导致在编译时无法解决fastutil。

我正在运行Java 16和Gradle 7.0.2。

1个回答

9

使用configurations.runtimeClasspath代替configurations.compileClasspathcompileClasspath包含编译所需的所有库,因此其中包含fastutil。 另一方面,runtimeClasspath不包括配置compileOnly中的库。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接