简体   繁体   English

如何将 Gradle 插件发布到 Artifactory?

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

I am working with this example Gradle Plugin project: https://github.com/AlainODea/gradle-com.example.hello-plugin我正在使用这个示例 Gradle 插件项目: https : //github.com/AlainODea/gradle-com.example.hello-plugin

When I run ./gradlew publishToMavenLocal it creates these files in M2_HOME:当我运行./gradlew publishToMavenLocal 时,它会在 M2_HOME 中创建这些文件:

  1. com/hello/com.example.hello.gradle.plugin/maven-metadata-local.xml 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 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 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 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 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 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 com/hello/gradle-com.example.hello-plugin/0.1-SNAPSHOT/maven-metadata-local.xml

When I run ./gradlew artifactoryPublish it logs:当我运行./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

Attempting to load the plug-in from another build.gradle:尝试从另一个 build.gradle 加载插件:

plugins {
    id 'java'
    id 'com.example.hello' version '0.1-SNAPSHOT'
}

With settings.gradle:使用 settings.gradle:

pluginManagement {
    repositories {
        maven {
            url 'https://artifactory.example.com/artifactory/libs-release-local-maven/'
        }
    }
}

Results in this error:导致此错误:

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

I'd like to get all of the artifacts that publishToMavenLocal creates to be published to Artifactory when I run artifactoryPublish.我希望在运行 artifactoryPublish 时将 publishToMavenLocal 创建的所有工件发布到 Artifactory。 I am open to alternatives to artifactoryPublish if it is the wrong tool.如果它是错误的工具,我对 artifactoryPublish 的替代品持开放态度。

How do I publish Gradle plugins to Artifactory?如何将 Gradle 插件发布到 Artifactory?

Since you have the maven-publish plugin on, the java-gradle-plugin already declares publications for you, so you can remove this explicit publications block from your build:因为你有maven-publish插件, java-gradle-plugin已经为你声明了发布,所以你可以从你的构建中删除这个显式的发布块

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

You can then reference all automatically created publications in your artifactory publish defaults block as follows:然后,您可以在artifactory 发布默认块中引用所有自动创建的发布,如下所示:

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

Why not just publishing.publications.names ?:为什么不只是publishations.publications.names ?:

  • publishing.publications.names has type SortedSet<String> publish.publications.names 的类型为 SortedSet<String>
  • ArtifactoryTask.publications() expects an Object... which is an Object[] really. ArtifactoryTask.publications() 需要一个 Object... 这实际上是一个 Object[]。
  • Calling ArtifactoryTask.publications() with a SortedSet<String> will attempt to add the entire set as if it is a single publication使用 SortedSet<String> 调用 ArtifactoryTask.publications() 将尝试添加整个集合,就好像它是单个发布一样
  • So you need toTypedArray() to make it a Object[] so that the varargs call works所以你需要 toTypedArray() 使它成为一个 Object[] 以便 varargs 调用工作

Here's the complete, corrected artifactory block:这是完整的、更正的人工块:

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())
        })
    })
}

Here's a complete adaptation of your build.gradle.kts solving the problem:这是解决问题的 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())
        })
    })
}

Here's a log showing the successful deployment of the plugin artifact to Artifactory:这是一个日志,显示了插件工件成功部署到 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

Since version 4.19 of the build-info-extractor-gradle plugin, there's an ALL_PUBLICATIONS constant that can be used:从 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.

相关问题 如何使用 maven-publish 和 artifactory-gradle-plugin 生成 maven-metata.xml? - How do I generate maven-metata.xml with maven-publish and the artifactory-gradle-plugin? 如何编写脚本 Gradle 以将 shadowjar 发布到 Artifactory - How to script Gradle in order to publish shadowjar into Artifactory 如何发布Gradle生成的BOM - How do I publish gradle generated BOM 如何依赖Gradle中Artifactory的版本和快照? - How do I depend on both releases and snapshots from Artifactory in Gradle? Gradle发布到Artifactory的特定回购 - Gradle publish to the specific repo of the Artifactory Gradle Artifactory Plugin - 如何从项目中的多个模块发布工件? - Gradle Artifactory Plugin - How to publish artifacts from multiple modules in a project? 如何使用gradle将多个zip文件发布(部署)到工件上? - How to publish(deploy) multiple zip files to artifactory using gradle? 如何在不发布依赖项目的情况下使用 Gradle 发布到 Artifactory - How to publish to Artifactory with Gradle without publishing dependent projects 与Maven发布一起摇篮:如何自动版本化和将目录中的非渐变罐子发布到Artifactory - Gradle with maven-publish: how to autoversion and publish non-gradle jars in a directory to Artifactory 我如何在teamcity上发布gradle来发布使用maven插件的gradle构建? - How can i do a gradle publish on teamcity to publish a gradle build that uses the maven plugin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM