Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • When I run gradle clean build -Pversion=0.1.5 from the parent myapp directory, I want all three subprojects to be built with a version of 0.1.5; hence myapp-client-0.1.5.jar, myapp-shared-0.1.5.jar and myapp-server-0.1.5.jar. Currently my build already does this, however...
  • If the client and shared JARs build successfully, I also want them published to my private Artifactory repo. (I don't wish to publish the server JAR).

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?

like image 583
smeeb Avatar asked Dec 28 '14 15:12

smeeb


1 Answers

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')
like image 123
JBaruch Avatar answered Sep 29 '22 07:09

JBaruch