Gradle maven publish plugin config has reference t

2019-06-08 00:37发布

I have a publication config block that looks like this in my build.gradle file

publishing {
publications {
    maven(MavenPublication) {
        groupId "groupId"
        artifactId 'artifactId'
        version "4.0"
        artifact "$buildDir/outputs/aar/artifact-release.aar"
        artifact releaseJavadocsJar

        // Generate pom file so that the artifact dependencies can be fetched automatically.
        pom.withXml {
            def dependenciesNode = asNode().appendNode('dependencies')
            configurations.compile.allDependencies.each { dependency ->
                def dependencyNode = dependenciesNode.appendNode('dependency')
                dependencyNode.appendNode('groupId', dependency.group)
                dependencyNode.appendNode('artifactId', dependency.name)
                dependencyNode.appendNode('version', dependency.version)
            }
        }
    }
}

}

note the releaseJavadocsJar, which is a gradle task defined like this:

  android.libraryVariants.all { variant ->
    if (variant.name == 'release') {
        def taskJavadoc = task("releaseJavadocs", type: Javadoc) {
            source = variant.javaCompile.source
            classpath = files(((Object) android.bootClasspath.join(File.pathSeparator)))
            classpath += files(variant.javaCompile.classpath.files)
        }
        task("releaseJavadocsJar", type: Jar) {
            classifier = 'javadoc'
            from taskJavadoc.destinationDir
        }
    }
}

When I sync gradle this line

   artifact releaseJavadocsJar

from the publication complains that

Error:(62, 0) Could not get unknown property 'releaseJavadocsJar' for object of type org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication.

which means that my dynamically created task is not there yet when the gradle syncing occurs.

Is there any way around this?

1条回答
叛逆
2楼-- · 2019-06-08 01:35

Wrapping it in a project.afterEvalute block didn't work, but this did:

project.tasks.whenTaskAdded { addedTask ->
if (addedTask.name == 'releaseJavadocsJar') {
    // Publishing to local maven repo via 'gradlew publishToMavenLocal'.
    publishing.publications {
         maven(MavenPublication) {
    groupId "groupId"
    artifactId 'artifactId'
    version "4.0"
    artifact "$buildDir/outputs/aar/artifact-release.aar"
    artifact releaseJavadocsJar

    // Generate pom file so that the artifact dependencies can be fetched automatically.
    pom.withXml {
        def dependenciesNode = asNode().appendNode('dependencies')
        configurations.compile.allDependencies.each { dependency ->
            def dependencyNode = dependenciesNode.appendNode('dependency')
            dependencyNode.appendNode('groupId', dependency.group)
            dependencyNode.appendNode('artifactId', dependency.name)
            dependencyNode.appendNode('version', dependency.version)
        }
    }
}
查看更多
登录 后发表回答