Jar task does not see dependency when using maven-publish

We have a build using maven-publish and want to include pom.xml into published JAR (discussed for example in https://issues.gradle.org/browse/GRADLE-2916). The build works: it creates JARs and publishes them. But when we run gradle tasks it fails. Simple version of a build to reproduce looks like this:

settings.gradle:

include 'a'
include 'b'

build.gradle:

subprojects {
    apply plugin: 'java'
    apply plugin: 'maven-publish'

    jar {
        dependsOn 'generatePomFileForMavenJavaPublication'
        into("META-INF/maven/$project.group/$project.archivesBaseName") {
            from new File(project.buildDir, 'publications/mavenJava')
            rename ".*", "pom.xml"

        }
    }

    publishing {
        publications {
             mavenJava(MavenPublication) {
                from components.java
            }
        }
        repositories {
            maven {
                name 'local'
                url new File(rootProject.buildDir, 'repo').toURI().toURL()
            }
        }
    }
}

b/build.gradle:

dependencies {
    compile project(':a')
}

The error looks like:

Execution failed for task ':tasks'.
> Could not determine the dependencies of task ':aggrDaoProtos:jar'.
...
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':tasks'.
Caused by: org.gradle.api.GradleException: Could not determine the dependencies of task ':aggrDaoProtos:jar'.
Caused by: org.gradle.api.UnknownTaskException: Task with path 'generatePomFileForMavenJavaPublication' not found in project ':aggrDaoProtos'.

Same with Gradle 2.5 and 2.6-rc-2. Any idea what is wrong?

Thanks Radim.

The publishing tasks are all created via the new configuration model, which means they suffer from issues around bridging between the new and old models. Another case is the use of Tasks.withType().

This looks like another use case where task bridging doesn’t seem to be working fully. I’ve verified that the supplied test fails, but I don’t yet understand why.

I found 2 ways to make ‘gradle tasks’ work :

  1. add tasks.realize() to the end of the build script for ‘:b’
  2. Remove the project dependency from the build script for ‘:b’

@Adrian_Kelly Please take a look at this as part of your design work for task bridging.

Hi Daz, thanks for looking into this. I suspected that the new configuration model will be involved. 1) looks interesting. Certainly it seems more elegant than

if (project.tasks.findByName('generatePomFileForMavenJavaPublication')) {
    dependsOn 'generatePomFileForMavenJavaPublication'
} else {
    project.tasks.whenTaskAdded { addedTask ->
        if (addedTask.name == 'generatePomFileForMavenJavaPublication') {
            project.tasks.jar.dependsOn 'generatePomFileForMavenJavaPublication'
        }
    }
}

Using 2) is not an option when the example is simplified version of our setup with these dependencies.

Yep, it’s related to task bridging. The design spec is here and we hope to start work on this soon.