smeeb
smeeb

Reputation: 29477

Publishing subprojects with the Gradle-Artifactory plugin

I have the following multi-project Gradle build:

myapp/
    myapp-client/
        build.gradle
        src/** (omitted for brevity)
    myapp-shared/
        build.gradle
        src/** (omitted for brevity)
    myapp-server
        build.gradle
        src/** (omitted for brevity)
    build.gradle
    settings.gradle

Where myapp/build.gradle looks like:

subprojects {
    apply plugin: 'groovy'
    sourceCompatibility = '1.7'
    targetCompatibility = '1.7'
    [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

    repositories {
        mavenCentral()
        maven {
            // My local/private Artifactory
            url "http://localhost:8081/artifactory/myapp-snapshots"
        }
    }

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

And where each of the 3 subproject Gradle files are currently very simple:

dependencies {
    compile (
        'org.apache.commons:commons-lang3:3.3.2'
    )
}

Here's what I'm trying to achieve:

The most compact code I've been able to find and test/confirm for publishing a Gradle-built JAR to Artifactory is:

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

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

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

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

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

When I use this snippet inside a test build.gradle project, and run gradle artifactoryPublish, it correctly publishes my JAR to Artifactory.

My question: How can I add this snippet to my project such that running gradle clean build from the parent dir, on success, produces client and shared JARs being published to Artifactory?

Although not ideal, I would be willing to make the publishing a separate, second step if its not possible to do all this in one fell swoop. Any ideas?

Upvotes: 8

Views: 7134

Answers (1)

JBaruch
JBaruch

Reputation: 22893

Using the artifactory plugin is the right way to do. You need to apply it to allprojects and disable the deployment on the top-level project with artifactoryPublish.skip=true. You can find a fully working example of a project much similar to yours here.

By default build is not dependent on artifactoryPublish ,so you need to run gradle build artifactoryPublish to upload the jars. The reason is you might want to run a lot of builds until you decide to upload something. You can establish the dependency by configuring build to be depending on artifactoryPublish:

build(dependsOn: 'artifactoryPublish')

Upvotes: 4

Related Questions