简体   繁体   English

在同一个构建文件中执行两次gradle shadowjar任务

[英]execute gradle shadowjar task twice in same build file

I am trying to create two 'fatJars' using ShadowJar plugin as part of the same build file. 我正在尝试使用ShadowJar插件创建两个'fatJars'作为同一构建文件的一部分。 I am trying to run the shadowJar task twice inside the build by declaring two tasks of ShadowJar type 我试图通过声明两个ShadowJar类型的任务在构建内运行两次shadowJar任务

So far, I have defined two tasks like so: 到目前为止,我已经定义了两个这样的任务:

task shadowjar_one (type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar)
task shadowjar_two (type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar)

and now trying to create my jars like so: 现在尝试像这样创建我的罐子:

shadowjar_one {
    mergeServiceFiles()
    exclude 'somefile.txt'

    archiveName = 'jar1.jar'

    appendManifest {
         attributes 'Main-Class': 'some.package.someClass'
    }
}

shadowjar_two {
    mergeServiceFiles()
    exclude 'someOtherfile.txt'

    archiveName = 'jar2.jar'

    appendManifest {
         attributes 'Main-Class': 'some.package.someOtherClass'
    }
}

The problem I am facing is that the jars are created, but they do not contain any of the other dependencies (packages, files etc) from 'other' jars. 我面临的问题是创建了jar,但它们不包含来自“其他”jar的任何其他依赖项(包,文件等)。 The jars only contain the META-INF and current project's package directories. jar只包含META-INF和当前项目的包目录。

Any idea what could be the issue? 知道可能是什么问题吗?

Note: I am expecting two slightly different jar files to be produced. 注意:我期待生成两个略有不同的jar文件。 Both must have the same project codebase with differences in the Main-Class attribute of manifest (and a couple of other small differences) 两者必须具有相同的项目代码库,并且清单的Main-Class属性存在差异(以及其他一些小差异)

Many Thanks! 非常感谢!

The author gave a very good solution (in that it is both short and working) here: 作者给出了一个非常好的解决方案(因为它既简短又有效):

https://github.com/johnrengelman/shadow/issues/108 https://github.com/johnrengelman/shadow/issues/108

I'm actually using a tweak to that solution, appearing at the bottom of that page (I've added remarks to explain it a little): 我实际上正在使用该解决方案的调整,出现在该页面的底部(我添加了一些说明来解释它):

task bootstrapNodeJar(type: ShadowJar) {
 group = "shadow" // Not a must have, but it's always good to have a group, you can chose whichever - this is the one shadowJar belongs to
 description = "Builds a Bitsquare bootstrap node executable jar" // Same as the above
 manifest.attributes 'Main-Class': 'io.bitsquare.app.cli.BootstrapNodeMain' // The main attraction! Be sure to update this line
 classifier = 'bootstrapNode' // General jar task property - see more about it in the Gradle manual
 from(project.convention.getPlugin(JavaPluginConvention).sourceSets.main.output) // Leave as is
 configurations = [project.configurations.runtime] // Same as the above
 exclude('META-INF/INDEX.LIST', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA') // This one is actually really important!

 // Here you can add other Jar properties like destinationDir, for example
}

Shadow plugin author here - I was just made aware of this question here. Shadow插件作者在这里 - 我刚刚在这里知道了这个问题。 What you are encountering is the fact that the Shadow plugin creates and configures a shadowJar task using a set of defined conventions for that task. 您遇到的是Shadow插件使用该任务的一组已定义约定创建和配置shadowJar任务。

When you are creating your own tasks using that type, you'll need to manually define a number of those configuration options since there is no way for the plugin to know what your intent is with those tasks. 当您使用该类型创建自己的任务时,您需要手动定义许多配置选项,因为插件无法知道您对这些任务的意图。

You can reference the configuration that is being applied to the built in task here: https://github.com/johnrengelman/shadow/blob/master/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy#L38-L63 您可以在此处引用正在应用于内置任务的配置: https//github.com/johnrengelman/shadow/blob/master/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ ShadowJavaPlugin.groovy#L38-L63

A workaround that I've used is to have a single shadowJar task, but pass parameters. 我使用的解决方法是使用单个shadowJar任务,但传递参数。 In your case, something like: 在你的情况下,像:

shadowJar {
  mergeServiceFiles()
  exclude System.properties.getProperty('exclude')
  archiveName = System.properties.getProperty('archiveName')
  appendManifest {
    attributes 'Main-Class': System.properties.getProperty('mainClass')
  }
}

Then, when starting your application: 然后,在启动您的应用程序时:

gradlew shadowJar -Dexclude=... -DarchiveName=... -DmainClass=...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Gradle Build - 任务 ':sonarqube' 的执行失败不能被索引两次 - Gradle Build - Execution failed for task ':sonarqube' can't be indexed twice 如何使用适用于 java 的 gradle shadowJar 创建 jar 文件 11 - How to create a jar file with gradle shadowJar that works on java 11 如何在 gradle 中与构建任务分开执行注释处理 - How execute annotation processing in gradle separatly from build task 在使用gradle构建的shadowJar中使用truezip - using truezip in shadowJar built with gradle 如何使 gradle ShadowJar 任务也创建其子项的源代码和 javadoc? - How to make the gradle ShadowJar task also create sources and javadoc of its children? gradle shadowJar 任务未找到 class org.apache.tools.ZADCDBD79A8D84102F229B192AADC - gradle shadowJar task is not finding class org.apache.tools.zip.ZipOutputStream Gradle WAR构建后,WAR文件包含两次静态资源 - WAR File contains static resources twice after Gradle WAR build 执行任务时如何将变量写入gradle属性文件? - how to write variables to gradle property file when you execute task? 如何在Gradle文件中的任务中执行多个Shell脚本 - How to execute more than one shell scripts in a task in gradle file Gradle 中的 uberJar、fatJar 和 shadowJar 有什么区别? - What are the differences between uberJar, fatJar and shadowJar in Gradle?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM