javagradlesonatypemaven-publishandroid-gradle-7.0

How to publish to Maven Repo using Gradle 7.0


I am looking for working documentation on how to publish a library to the public Maven repository using Gradle 7.0.

I have signed up for an account at Maven (Sonatype Actually), have jumped through the verification hoops, and so that seem to be ready to go. I have tried manual publishing, but ready to automate.

The Maven Documentation is for Gradle 6 and lower. The specific module necessary has been removed from Gradle, and there is a clear message to this effect. "maven" has been removed, you have to use "maven-publish". It seems that everything has changed, and the documentation on Maven is useless for Gradle 7

So then there is the documentation on Gradle which is just wrong as far as I can see. It says to include the following to apply the plugin:

plugins {
    id 'maven-publish'
}

Good enough, but the documentation then says there are tasks, but those tasks are not there. The documentation claims there is:

However, here are the tasks that I have available from running tasks --all:

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
javadocJar - Assembles a jar archive containing the main javadoc.
sourcesJar - Assembles a jar archive containing the main sources.
testClasses - Assembles test classes.

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in project ':purpleLib'.
dependencies - Displays all dependencies declared in project ':purpleLib'.
dependencyInsight - Displays the insight into a specific dependency in project ':purpleLib'.
help - Displays a help message.
javaToolchains - Displays the detected java toolchains.
outgoingVariants - Displays the outgoing variants of project ':purpleLib'.
projects - Displays the sub-projects of project ':purpleLib'.
properties - Displays the properties of project ':purpleLib'.
tasks - Displays the tasks runnable from project ':purpleLib'.

Publishing tasks
----------------
publish - Publishes all publications produced by this project.
publishToMavenLocal - Publishes all Maven publications produced by this project to the local Maven cache.

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

Other tasks
-----------
compileJava - Compiles main Java source.
compileTestJava - Compiles test Java source.
components - Displays the components produced by project ':purpleLib'. [deprecated]
dependentComponents - Displays the dependent components of components in project ':purpleLib'. [deprecated]
model - Displays the configuration model of project ':purpleLib'. [deprecated]
processResources - Processes main resources.
processTestResources - Processes test resources.

I know there is more configuration needed. I have hacked some things, and gotten publishToMavenLocal to create files in the .m2 folder, but under no circumstances can I actually publish, so I am starting over, going by the book, however the book seems to be completely broken.

Does anyone have a working example of Gradle 7 build file that WORKS when publishing to Maven?

For completeness, my current gradle.build file is:

/*
 build for the purple utilities library
 */
 
plugins {
    // the "id" commands below find an existing pluging code, 
    // create an instance of it, and put into the project.
    id 'java-library';
    id 'maven-publish';
}
java {
    withJavadocJar()
    withSourcesJar()
}
project.repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral();
}; 

project.version '3.0';
project.description = "Purple utilities for web programming";
project.group = 'com.purplehillsbooks.purple';
project.archivesBaseName = 'purple';

project.dependencies({

    // Use JUnit test framework.
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
    
    compileOnly 'org.apache.tomcat:tomcat-catalina:8.5.46'
})


project.test {
    useJUnitPlatform()
}

Maybe I have to downgrade to Gradle 6? I have been trying to get things to work with 7, however it seems that the documentation is piss poor from the Gradle people. Searching is so difficult because 9 of 10 hits are about retrieving from Maven, not publishing, and all the rest are random fragments which most of the time only applies to Gradle 6. But without in depth experience on the prior workings of Gradle it is often impossible to tell. A working example, if you have one, would be very helpful.

Unfortunately this is my first attempt to automate the publication to Maven, so I have no old working example to work from, and no experience with how Gradle does this in the past. I have read all the documentation from Gradle, but so much of it simply does not describe what I can get to run that it is useless, and I find myself doubting that it is correct.


Solution

  • The new documentation has not been deployed yet, as you can see on sonatype jira ticket: https://issues.sonatype.org/browse/OSSRH-70091

    Here my configuration, which work with gradle 7.3:

    repositories {
            maven {
                name = "ossrh"
                url = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
    
                credentials {
                    username = System.getenv("MAVEN_USERNAME")
                    password = System.getenv("MAVEN_PASSWORD")
                }
            }
    }