smeeb
smeeb

Reputation: 29477

Fixing Gradle Artifactory plugin publishing issue

I have a multi-project build that I am building with Gradle:

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

Where settings.gradle looks like:

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

I have successfully got Gradle to compile my Groovy source code, run unit tests, generate GroovyDocs, and package both binary and source JARs for all 3 subprojects. The build invocation for which is: gradle clean build groovydoc sourcesJar -Pversion=<whatever version I specify>.

I am now attempting to add the Gradle-Artifactory plugin such that:

Here's my best attempt (my complete 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')
}

When I run gradle clean build groovydoc sourcesJar -Pversion=0.1.1, I get the following command line exception:

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

My question: what is going on here, and what do I need to do (specifically) to fix it and get the Artifactory plugin publishing?

Bonus question: I'm specifying version number in 2 places (the build invocation as well as inside the build.gradle file. I want to specify version number only via the build invocation. How do I configure artifactoryPublish (or rather, the Gradle-Artifactory plugin) to accept the version I specify from the command-line?

Upvotes: 1

Views: 4536

Answers (1)

JBaruch
JBaruch

Reputation: 22893

Number of issues here:

  1. buildscript should be top-level block, not inside allprojects
  2. When using Artifactory, you don't need to specify any other repositories except of Artifactory (don't need mavenCentral())
  3. If you want to use artifactoryPublish you need to configure the Artifactory plugin. Here are the docs and here are two fully working examples of multi-module Gradle projects: 1 and 2. Some highlights:
  4. You need to apply maven or maven-publish plugin.
  5. You need to add the produced artifacts to configuration or publication accordingly.
  6. You need to configure the plugin with Artifactory instance you are working with, provide resolution and deployment repository names, credentials (usually for deployment only) and specify which configuration or publication you want to publish.

Upvotes: 4

Related Questions