Adding custom artifacts to an existing publication

I have a very simple plugin project whose Gradle script looks like this

apply plugin: 'groovy'
apply plugin: 'maven-publish'
apply plugin: 'java-gradle-plugin'

group='com.company.project'
version='1.0.0-SNAPSHOT'

defaultTasks 'clean', 'build'

dependencies {
  compile localGroovy()
}

task packageSources(type: Jar) {
  classifier = 'sources'
  from sourceSets.main.allSource
}

publishing {
  repositories {
    maven {
      url "${buildDir}/local-repository"
    }
  }
}

When I run “gradlew clean publish”, the repository contains the following files:

build/local-repository/
└── com
    └── company
        └── project
            └── wd-assertions
                ├── 1.0.0-SNAPSHOT
                │   ├── maven-metadata.xml
                │   ├── maven-metadata.xml.md5
                │   ├── maven-metadata.xml.sha1
                │   ├── wd-assertions-1.0.0-20160615.145156-1.jar
                │   ├── wd-assertions-1.0.0-20160615.145156-1.jar.md5
                │   ├── wd-assertions-1.0.0-20160615.145156-1.jar.sha1
                │   ├── wd-assertions-1.0.0-20160615.145156-1.pom
                │   ├── wd-assertions-1.0.0-20160615.145156-1.pom.md5
                │   └── wd-assertions-1.0.0-20160615.145156-1.pom.sha1
                ├── maven-metadata.xml
                ├── maven-metadata.xml.md5
                └── maven-metadata.xml.sha1

The build published a POM and a jar file.

Now I want to publish the “sources” jar file. If I add the following lines (see below), I end up with 2 publications because the ‘java-gradle-plugin’ creates a ‘pluginMaven’ publication when applied.

publishing {
  publications {
    mavenJava(MavenPublication) {
      artifact packageSources
    }
  }

  repositories {
    maven {
      url "${buildDir}/local-repository"
    }
  }
}

Running “gradlew clean publish” yields

build/local-repository/
└── com
    └── company
        └── project
            └── wd-assertions
                ├── 1.0.0-SNAPSHOT
                │   ├── maven-metadata.xml
                │   ├── maven-metadata.xml.md5
                │   ├── maven-metadata.xml.sha1
                │   ├── wd-assertions-1.0.0-20160615.145511-1-sources.jar
                │   ├── wd-assertions-1.0.0-20160615.145511-1-sources.jar.md5
                │   ├── wd-assertions-1.0.0-20160615.145511-1-sources.jar.sha1
                │   ├── wd-assertions-1.0.0-20160615.145511-1.pom
                │   ├── wd-assertions-1.0.0-20160615.145511-1.pom.md5
                │   ├── wd-assertions-1.0.0-20160615.145511-1.pom.sha1
                │   ├── wd-assertions-1.0.0-20160615.145512-2.jar
                │   ├── wd-assertions-1.0.0-20160615.145512-2.jar.md5
                │   ├── wd-assertions-1.0.0-20160615.145512-2.jar.sha1
                │   ├── wd-assertions-1.0.0-20160615.145512-2.pom
                │   ├── wd-assertions-1.0.0-20160615.145512-2.pom.md5
                │   └── wd-assertions-1.0.0-20160615.145512-2.pom.sha1
                ├── maven-metadata.xml
                ├── maven-metadata.xml.md5
                └── maven-metadata.xml.sha1

We can see that one publication (mavenJava) deployed 1 POM file and the sources jar in version 1.0.0-20160615.145511-1 whereas another publication (pluginMaven) published 1 POM file and the main jar in version 1.0.0-20160615.145512-2. However those 2 publications are disconnected because they don’t refer to the same version.

If I try to update the ‘pluginMaven’ publication like this

publishing {
  publications {
    pluginMaven(MavenPublication) {
      artifact packageSources
    }
  }

  repositories {
    maven {
      url "${buildDir}/local-repository"
    }
  }
}

Gradle fails with the following error: “Cannot add a Publication with name ‘pluginMaven’ as a Publication with that name already exists.”

So my question is: how can I add a custom artifact (the sources jar) to an existing configuration (pluginMaven created by the ‘java-gradle-plugin’) ?

I tried to retrieve the existing configuration instead of creating it but was unable to find the correct syntax to do so

As you’ve encountered, the xyz(MavenPublication) syntax is used to create a new publication, but the pluginMaven publication already exists. You can use the model block to create a rule against the PublishingExtension, try something like:

model {
    publishing {
        publications {
            pluginMaven {
                artifact tasks.packageSources
            }
        }
    }
}

Thanks @Chris_Dore, this is my first encounter with the new configuration model. I guess I need to read the documentation again :slight_smile: