1954

Thoughts, stories and ideas.

Gradle maven-publish pluginでSonatype OSSRHにlibraryをpublishする

Gradleでmaven repositoryにlibraryを上げる場合、maven pluginとmaven-publish pluginの二つの選択肢がある。

以前はmaven-publishがsigningをサポートしてなかったりでmaven pluginを使うケースが多かったようだが、現在はすでにstableになっている。

docs.gradle.org

use of the maven plugin is discouraged as it will eventually be deprecated — please migrate

と記載もあり、今後はmaven-publishを使うのがよいだろう。

Initial Setup

まずはこちらに従い、accountおよびticketを起票する。

central.sonatype.org

すでに済んでいる人は飛ばしてよい。

Setup credentials

ここもSonatypeのGuideに従い、$HOME/.gradle/gradle.propertiesにcredentialsを用意したものとする。

https://central.sonatype.org/pages/gradle.html#credentials

Setup build.gradle

ほとんどGradleのofficial documentを見ながら設定すればよいだけだが、maven pluginとの違いについて少し。

maven pluginではpomに署名するために以下のような記述が必要だった。

uploadArchives {
    repositories {
        mavenDeployer {
            beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
        }
    }
}

maven-publish pluginではMavenPublicationを通して色々な設定をしていくわけだが、signブロックを以下のように書けばpomも自動的に含まれるので特別な記述は必要無い。

signing {
    sign publishing.publications.mavenJava
}

Publish

maven-publish pluginではpublish taskを使う。

$ ./gradlew --no-daemon publish

local maven repositoryにpublishする場合は以下。

$ ./gradlew --no-daemon publishToMavenLocal

Conclusion

maven pluginを使っていたlibraryをmaven-publishに移行してみたが、とくに問題に当たらず公開できた。

完全なsampleは以下にある。

github.com