修复Gradle Artifactory插件发布问题

时间:2014-12-31 18:13:21

标签: groovy gradle artifactory

我有一个使用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 源JAR都发布到我在本地运行的Artifactory;和
  • 只要调用artifactoryPublish
  • gradle build任务就会执行

这是我最好的尝试(我的完整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

我的问题:这里发生了什么,我需要做什么(特别是)修复它并获得Artifactory插件发布?

奖金问题:我在2个位置指定版本号(构建调用以及build.gradle文件内部。我想通过构建调用指定版本号 如何配置artifactoryPublish(或更确切地说,Gradle-Artifactory插件)以接受我从命令行指定的版本?

1 个答案:

答案 0 :(得分:3)

此处的问题数量:

  1. buildscript应该是顶级阻止,而不是allprojects
  2. 使用Artifactory时,除了Artifactory之外,您不需要指定任何其他存储库(不需要mavenCentral()
  3. 如果您想使用artifactoryPublish,则需要配置Artifactory插件。 Here are the docs以下是多模块Gradle项目的两个完整工作示例:12。一些亮点:
    1. 您需要应用mavenmaven-publish插件。
    2. 您需要相应地将生成的工件添加到configurationpublication
    3. 您需要使用正在使用的Artifactory实例配置插件,提供解析和部署存储库名称,凭据(通常仅用于部署)并指定要发布的configurationpublication