首页 文章

修复Gradle Artifactory插件发布问题

提问于
浏览
0

我有一个使用Gradle构建的多项目构建:

myapp/
    myapp-client/
    myapp-shared/
    myapp-server/
    build.gradle
    settings.gradle

settings.gradle 的样子:

include ':myapp-shared'
include ':myapp-client'
include ':myapp-server'

我已经成功地让Gradle编译我的Groovy源代码,运行单元测试,生成GroovyDocs,并为所有3个子项目打包二进制和源JAR . 构建调用是: gradle clean build groovydoc sourcesJar -Pversion=<whatever version I specify> .

我现在正在尝试添加Gradle-Artifactory插件,以便:

  • 所有3个子项目都为它们生成POM;和

  • 所有3个子项目二进制JAR,POM and 源JAR发布到我在本地运行的Artifactory;和

  • 只要调用 gradle build ,就会执行 artifactoryPublish 任务

这是我最好的尝试(我的完整 build.gradle ):

allprojects {
    buildscript {
        repositories {
            maven {
                url 'http://localhost:8081/artifactory/plugins-release'
                credentials {
                    username = "admin"
                    password = "password"
                }
                name = "maven-main-cache"
            }
        }
        dependencies {
            classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.0.1"
        }
    }

    apply plugin: 'groovy'
    apply plugin: 'maven'
    apply plugin: 'maven-publish'
    apply plugin: "com.jfrog.artifactory"

    version="0.0.1"
    group = "mygroup"

    repositories {
        mavenCentral()
        add buildscript.repositories.getByName("maven-main-cache")
        maven {
            url "http://localhost:8081/artifactory/mydev-snapshots"
        }
    }

    artifactory {
        contextUrl = "http://localhost:8081/artifactory"
        publish {
            repository {
                repoKey = 'mydev-snapshots'
                username = "admin"
                password = "password"
                maven = true
            }
            defaults {
                publications ('mavenJava')
            }
        }
    }

    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java
            }
        }
    }
}

rootProject {
    artifactoryPublish.skip=true
}

subprojects {
    apply plugin: 'groovy'
    apply plugin: 'eclipse'

    sourceCompatibility = '1.7'
    targetCompatibility = '1.7'

    [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

    repositories {
        mavenLocal()
        mavenCentral()
        maven {
            url "https://repository.apache.org/content/repositories/snapshots"
        }
        maven {
            url "http://localhost:8081/artifactory/mydev-snapshots"
        }
        maven {
            url "https://code.google.com/p/guava-libraries/"
        }
    }

    dependencies {
        compile (
            'org.codehaus.groovy:groovy-all:2.3.7'
        )
    }

    task sourcesJar(type: Jar, dependsOn: classes) {
        classifier = 'sources'
        from sourceSets.main.allSource
    }

    task wrapper(type: Wrapper) {
        gradleVersion = '1.11'
    }

    build(dependsOn: 'artifactoryPublish')
}

当我运行 gradle clean build groovydoc sourcesJar -Pversion=0.1.1 时,我得到以下命令行异常:

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\myuser\sandbox\eclipse\workspace\myapp\build.gradle' line: 14

* What went wrong:
A problem occurred evaluating root project 'myapp'.
> You can't change a configuration which is not in unresolved state!

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2.589 secs

我的问题: what is going on here, and what do I need to do (specifically) to fix it and get the Artifactory plugin publishing?

奖金问题:我在2个位置指定版本号(构建调用以及 build.gradle 文件内部 . 我想仅通过构建调用指定版本号. How do I configure artifactoryPublish (or rather, the Gradle-Artifactory plugin) to accept the version I specify from the command-line?

1 回答

  • 3

    这里的问题数量:

    • buildscript 应该是顶级块,而不是 allprojects

    • 使用Artifactory时,你不需要 mavenCentral()

    • 如果要使用 artifactoryPublish ,则需要配置Artifactory插件 . Here are the docs以下是多模块Gradle项目的两个完整工作示例:12 . 一些亮点:

    • 您需要应用 mavenmaven-publish 插件 .

    • 您需要相应地将生成的工件添加到 configurationpublication .

    • 您需要使用正在使用的Artifactory实例配置插件,提供解析和部署存储库名称,凭据(通常仅用于部署)并指定要发布的 configurationpublication .

相关问题