首页 文章

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

提问于
浏览
7

我正在尝试使用ShadowJar插件创建两个'fatJars'作为同一构建文件的一部分 . 我试图通过声明两个ShadowJar类型的任务在构建中运行shadowJar任务两次

到目前为止,我已经定义了两个这样的任务:

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

现在尝试像这样创建我的 jar :

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'
    }
}

我面临的问题是创建了jar,但它们不包含来自“其他”jar的任何其他依赖项(包,文件等) . 这些jar只包含META-INF和当前项目的包目录 .

知道可能是什么问题吗?

注意:我期待生成两个略有不同的jar文件 . 两者必须具有相同的项目代码库,并且清单的Main-Class属性存在差异(以及其他一些小差异)

非常感谢!

3 回答

  • 2

    Shadow插件作者在这里 - 我刚刚在这里知道了这个问题 . 您遇到的是Shadow插件使用一组为该任务定义的约定创建和配置 shadowJar 任务的事实 .

    当您使用该类型创建自己的任务时,您需要手动定义许多配置选项,因为插件无法知道您对这些任务的意图 .

    您可以在此处引用正在应用于内置任务的配置:https://github.com/johnrengelman/shadow/blob/master/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy#L38-L63

  • 4

    作者提供了一个非常好的解决方案(因为它既简短又有效):

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

    我实际上正在使用该解决方案的调整,出现在该页面的底部(我添加了一些说明来解释它):

    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
    }
    
  • 4

    workaround that I've used将具有单个shadowJar任务,但是传递参数 . 在你的情况下,像:

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

    然后,在启动您的应用程序时:

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

相关问题