Gradle publish out puts packaged *.java files instead of *.class files

Hi everyone,

I was trying to publish a gradle project to our private Sonatype Nexus repo using the maven-publish plugin. I followed the example “Example of publishing a java module with a source artifact and custom POM description” on this page:
https://docs.gradle.org/current/javadoc/org/gradle/api/publish/maven/MavenPublication.html

apply plugin: "java"
apply plugin: "maven-publish"

group = 'com.mywebsite'
version = '1.3-SNAPSHOT'

task sourceJar(type: Jar) {
  from sourceSets.main.allJava
}

publishing {
  publications {
    myPublication(MavenPublication) {
      from components.java
      artifact(sourceJar) {
        classifier "sources"
      }
      pom.withXml {
        asNode().appendNode('description', 'A demonstration of Maven POM customization')
      }
    }
  }
}

As a result, artifacts: “myproject-1.3-SNAPSHOT-sources.jar”, “myproject-1.3-SNAPSHOT.jar”, got published to nexus(can be reproduced on local .m2/repository), but they are identical(checked with md5 hash), and when unpacking, they have *.java file in there instead of expected *.class files.

Then I tried the following code, modified from this page:

apply plugin: "java"
apply plugin: "maven-publish"

group = 'com.mywebsite'
version = '1.3-SNAPSHOT'

task sourceJar(type: Jar) {
  from sourceSets.main.allJava
}
publishing {
  publications {
    myPublication(MavenPublication) {
      from project.components.java
      artifact sourceJar {
        classifier "sources"
      }
      pom.withXml {
        asNode().appendNode('description', 'A demonstration of Maven POM customization')
      }
    }
  }
}

It worked as expected, artifacts: “myproject-1.3-SNAPSHOT-sources.jar”, “myproject-1.3-SNAPSHOT.jar”, got published, and when unpacking them, the first one has *.java file in there and the second one has *.class files in there.

The only difference I noticed between these two block of code is:

artifact(sourceJar)
vs
artifact sourceJar

I don’t have enough knowledge about gradle to tell why replacing the parenthesis with empty space made such difference, but since the example in the doc doesn’t work, I think it probably need some revision? I’d be very glad to know why parenthesis made this difference. But I could be wrong, please let me know if I missed something here.

Thanks!

PS: this is the output of gradle --version:

------------------------------------------------------------
Gradle 2.6
------------------------------------------------------------

Build time:   2015-08-10 13:15:06 UTC
Build number: none
Revision:     233bbf8e47c82f72cb898b3e0a96b85d0aad166e

Groovy:       2.3.10
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM:          1.8.0_51 (Oracle Corporation 25.51-b03)
OS:           Mac OS X 10.10.4 x86_64

This is actually a pretty interesting one :smile:

The main problem is with your ‘sourceJar’ task definition. Mainly, the task is going to create a JAR file in the ‘build/libs’ directory with the exact same name as the regular ‘jar’ task (due to default naming convention being {projectName}-{version}.jar). So since the ‘sourcesJar’ ask runs after the ‘jar’ task it overwrites the jar and the same file is used for both publication artifacts.

In your second example what is actually happening is you are configuring the ‘sourceJar’ task and setting it’s classifier to “sources” (which is correct) and then adding the task itself as an artifact. This is really to do with the particularities of Groovy syntax when you removed the parenthesis.

The best, and most explicit solution is to set the classifier on the task so that the JAR file it produces is named correctly.

task sourceJar(type: Jar) {
  from sourceSets.main.allJava
  classifier 'sources'
}

Then you can omit that in the publication definition.

artifact sourceJar
1 Like

Also, I’ve gone ahead and fixed the example in the Javadoc to work as expected. Thanks for bringing this to our attention.

@mark_vieira Thanks for the fast, detailed response and suggesting the best way to do this. :smile: