将jar和源jar发布到Gradle的Artifactory

时间:2014-11-20 19:08:31

标签: gradle artifactory

这是我的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'
    }
}

基于此当前设置:

  • 要构建我的JAR,我必须运行gradle clean build groovydoc sourcesJar dist然后
  • 要发布到Artifactory,我必须运行gradle artifactoryPublish
  • 的第二个命令

我想在这里改变两件事:

  1. gradle artifactoryPublish只发布我构建的JAR和动态创建的POM到Artifactory。我希望它还发布我的构建正在创建的源JAR。 如何?;和
  2. 理想情况下,我希望能够通过调用gradle publish而不必按顺序运行2个命令来完成上述所有操作。 这可能吗?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:17)

说到发布源代码,您需要按以下方式修改脚本:

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

当涉及到单个命令时,您需要做的是定义任务之间的依赖关系。不幸的是,我无法尝试脚本,所以它可能是多余的,但应该完成这项工作:

artifactoryPublish.dependsOn('clean', 'build', 'groovydoc', 'sourcesJar', 'dist')
publish.dependsOn(artifactoryPublish)