Applying maven-publish from a plugin

I have a custom Gradle plugin that configures the build of a large number of projects. This does things that need to be the same way such as setting up tests, and misc other plugins. Every thing works fine except for one. The maven-publish plugin. The problem is that when it generates the POM, the dependencies are missing. This only happens when my custom plugin applies the maven-plugin.

Here is my how my plugin applies maven-publish:

@Override
void apply(Project project) {
  project.apply(from: this.class.getResource('/maven-config.gradle').toURI())
}

(And in maven-config.gradle)

allprojects {
  apply plugin: 'maven-publish'
  publishing {
    publications {
      mavenJava(MavenPublication) {
        from components.java

        artifact sourcesJar
        artifact testJar
      }
    }
  }
}

maven-config.gradle has been simplified of course to show only the relevant parts. With this setup, the pom does NOT have dependencies when generated.

If I put the publishing config directly in the build.gradle of my consuming project, the pom IS generated with dependencies.

The other scenario where dependencies ARE in the pom is when I do this in maven-config.gradle:

allprojects {
  afterEvaluate {
    apply plugin: 'maven-publish'
    publishing {
      publications {
        mavenJava(MavenPublication) {
          from components.java

          artifact sourcesJar
          artifact testJar
        }
      }
    }
  }
}

This seems to work, but I am unsure about the correctness of this approach, and if there are unintended consequences of applying maven-publish in afterEvaluate(). Why does this work? Is this an appropriate thing to do for this situation? What else might this break?

Thanks

1 Like

Absolutely the same for me. One day was completely killed by investigation why it happens.

I spent an entire day trying to figure this out, and found the problem. This turned out to be a bug, which I reported here:

As of this writing, it is not yet fixed, but you can see the workaround.