Publish jar and source jar from gradle to artifact

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?
This question and all comments follow the "Attribution Required."

All Answers

Answers Loading...
Leave a Reply
You must be logged in to post a answer.
About the author