4

I have a hierarchical gradle 2.4 project like this:

Root
|- subProject1
|- subProject2
|- subProject3

For each sub project, I want to publish a .jar file and a .pom file and store them in a maven repository. All regular compile('com.example:awesome-lib') commands are properly exported in the *.pom files.

Let's assume that subProject2 has a compile project(':subProject1') dependency in it. This dependency will not be contained in the generated *.pom file.

My gradle publishing structure looks like this:

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
            groupId project.group
            artifactId jar.baseName
            version project.version
        }
    }
}

... am I doing something wrong? Why will my compile project dependencies not be exported in the *.pom files? Are there any workarounds? For clarification, what I expect as outcome in the example above is that the subProject2.pom declares a dependency to subProject1.jar.

Martin Häusler
  • 6,544
  • 8
  • 39
  • 66
  • I've found a workaround. Using the pom API described here: https://docs.gradle.org/current/userguide/publishing_maven.html ... I added the dependencies via scripting in Groovy (`pom.withXml`). Not very pretty, but it gets the job done. The downside is that I have to re-state the project dependencies which is against the DRY principle. – Martin Häusler Feb 22 '16 at 14:15

1 Answers1

1

After extensive search for an answer, and several hours wasted, I've finally found the solution. The problem seems to be the execution order in gradle. At the time when my dependencies were written into the *.pom file, the dependencies between sub-projects were not yet resolved.

The fix turned out to be very simple. All I needed to do was to replace this:

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
            groupId project.group
            artifactId jar.baseName
            version project.version
        }
    }
}

... with this (note the wrapper):

afterEvaluate { project ->

    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java
                groupId project.group
                artifactId jar.baseName
                version project.version
            }
        }
    }

}

The afterEvaluate block forces gradle to wait with the contents until the sub-project dependencies are resolved, resulting in a correct *.pom file.

For more details on the gradle build lifecycle and afterEvaluate, please see the gradle documentation.

Martin Häusler
  • 6,544
  • 8
  • 39
  • 66