gradle - 如何将Gradle插件发布到Artifactory?

标签 gradle kotlin artifactory gradle-plugin

我正在使用此示例Gradle Plugin项目:
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构件发布时,它将记录:
    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
    

    我想让publishToMavenLocal创建的所有 Artifact 都可以在运行artifactoryPublish时发布到Artifactory。如果它是错误的工具,那么我愿意接受articleoryPublish的替代方法。

    如何将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()需要一个Object ...实际上是Object []。
  • 使用SortedSet 调用ArtifactoryTask.publications()将尝试将整个集合添加为单个发布
  • 因此,您需要toTypedArray()使其成为Object [],以便varargs调用可以工作

  • 这是完整的,已纠正的 Artifact 块:
    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())
            })
        })
    }
    

    以下是显示插件 Artifact 成功部署到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
    

    关于gradle - 如何将Gradle插件发布到Artifactory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54757070/

    相关文章:

    gradle - 在 Github Actions 中找不到或加载主类 org.gradle.wrapper.GradleWrapperMain

    android - 无法使用 ViewModelFactory 实例化我的 ViewModel

    ssl - Artifactory jfrog cli : x509: certificate signed by unknown authority

    android - 创建签名包后执行任务

    groovy - 从外部文件加载属性到 build.gradle

    android - 模拟每个 { ... } block 内的缺失调用

    kotlin - 如何在 Kotlin/Native 中构建 kotlinx.coroutines(测试版本 0.23.4-native-1)

    java - 模块依赖与 Artifactory

    java - 通过maven从远程存储库拉取jar文件来执行jar文件

    android - java.io.File 无法转换为 org.gradle.api.artifacts.Configuration