2

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?

sakis kaliakoudas
  • 2,039
  • 4
  • 31
  • 51
  • Please try to move the block of code that has the definition of `releaseJavadocsJar` before the `publishing` block. – Opal Sep 24 '17 at 20:07
  • the definition is before the publishing block but the publication block seems to be called first anyway – sakis kaliakoudas Sep 25 '17 at 09:27
  • For a moment I thought that you can change the plugins order but this will not help. You can try to move the `publishing` block to `project.afterEvaluate` closure. In this block the whole project is evaluated and all tasks should be added. Could you please try it out? – Opal Sep 25 '17 at 09:39
  • thanks for the suggestion. Unfortunately this doesn't work either, as the `libraryVariants.all` seems to be evaluated late itself. I added a couple of log statements to confirm this, and saw that even though the `android` block is accessed before the publication one, the `libraryVariants.all` isn't – sakis kaliakoudas Sep 26 '17 at 07:55
  • Ok, so can't help you. I'm not an android developer so I even can't try it out. But more or less this is direction I'd head towards - the order in which it's all evaluated. – Opal Sep 26 '17 at 07:59
  • Thanks for trying. Are there any similar commands to `project.afterEvaluate` that I can try? – sakis kaliakoudas Sep 26 '17 at 08:08
  • Rather not as far as I remember. Could you please try also: `artifact "releaseJavadocsJar"`? – Opal Sep 26 '17 at 09:13
  • I tried, it didn't work. But I found a way around this, check my answer below. Thanks again for your help! – sakis kaliakoudas Sep 26 '17 at 10:49
  • That looks good, cool! – Opal Sep 26 '17 at 10:53

1 Answers1

1

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)
        }
    }
}
sakis kaliakoudas
  • 2,039
  • 4
  • 31
  • 51