我使用Gradle及其ShadowJar插件为我的应用程序构建一个胖jar,它被部署在两个上下文之一。
在一个上下文中,环境提供了依赖关系A,B和C(及其所有传递依赖关系),并且这些类不应该是我的胖罐子的一部分。
在另一个上下文中,环境只提供依赖关系A和B,我必须确保C及其所有传递依赖关系都捆绑在我的胖罐中。
如何在Gradle构建文件中定义此行为?我认为最好的方法是根据构建目标或命令行参数以某种方式调整<textarea style="width:350px;
height:80px;" cols="42" rows="5" name="sitelink"
><?php if($siteLink_val) echo $siteLink_val; ?></textarea>
属性。
答案 0 :(得分:0)
所以我认为你正在寻找两种配置。
apply plugin: 'groovy'
repositories {
jcenter()
}
dependencies {
compile localGroovy()
compile 'org.slf4j:slf4j-api:1.7.21'
compileOnly group: 'com.google.guava', name: 'guava', version: '20.0'
testCompile 'junit:junit:4.12'
}
task fatJar(type: Jar) {
from sourceSets.main.output, configurations.compile
baseName = "$project.name-fat"
}
jar.dependsOn fatJar
现在我们可以为我们感兴趣部署到
的每个上下文创建一个jar$ ./gradlew clean build
Configuration on demand is an incubating feature.
:clean
:compileJava UP-TO-DATE
:compileGroovy
:processResources UP-TO-DATE
:classes
:fatJar
:jar
:assemble
:compileTestJava UP-TO-DATE
:compileTestGroovy UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
BUILD SUCCESSFUL
Total time: 4.997 secs
现在让我们验证我们有一个胖罐
$ unzip build/libs/q40727142-fat.jar -d build/libs/included
Archive: build/libs/q40727142-fat.jar
creating: build/libs/included/META-INF/
inflating: build/libs/included/META-INF/MANIFEST.MF
creating: build/libs/included/com/
creating: build/libs/included/com/jbirdvegas/
creating: build/libs/included/com/jbirdvegas/q40727142/
inflating: build/libs/included/com/jbirdvegas/q40727142/Hello.class
inflating: build/libs/included/groovy-all-2.4.7.jar
inflating: build/libs/included/slf4j-api-1.7.21.jar
并且让我们验证原始配置没有我们的罐子,因为它们应该被提供。
$ unzip build/libs/q40727142.jar -d build/libs/notIncluded
Archive: build/libs/q40727142.jar
creating: build/libs/notIncluded/META-INF/
inflating: build/libs/notIncluded/META-INF/MANIFEST.MF
creating: build/libs/notIncluded/com/
creating: build/libs/notIncluded/com/jbirdvegas/
creating: build/libs/notIncluded/com/jbirdvegas/q40727142/
inflating: build/libs/notIncluded/com/jbirdvegas/q40727142/Hello.class
答案 1 :(得分:0)
我使用了一个看起来像这样的构建文件:
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'
}
}
}
它可以实现我想要的功能,只需对构建文件进行最少的更改。