Publishing Subprojects with Gradle Plugin -Artifactory

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

it looks like this:

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 sub-projects of Gradle files are currently very simple:

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

      

This is what I am trying to achieve:

  • When I run gradle clean build -Pversion=0.1.5

    from the parent directory myapp

    , I want all three subprojects to be built with version 0.1.5; therefore myapp-client-0.1.5.jar

    , myapp-shared-0.1.5.jar

    and myapp-server-0.1.5.jar

    . Currently my build is already doing this, however ...
  • If the client and shared JARs are successfully generated, I also want them to be published in my private Artifactory refactor. (I don't want to publish the JAR server).

Most compact code I could find and verify / validate for posting Gradle-built JAR for Artifactory:

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 project build.gradle

and run it gradle artifactoryPublish

, it publishes my JAR for Artifactory correctly.

My question is: How do I add this snippet to my project so that launching gradle clean build

from the parent directory, on successful launch, will publish the publication by the client and shared JAR to Artifactory?

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

+3


source to share


1 answer


Using a plugin artifactory

is the correct way. You need to apply it to allprojects

and disable deployment in the top-level project with artifactoryPublish.skip=true

. You can find a fully working example of a project that looks a lot like yours here .

The default build

is independent artifactoryPublish

, so you need to run gradle build artifactoryPublish

to load the jars. The reason is that you might want to run many assemblies until you decide to download something. You can install the dependency by configuring build

depending on artifactoryPublish

:



build(dependsOn: 'artifactoryPublish')

      

+2


source







All Articles