Publishing Jars with Classifiers after v5.0

It appears that between 5.0 and 5.1, using a classifier when publishing JARs using the Maven Publishing plugin has been depreciated. I mention this because many of the fields are now marked deprecated in the v5.1 docs, but they were not in the v5.0 docs. I didn’t however see any mention of this deprecation in the v5.1 release notes, but perhaps I missed it.

What is the new way to specify a classifier when publishing JARs to Maven repos?

Note that the current version of the Maven Publishing Docs still list classifier in the complete example.

Hi,

I came across the same problem. The documentation (5.1.1) contains the following statement:
" And note that you need to use classifier rather than appendix here for correct publication of the JAR."
https://docs.gradle.org/current/userguide/building_java_projects.html

The java code (AbstractArchiveTask) contains the following deprecation doc:
Use getArchiveClassifier()
But you cannot replace a setter with a getter.

1 Like

You are perfectly right, these changes happened in 5.1 but were not properly reflected in documentation.

In short, instead of classifier which is a simple field, you should use archiveClassifier which is a Property and thus enables Gradle to do more. For example if you were to set this value by using a task output, Gradle could infer the task dependency automatically. See javadoc for more features.

So all your code that is:
classifier = 'foo'
needs to be changed to:
archiveClassifier = 'foo' // DSL equivalent to archiveClassifier.set('foo')

I filed https://github.com/gradle/gradle/issues/8217 to follow up on this.

Thankys for the fast reply.

How can I set the archiveClassifier with the KotlinDSL?
Example from the documentation:

tasks.register<Jar>("sourcesJar") {
    classifier = "sources"
    from(sourceSets.main.get().allJava)
}

I get a compile error “Val cannot be reassigned” when I use the follwoing code.

archiveClassifier ="sources"
1 Like

For the Kotlin DSL, you have to do: archiveClassifier.set("sources")
There is no DSL sugar that allows to use the = for assigning a value to the property.

That limitation is due to a language level Kotlin limitation from what I can recall.

1 Like

Thank you! It works fine.

tasks.register<Jar>("sourcesJar") {
    archiveClassifier.set("sources")
    from(sourceSets.main.get().allJava)
    manifest = project.the<JavaPluginConvention>().manifest {
        from(sharedManifest)
    }
}

tasks.register<Jar>("javadocJar") {
    archiveClassifier.set("javadoc")
    dependsOn("javadoc")
    from(tasks.javadoc.get().destinationDir)
    manifest = project.the<JavaPluginConvention>().manifest {
        from(sharedManifest)
    }
}

That worked for me. Many thanks!

FWIW: https://youtrack.jetbrains.com/issue/KT-4075

If that were to happen, one could also add an overload with an enum type for standard classifiers such as sources, javadoc, etc (so they can be discovered through the IDE while writing the task).