使用build参数控制gradle运行时依赖项 [英] Controlling gradle runtime dependencies with build parameter
问题描述
我使用Gradle及其ShadowJar插件为我的应用程序构建了一个胖子罐,该胖子罐部署在两个上下文之一中.
I use Gradle and its ShadowJar plugin to build a fat jar for my application, which is deployed in one of two contexts.
在一种情况下,环境提供了A,B和C依赖关系(及其所有传递依赖关系),并且这些类不应成为我的胖子的一部分.
In one context, the environment provides dependencies A,B and C (and all their transitive dependencies), and these classes should not be part of my fat jar.
在另一种情况下,环境仅提供依赖项A和B,我必须确保C及其所有传递依赖项都捆绑在胖子中.
In the other context, the environment only provides dependencies A and B and I must ensure that C and all its transitive dependencies are bundled in my fat jar.
如何在我的Gradle构建文件中定义此行为?我认为最好的方法是根据构建目标或命令行参数以某种方式调整 runtime.exclude
属性.
How can I define this behaviour in my Gradle build file? I think the best way would be to adjust the runtime.exclude
property somehow based on a build target or command-line parameter.
推荐答案
我使用了一个看起来像这样的构建文件:
I went with a build file that looks like this:
subprojects {
apply plugin: "java"
apply plugin: "scala"
version = "1.0-SNAPSHOT"
group = "com.example.projects"
ext.deployEnv = "Env1"
if (project.hasProperty("deployEnv")) {
ext.deployEnv= project.property("deployEnv")
}
}
project(":myproject") {
configurations {
runtime.exclude group: 'A'
runtime.exclude group: 'B'
}
if (ext.deployEnv == 'Env0') {
configurations {
runtime.exclude group:'C'
}
}
}
它可以满足我的要求,而对构建文件的更改却很少.
It does what I want, with minimal changes to the build file.
这篇关于使用build参数控制gradle运行时依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!