Publish test class jars independently from main jar

RE: gradle 5.6.4

I have a Java library that has a utility API that includes utilities for unit testing. I want to publish two separate artifacts to maven, one for the utility API and one for the unit test utility API. I’ve defined the following task for jar’ing the test classes:

task testJar(type: Jar, dependsOn: testClasses) {
  archiveClassifier 'tests'
  from sourceSets.test.output
}

I then have the following maven publishing declaration:

publishing {
  ...

  publications {
    mavenJava(MavenPublication) {
      // publish main jar
      ...
    }

    mavenJava(MavenPublication) {
      groupId project.group
      artifactId "${project.name}-tests"
      version project.version

      artifact testJar
    }

This works in that two distinct artifacts are published to maven, but, the second artifact with artifactId '${project.name}-tests' includes BOTH the -tests.jar with the test classes AND the regular main classes jar. I’m not sure that this is actually going to create a problem for projects that are dependent on this artifact, but I’d prefer to exclude the main classes jar if possible. Does anyone know if I can do this? I’m not sure how/why the jar is automagically being included in the first place if I’m not calling from components.java. I’ve tried experimenting with a few different variants in this mavenJava publication block, but the results are always the same. Any guidance would be appreciated.

The code snippet is not correctly creating two separate publications. Rename one of the publications from mavenJava to a different name. Once you do that you will no longer need to set archiveClassifier and the test jar can be published as the main artifact of the <project name>-test module.

IMO Gradle should fail when you try to create two publications with the same name, but I guess there could be a bug somewhere.

Chris, thank you very much for your reply. I didn’t realize that the mavenJava declaration could take different names. I’m not terribly familiar with the gradle DSL or its “parts of speech”. That said, by creating two publications with different names, I was indeed able to solve my problem.