如何使 gradle ShadowJar 任务也创建其子项的源代码和 javadoc?

How to make the gradle ShadowJar task also create sources and javadoc of its children?

我有一个包含 8 个子项目的 gradle 项目和一个配置的 shadowjar 任务来创建一个 "all" jar。顶层项目设置为对其所有子项目具有依赖性,这告诉 shadowjar 要包含的内容:

project(':') {
    dependencies {
        compile project(':jfxtras-agenda')
        compile project(':jfxtras-common')
        compile project(':jfxtras-controls')
        compile project(':jfxtras-icalendarfx')
        compile project(':jfxtras-icalendaragenda')
        compile project(':jfxtras-menu')
        compile project(':jfxtras-gauge-linear')
        compile project(':jfxtras-font-roboto')
    }
}

shadowJar {
   classifier = null // do not append "-all", so the generated shadow jar replaces the existing jfxtras-all.jar (instead of generating jfxtras-all-all.jar)
}

这工作正常,但 maven central 拒绝所有 jar,因为它没有关联的源和 javadocs jar。

如何让 gradle 也生成源代码和 javadoc? ShadowJar 的文档说它应该默认执行此操作。

shadow 插件似乎没有构建 fat sources/javadocs 罐子的功能。

下面,我提供了一些简短的任务(javadocJarsourcesJar),它们将构建 fat javadoc 和源 jar。它们被链接为始终在 shadowJar 之后执行。但是它不依赖shadow jar插件。

subprojects {
    apply plugin: 'java'
}

// Must be BELOW subprojects{}
task alljavadoc(type: Javadoc) {
    source subprojects.collect { it.sourceSets.main.allJava }
    classpath = files(subprojects.collect { it.sourceSets.main.compileClasspath })
    destinationDir = file("${buildDir}/docs/javadoc")
}

task javadocJar(type: Jar, dependsOn: alljavadoc) {
    classifier = 'javadoc'
    from alljavadoc.destinationDir
}

task sourcesJar(type: Jar) {
    classifier = 'sources'
    from subprojects.collect { it.sourceSets.main.allSource }
}

shadowJar.finalizedBy javadocJar
shadowJar.finalizedBy sourcesJar

请注意,subprojects 部分是必需的,即使您已经在子项目中应用了 java 插件。

另请注意,它不包括您的子项目可能依赖的第三方库的 javadoc。但通常你可能不想这样做。

这是一个旧线程,但我发布了我的解决方案,因为它比上面的要容易得多,而且我已经确认它有效:

plugins {
    id 'com.github.johnrengelman.shadow' version '7.1.0'
    id 'signing'
    id 'maven-publish'
}

// If using Spring Boot, this is needed
jar.enabled = true
jar.dependsOn shadowJar

java {
    withJavadocJar()
    withSourcesJar()
}

// Remove the -all extension from the "fat" Jar, or it can't be used
// when published to Maven Central.
shadowJar {
    archiveClassifier.set('')
}

// The contents of this section are described here:
//   https://docs.gradle.org/current/userguide/publishing_maven.html
publishing {
    publications {
        jwtopaLibrary(MavenPublication) {
            artifactId = 'jwt-opa'
            artifacts = [ shadowJar, javadocJar, sourcesJar ]
            pom {
    // etc. ...
}

// Signs the `publication` generated above with the name `jwtopaLibrary`
// Signing plugin, see: https://docs.gradle.org/current/userguide/signing_plugin.html#signing_plugin
signing {
    sign publishing.publications.jwtopaLibrary
}

这里没有说清楚,需要多处搜集信息,但是要让signing插件正常工作,你需要short form十六进制密钥ID:

# gradle.properties

# The `signing` plugin documentation is less than helpful; 
# however, this is the magic incantation to find the `keyId`:
#
#   gpg --list-signatures --keyid-format 0xshort
#
# The key also needs to be distributed to public GPG servers:
#
#   gpg --keyserver keyserver.ubuntu.com --send-keys 123...fed
#
# In all cases, we need to use the values from the `pub` key.
signing.keyId=0x1234abcde

然后,这只是 运行 ./gradlew publish 的问题,魔法就会发生(好吧,不是真的,你仍然必须去 Sonatype 存储库,做“关闭和释放舞蹈”,但是你知道的,随便吧)。