Deploying to OSSRH with Gradle in 2020

It seems that the method described in the official page is a little bit old. Here I’ll introduce a modern way.

Metadata and Signing

Use the maven-publish plugin instead of the maven plugin. No need to configure the artifacts extension.

Jar Files

The classifier property of Jar task is now deprecated. Instead, we can use withJavadocJar() and withSourcesJar() in the java extension.

java {
    withJavadocJar()
    withSourcesJar()
}

Signing artifacts

We’re using maven-publish plugin, so the signing extension in our build script will be like the following:

ext.isReleaseVersion = !version.endsWith("SNAPSHOT")

// -------------------------------------
// here 'publishing' extension will come
// -------------------------------------

signing {
    sign publishing.publications.mavenJava
}
tasks.withType(Sign) {
    onlyIf { isReleaseVersion }
}

Metadata Definition and Upload

Instead of uploadArchives extension for the maven plugin, configure publishing extension for the maven-publish plugin.

publishing {
    repositories {
        maven {
            def releaseRepo = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
            def snapshotRepo = "https://oss.sonatype.org/content/repositories/snapshots/"
            url = isReleaseVersion ? releaseRepo : snapshotRepo
            credentials {
                username = project.hasProperty('ossrhUsername') ? ossrhUsername : "Unknown user"
                password = project.hasProperty('ossrhPassword') ? ossrhPassword : "Unknown password"
            }
        }
    }

    publications {
        mavenJava(MavenPublication) {
            pom {
                groupId = 'com.example'
                name = 'example project'
                description = 'Example Project to learn how to deploy to OSSRH'
                url = 'https://example.com/'
                from components.java
                licenses {
                    license {
                        name = 'The Apache License, Version 2.0'
                        url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                scm {
                    connection = 'scm:git:git@github.com:example.com/example.git'
                    developerConnection = 'scm:git:git@github.com:example.com/example.git'
                    url = 'https://github.com/example.com/example/'
                }
            }
        }
    }
}

Deployment

Use the publishToMavenLocal task to publish artifacts to maven local repository.

Use the publish task to publish artifacts to maven remote repositories. Don’t forget to visit oss.sonatype.org to close and release your staging repository.

原文链接:Deploying to OSSRH with Gradle in 2020

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容