Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publishing both jar and sources jar to Artifactory from Gradle

Here is my build.gradle:

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: 'codenarc'
apply plugin: 'maven-publish'
apply plugin: "com.jfrog.artifactory"

version="0.0.2"
group = "mylib"

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

dependencies {
    compile 'commons-validator:commons-validator:1.4.0'
    testCompile 'junit:junit:4.11'
}

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

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

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

artifacts {
    archives sourcesJar
}

task dist(type: Zip, dependsOn: build) {
    classifier = 'buildreport'

    from('build/test-results') {
        include '*.xml'
        into 'tests'
    }

    from('build/reports/codenarc') {
        into 'reports'
    }

    from('build/docs') {
        into 'api'
    }

    from(sourcesJar) {
        into 'source'
    }

    from('build/libs') {
        exclude '*-sources.jar'
        into 'bin'
    }
}

Based on this current setup:

  • To build my JAR I have to run gradle clean build groovydoc sourcesJar dist and then
  • To publish to Artifactory, I have to run a second command of gradle artifactoryPublish

Two things I'm looking to change here:

  1. gradle artifactoryPublish only publishes my built JAR and a dynamically-created POM to Artifactory. I'd like it to also publish the sources JAR that my build is creating. How?; and
  2. Ideally I'd like to be able to do all of the above by just invoking gradle publish instead of having to run the 2 commands sequentially. Is this possible? If so, how?
like image 225
unfettered Avatar asked Nov 20 '14 19:11

unfettered


People also ask

How do I publish a jar to Artifactory?

Upload to the Artifactory repository manually The easiest way, and the least DevOps-friendly way, to upload a JAR to an Artifactory repository is to simply log in to the administrative console, select a target folder and drag and drop the JAR onto the deployment screen.


1 Answers

When it comes to publishing source you need to modify your script in the following way:

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
            artifact (sourcesJar) { 
                classifier = 'sources'
            }
        }
    }
}

When it comes to a single command what you need to do is to define dependencies between tasks. Unfortunately I can't try the script so it may be redundant but should do the job:

artifactoryPublish.dependsOn('clean', 'build', 'groovydoc', 'sourcesJar', 'dist')
publish.dependsOn(artifactoryPublish)
like image 72
Opal Avatar answered Dec 07 '22 22:12

Opal