LoginSignup
1
0

build.gradle.ktsでMavenリポジトリにpublish

Last updated at Posted at 2022-03-31

===
日記

環境

  • Gradle 7.1
  • Kotnin 1.6
  • Nexus Repository Manager 3.36.0

サンプル

build.gradle.kts に Maven Pluginを追加

さらに、publish対象モジュールの名前とバージョンなどを定義

build.gradle.kts
plugins {
    kotlin("jvm") version "1.5.31"
    `maven-publish`
}

//...

publishing {
    val myGroupId = "jp.live-on.shokkaa"
    val myArtifactId = "snmp4jutils"
    val myVersion = "1.1"

    publications {
        create<MavenPublication>("maven") {
            groupId = myGroupId
            artifactId = myArtifactId
            version = myVersion
            from(components["kotlin"])
        }
    }
}

ローカルリポジトリへのpublish

個人で使うならこれで十分

gradlew publishToMavenLocal

$HOME/.m2/repository 以下に成果物ファイルがコピーされる。

指定したローカルリポジトリへのpublish

デフォルトのリポジトリ(Maven)と区別するため、リポジトリ名(例えば name="ProjectLocal"`)を指定

publishing {
//...
    repositories {
        maven {
            name = "ProjectLocal"
            url = uri(layout.buildDirectory.dir("repos"))
        }
    }
}
gradlew publishMavenPublicationToProjectLocalRepository

プロジェクト中のディレクトリ build/repos/ 以下にリポジトリが生成される。

リモートリポジトリへのpublish

gradlew publish

例は自前のNexus repository Managerのデフォルトレポジトリ(maven-releases)にpublishしている。

build.gradle.kts
plugins {
    java
    `maven-publish`
}

group = "jp.wjp.shokkaa"
version = "1.0"

publishing {
    publications {
        create<MavenPublication>("maven") {
            from(components["java"])
        }
        repositories {
            maven {
                url = uri("http://192.168.3.115:8081/repository/maven-releases")
                isAllowInsecureProtocol = true // 非セキュアの場合
                credentials {
                    username = System.getenv("MAVEN_USER")  // example: MAVEN_USER=admin
                    password = System.getenv("MAVEN_PASSWORD") // example: MAVEN_PASSWORD=secret-word
                }
            }
        }
    }
}

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0