如何将 Gradle 个插件发布到 Artifactory?
How do I publish Gradle plugins to Artifactory?
我正在使用这个示例 Gradle 插件项目:
https://github.com/AlainODea/gradle-com.example.hello-plugin
当我 运行 ./gradlew publishToMavenLocal 它在 M2_HOME:
中创建这些文件
- com/hello/com.example.hello.gradle.plugin/maven-metadata-local.xml
- com/hello/com.example.hello.gradle.plugin/0.1-SNAPSHOT/com.example.hello.gradle.plugin-0.1-SNAPSHOT.pom
- com/hello/com.example.hello.gradle.plugin/0.1-SNAPSHOT/maven-metadata-local.xml
- com/hello/gradle-com.example.hello-plugin/maven-metadata-local.xml
- com/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/gradle-com.example.hello-plugin-0.1-SNAPSHOT.jar
- com/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/gradle-com.example.hello-plugin-0.1-SNAPSHOT.pom
- com/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/maven-metadata-local.xml
当我 运行 ./gradlew artifactoryPublish 它记录:
Deploying artifact: https://artifactory.example.com/artifactory/libs-release-local-maven/com/example/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/gradle-com.example.hello-plugin-0.1-SNAPSHOT.jar
Deploying artifact: https://artifactory.example.com/artifactory/libs-release-local-maven/com/example/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/gradle-com.example.hello-plugin-0.1-SNAPSHOT.pom
Deploying build descriptor to: https://artifactory.example.com/artifactory/api/build
Build successfully deployed. Browse it in Artifactory under https://artifactory.example.com/artifactory/webapp/builds/gradle-com.example.hello-plugin/1234567890123
正在尝试从另一个 build.gradle 加载插件:
plugins {
id 'java'
id 'com.example.hello' version '0.1-SNAPSHOT'
}
与settings.gradle:
pluginManagement {
repositories {
maven {
url 'https://artifactory.example.com/artifactory/libs-release-local-maven/'
}
}
}
导致此错误:
Plugin [id: 'com.example', version: '0.1-SNAPSHOT'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.example.hello:com.example.hello.gradle.plugin:0.1-SNAPSHOT')
Searched in the following repositories:
maven(https://artifactory.example.com/artifactory/libs-release-local-maven/)
Gradle Central Plugin Repository
我希望在 运行 artifactoryPublish 时将 publishToMavenLocal 创建的所有工件发布到 Artifactory。如果它是错误的工具,我愿意接受 artifactoryPublish 的替代品。
如何将 Gradle 插件发布到 Artifactory?
因为你有 maven-publish 插件,java-gradle-plugin 已经为您声明出版物,因此您可以从构建中删除 this explicit publications block:
publishing {
publications {
create<MavenPublication>("mavenJava") {
from(components["java"])
}
}
}
然后您可以引用 your artifactory publish defaults block 中所有自动创建的出版物,如下所示:
invokeMethod("publications", publishing.publications.names.toTypedArray())
为什么不 publishing.publications.names?:
- publishing.publications.names 的类型为 SortedSet
- ArtifactoryTask.publications() 需要一个对象...实际上是一个对象[]。
- 使用 SortedSet
调用 ArtifactoryTask.publications() 将尝试添加整个集合,就好像它是单个出版物一样
- 因此您需要 toTypedArray() 使其成为对象[],以便可变参数调用有效
这是完整的、修正后的人工制品方块:
artifactory {
setProperty("contextUrl", "https://artifactory.verafin.com/artifactory")
publish(delegateClosureOf<PublisherConfig> {
repository(delegateClosureOf<GroovyObject> {
setProperty("repoKey", "libs-release-local-maven")
})
defaults(delegateClosureOf<GroovyObject> {
invokeMethod("publications", publishing.publications.names.toTypedArray())
})
})
}
这是对您 build.gradle.kts 解决问题的完整改编:
import groovy.lang.GroovyObject
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jfrog.gradle.plugin.artifactory.dsl.PublisherConfig
buildscript {
repositories {
jcenter()
}
}
plugins {
`java-gradle-plugin`
`maven-publish`
`kotlin-dsl`
id("com.jfrog.artifactory") version "4.9.0"
kotlin("jvm") version "1.3.11"
id("io.spring.dependency-management") version "1.0.6.RELEASE"
}
group = "com.example.hello"
version = "0.1-SNAPSHOT"
gradlePlugin {
plugins {
create("helloPlugin") {
id = "com.example.hello"
implementationClass = "com.example.HelloPlugin"
}
}
}
repositories {
mavenCentral()
}
dependencyManagement {
imports {
mavenBom("org.junit:junit-bom:5.3.2")
}
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
testImplementation(kotlin("test"))
testImplementation(kotlin("test-junit5"))
testImplementation("org.junit:junit-bom:latest.release")
testImplementation("org.junit.jupiter:junit-jupiter-api")
testImplementation("com.natpryce:hamkrest:1.7.0.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}
tasks {
withType<JavaExec> {
jvmArgs = listOf("-noverify", "-XX:TieredStopAtLevel=1")
}
withType<KotlinCompile> {
val javaVersion = JavaVersion.VERSION_1_8.toString()
sourceCompatibility = javaVersion
targetCompatibility = javaVersion
kotlinOptions {
apiVersion = "1.3"
javaParameters = true
jvmTarget = javaVersion
languageVersion = "1.3"
}
}
withType<Test> {
@Suppress("UnstableApiUsage")
useJUnitPlatform()
}
}
artifactory {
publish(delegateClosureOf<PublisherConfig> {
repository(delegateClosureOf<GroovyObject> {
setProperty("repoKey", "libs-release-local-maven")
})
defaults(delegateClosureOf<GroovyObject> {
invokeMethod("publications", publishing.publications.names.toTypedArray())
})
})
}
这是一个日志,显示插件工件已成功部署到 Artifactory:
Deploying artifact: https://artifactory.example.com/artifactory/libs-release-local-maven/com/example/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/gradle-com.example.hello-plugin-0.1-SNAPSHOT.jar
Deploying artifact: https://artifactory.example.com/artifactory/libs-release-local-maven/com/example/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/gradle-com.example.hello-plugin-0.1-SNAPSHOT.pom
Deploying artifact: https://artifactory.example.com/artifactory/libs-release-local-maven/com/example/hello/com.example.hello.gradle.plugin/0.1-SNAPSHOT/com.example.hello.gradle.plugin-0.1-SNAPSHOT.pom
Deploying build descriptor to: https://artifactory.example.com/artifactory/api/build
Build successfully deployed. Browse it in Artifactory under https://artifactory.example.com/artifactory/webapp/builds/gradle-com.example.hello-plugin/1234567890123
从 build-info-extractor-gradle 插件的 4.19 版开始,有一个 ALL_PUBLICATIONS
常量可以使用:
artifactory {
publish {
contextUrl = 'https://url.com/artifactory'
repository {
// repoKey, etc. here
}
defaults {
publications 'ALL_PUBLICATIONS'
}
}
}
在投入大量时间后,我终于得到了将自定义插件部署到私有工件的工作代码
artifactory {
contextUrl = 'http://localhost:8081/artifactory'
publish {
repository {
repoKey = 'libs-release-local'
username = ‘username’
password = ‘password’
}
defaults {
publications("pluginMaven")
publishArtifacts = true
publishPom = true
}
}
}
Gradle 插件依赖项
plugins {
id "java-gradle-plugin"
id "org.gradle.kotlin.kotlin-dsl" version "2.1.4"
id "com.jfrog.artifactory" version "4.27.1"
id 'org.jetbrains.kotlin.jvm' version '1.5.10'
id "maven-publish"
id "com.gradle.plugin-publish" version "0.18.0"
}
gradle-wrapper.属性
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
发布必须使用 pluginMaven
这是我的完整 build.Gradle 文件
plugins {
id "java-gradle-plugin"
id "org.gradle.kotlin.kotlin-dsl" version "2.1.4"
id "com.jfrog.artifactory" version "4.27.1"
id 'org.jetbrains.kotlin.jvm' version '1.5.10'
id "maven-publish"
id "com.gradle.plugin-publish" version "0.18.0"
}
group = "com.example"
version = "1.0"
repositories {
mavenCentral()
google()
}
gradlePlugin {
plugins {
simplePlugin {
id = 'com.example.plugin'
displayName = 'Code Check Plugin'
description = 'This plugin will be used for checking code convention'
implementationClass = 'com.example.plugin.CodeCheckPlugin'
}
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation "com.android.tools.build:gradle:4.0.2"
implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
}
tasks.create("checkJavaVersion").doLast {
def javaVersion = JavaVersion.current()
if (!javaVersion.isJava8()) {
throw new GradleException(
"The plugin must be published under Java 1.8 but $javaVersion is found"
)
}
}
artifactory {
contextUrl = 'http://localhost:8080/artifactory'
publish {
repository {
repoKey = 'libs-release-local'
username = 'user_name'
password = 'password'
}
defaults {
publications("pluginMaven")
publishArtifacts = true
publishPom = true
}
}
}
我正在使用这个示例 Gradle 插件项目: https://github.com/AlainODea/gradle-com.example.hello-plugin
当我 运行 ./gradlew publishToMavenLocal 它在 M2_HOME:
中创建这些文件- com/hello/com.example.hello.gradle.plugin/maven-metadata-local.xml
- com/hello/com.example.hello.gradle.plugin/0.1-SNAPSHOT/com.example.hello.gradle.plugin-0.1-SNAPSHOT.pom
- com/hello/com.example.hello.gradle.plugin/0.1-SNAPSHOT/maven-metadata-local.xml
- com/hello/gradle-com.example.hello-plugin/maven-metadata-local.xml
- com/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/gradle-com.example.hello-plugin-0.1-SNAPSHOT.jar
- com/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/gradle-com.example.hello-plugin-0.1-SNAPSHOT.pom
- com/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/maven-metadata-local.xml
当我 运行 ./gradlew artifactoryPublish 它记录:
Deploying artifact: https://artifactory.example.com/artifactory/libs-release-local-maven/com/example/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/gradle-com.example.hello-plugin-0.1-SNAPSHOT.jar
Deploying artifact: https://artifactory.example.com/artifactory/libs-release-local-maven/com/example/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/gradle-com.example.hello-plugin-0.1-SNAPSHOT.pom
Deploying build descriptor to: https://artifactory.example.com/artifactory/api/build
Build successfully deployed. Browse it in Artifactory under https://artifactory.example.com/artifactory/webapp/builds/gradle-com.example.hello-plugin/1234567890123
正在尝试从另一个 build.gradle 加载插件:
plugins {
id 'java'
id 'com.example.hello' version '0.1-SNAPSHOT'
}
与settings.gradle:
pluginManagement {
repositories {
maven {
url 'https://artifactory.example.com/artifactory/libs-release-local-maven/'
}
}
}
导致此错误:
Plugin [id: 'com.example', version: '0.1-SNAPSHOT'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.example.hello:com.example.hello.gradle.plugin:0.1-SNAPSHOT')
Searched in the following repositories:
maven(https://artifactory.example.com/artifactory/libs-release-local-maven/)
Gradle Central Plugin Repository
我希望在 运行 artifactoryPublish 时将 publishToMavenLocal 创建的所有工件发布到 Artifactory。如果它是错误的工具,我愿意接受 artifactoryPublish 的替代品。
如何将 Gradle 插件发布到 Artifactory?
因为你有 maven-publish 插件,java-gradle-plugin 已经为您声明出版物,因此您可以从构建中删除 this explicit publications block:
publishing {
publications {
create<MavenPublication>("mavenJava") {
from(components["java"])
}
}
}
然后您可以引用 your artifactory publish defaults block 中所有自动创建的出版物,如下所示:
invokeMethod("publications", publishing.publications.names.toTypedArray())
为什么不 publishing.publications.names?:
- publishing.publications.names 的类型为 SortedSet
- ArtifactoryTask.publications() 需要一个对象...实际上是一个对象[]。
- 使用 SortedSet
调用 ArtifactoryTask.publications() 将尝试添加整个集合,就好像它是单个出版物一样 - 因此您需要 toTypedArray() 使其成为对象[],以便可变参数调用有效
这是完整的、修正后的人工制品方块:
artifactory {
setProperty("contextUrl", "https://artifactory.verafin.com/artifactory")
publish(delegateClosureOf<PublisherConfig> {
repository(delegateClosureOf<GroovyObject> {
setProperty("repoKey", "libs-release-local-maven")
})
defaults(delegateClosureOf<GroovyObject> {
invokeMethod("publications", publishing.publications.names.toTypedArray())
})
})
}
这是对您 build.gradle.kts 解决问题的完整改编:
import groovy.lang.GroovyObject
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jfrog.gradle.plugin.artifactory.dsl.PublisherConfig
buildscript {
repositories {
jcenter()
}
}
plugins {
`java-gradle-plugin`
`maven-publish`
`kotlin-dsl`
id("com.jfrog.artifactory") version "4.9.0"
kotlin("jvm") version "1.3.11"
id("io.spring.dependency-management") version "1.0.6.RELEASE"
}
group = "com.example.hello"
version = "0.1-SNAPSHOT"
gradlePlugin {
plugins {
create("helloPlugin") {
id = "com.example.hello"
implementationClass = "com.example.HelloPlugin"
}
}
}
repositories {
mavenCentral()
}
dependencyManagement {
imports {
mavenBom("org.junit:junit-bom:5.3.2")
}
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
testImplementation(kotlin("test"))
testImplementation(kotlin("test-junit5"))
testImplementation("org.junit:junit-bom:latest.release")
testImplementation("org.junit.jupiter:junit-jupiter-api")
testImplementation("com.natpryce:hamkrest:1.7.0.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}
tasks {
withType<JavaExec> {
jvmArgs = listOf("-noverify", "-XX:TieredStopAtLevel=1")
}
withType<KotlinCompile> {
val javaVersion = JavaVersion.VERSION_1_8.toString()
sourceCompatibility = javaVersion
targetCompatibility = javaVersion
kotlinOptions {
apiVersion = "1.3"
javaParameters = true
jvmTarget = javaVersion
languageVersion = "1.3"
}
}
withType<Test> {
@Suppress("UnstableApiUsage")
useJUnitPlatform()
}
}
artifactory {
publish(delegateClosureOf<PublisherConfig> {
repository(delegateClosureOf<GroovyObject> {
setProperty("repoKey", "libs-release-local-maven")
})
defaults(delegateClosureOf<GroovyObject> {
invokeMethod("publications", publishing.publications.names.toTypedArray())
})
})
}
这是一个日志,显示插件工件已成功部署到 Artifactory:
Deploying artifact: https://artifactory.example.com/artifactory/libs-release-local-maven/com/example/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/gradle-com.example.hello-plugin-0.1-SNAPSHOT.jar
Deploying artifact: https://artifactory.example.com/artifactory/libs-release-local-maven/com/example/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/gradle-com.example.hello-plugin-0.1-SNAPSHOT.pom
Deploying artifact: https://artifactory.example.com/artifactory/libs-release-local-maven/com/example/hello/com.example.hello.gradle.plugin/0.1-SNAPSHOT/com.example.hello.gradle.plugin-0.1-SNAPSHOT.pom
Deploying build descriptor to: https://artifactory.example.com/artifactory/api/build
Build successfully deployed. Browse it in Artifactory under https://artifactory.example.com/artifactory/webapp/builds/gradle-com.example.hello-plugin/1234567890123
从 build-info-extractor-gradle 插件的 4.19 版开始,有一个 ALL_PUBLICATIONS
常量可以使用:
artifactory {
publish {
contextUrl = 'https://url.com/artifactory'
repository {
// repoKey, etc. here
}
defaults {
publications 'ALL_PUBLICATIONS'
}
}
}
在投入大量时间后,我终于得到了将自定义插件部署到私有工件的工作代码
artifactory {
contextUrl = 'http://localhost:8081/artifactory'
publish {
repository {
repoKey = 'libs-release-local'
username = ‘username’
password = ‘password’
}
defaults {
publications("pluginMaven")
publishArtifacts = true
publishPom = true
}
}
}
Gradle 插件依赖项
plugins {
id "java-gradle-plugin"
id "org.gradle.kotlin.kotlin-dsl" version "2.1.4"
id "com.jfrog.artifactory" version "4.27.1"
id 'org.jetbrains.kotlin.jvm' version '1.5.10'
id "maven-publish"
id "com.gradle.plugin-publish" version "0.18.0"
}
gradle-wrapper.属性
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
发布必须使用 pluginMaven
这是我的完整 build.Gradle 文件
plugins {
id "java-gradle-plugin"
id "org.gradle.kotlin.kotlin-dsl" version "2.1.4"
id "com.jfrog.artifactory" version "4.27.1"
id 'org.jetbrains.kotlin.jvm' version '1.5.10'
id "maven-publish"
id "com.gradle.plugin-publish" version "0.18.0"
}
group = "com.example"
version = "1.0"
repositories {
mavenCentral()
google()
}
gradlePlugin {
plugins {
simplePlugin {
id = 'com.example.plugin'
displayName = 'Code Check Plugin'
description = 'This plugin will be used for checking code convention'
implementationClass = 'com.example.plugin.CodeCheckPlugin'
}
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation "com.android.tools.build:gradle:4.0.2"
implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
}
tasks.create("checkJavaVersion").doLast {
def javaVersion = JavaVersion.current()
if (!javaVersion.isJava8()) {
throw new GradleException(
"The plugin must be published under Java 1.8 but $javaVersion is found"
)
}
}
artifactory {
contextUrl = 'http://localhost:8080/artifactory'
publish {
repository {
repoKey = 'libs-release-local'
username = 'user_name'
password = 'password'
}
defaults {
publications("pluginMaven")
publishArtifacts = true
publishPom = true
}
}
}