簡體   English   中英

如何將 Gradle 插件發布到 Artifactory?

[英]How do I publish Gradle plugins to Artifactory?

我正在使用這個示例 Gradle 插件項目: https : //github.com/AlainODea/gradle-com.example.hello-plugin

當我運行./gradlew publishToMavenLocal 時,它會在 M2_HOME 中創建這些文件:

  1. com/hello/com.example.hello.gradle.plugin/maven-metadata-local.xml
  2. com/hello/com.example.hello.gradle.plugin/0.1-SNAPSHOT/com.example.hello.gradle.plugin-0.1-SNAPSHOT.pom
  3. com/hello/com.example.hello.gradle.plugin/0.1-SNAPSHOT/maven-metadata-local.xml
  4. com/hello/gradle-com.example.hello-plugin/maven-metadata-local.xml
  5. com/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/gradle-com.example.hello-plugin-0.1-SNAPSHOT.jar
  6. com/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/gradle-com.example.hello-plugin-0.1-SNAPSHOT.pom
  7. 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已經為你聲明了發布,所以你可以從你的構建中刪除這個顯式的發布塊

publishing {
    publications {
        create<MavenPublication>("mavenJava") {
            from(components["java"])
        }
    }
}

然后,您可以在artifactory 發布默認塊中引用所有自動創建的發布,如下所示:

invokeMethod("publications", publishing.publications.names.toTypedArray())

為什么不只是publishations.publications.names ?:

  • publish.publications.names 的類型為 SortedSet<String>
  • ArtifactoryTask.publications() 需要一個 Object... 這實際上是一個 Object[]。
  • 使用 SortedSet<String> 調用 ArtifactoryTask.publications() 將嘗試添加整個集合,就好像它是單個發布一樣
  • 所以你需要 toTypedArray() 使它成為一個 Object[] 以便 varargs 調用工作

這是完整的、更正的人工塊:

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 ,可以使用一個ALL_PUBLICATIONS常量:

artifactory {
  publish {
    contextUrl = 'https://url.com/artifactory'
    repository {
      // repoKey, etc. here
    }
    defaults {
      publications 'ALL_PUBLICATIONS'
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM