簡體   English   中英

在同一個構建文件中執行兩次gradle shadowjar任務

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

我正在嘗試使用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)

現在嘗試像這樣創建我的罐子:

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屬性存在差異(以及其他一些小差異)

非常感謝!

作者給出了一個非常好的解決方案(因為它既簡短又有效):

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
}

Shadow插件作者在這里 - 我剛剛在這里知道了這個問題。 您遇到的是Shadow插件使用該任務的一組已定義約定創建和配置shadowJar任務。

當您使用該類型創建自己的任務時,您需要手動定義許多配置選項,因為插件無法知道您對這些任務的意圖。

您可以在此處引用正在應用於內置任務的配置: https//github.com/johnrengelman/shadow/blob/master/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ ShadowJavaPlugin.groovy#L38-L63

我使用的解決方法是使用單個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=...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM