Setting up maven publications programmatically


(Tomer Doron) #1

i am trying to set up maven publication programmatically using a plugin. the use case is an internal build plugin that enforces consisted behavior across many projects, one of which, is what gets published. i.e. the end game here is that our developers won’t need to repeat the “publications” section in all of their projects’ build.gradle script and instead our plugin will dictate that for them.

on the surface, this seemed to be working correctly, except that the POM generated is missing the dependencies. if i move the exact publications setup from the plugin to the project’s build.gradle it works correctly (i.e POM tracks

my setup (called from the plugin’s apply())

project.plugins.withType(PublishingPlugin) { PublishingPlugin publishingPlugin ->
 def publishingExtension = project.extensions.findByName('publishing')
    publishingExtension.with {
  publications {
   mavenJava(MavenPublication) {
      from project.components.java
   }
  }
    }
}

(Tomer Doron) #2

i got around this by setting it up twice, once delayed after project evaluation. not very elegant but it works

// setup main publication
project.afterEvaluate {
     project.plugins.withType(MavenPublishPlugin) { MavenPublishPlugin publishingPlugin ->
  def publishingExtension = project.extensions.findByName('publishing')
     publishingExtension.with {
   publications {
    mavenJava(MavenPublication) {
       from project.components.java
    }
   }
     }
 }
}
  // setup extra publication
project.plugins.withType(MavenPublishPlugin) { MavenPublishPlugin publishingPlugin ->
 def publishingExtension = project.extensions.findByName('publishing')
    publishingExtension.with {
  publications {
   mavenJava(MavenPublication) {
      artifact project.packageSources
    artifact project.packageDocs
   }
  }
    }
}