Gradle - 使用不同版本的依赖项创建多个 jar

Gradle - Create multiple jars with different versions of a dependency

我需要为 grails3 插件创建两个具有不同版本自定义库的 jar 文件,并将两个工件发布到远程 maven 存储库(工件)。 这是构建文件的示例:

buildscript {
    ext {
        grailsVersion = project.grailsVersion
    }
    repositories {
        jcenter()
        mavenLocal()
        maven { url "https://repo.grails.org/grails/core" }
    }
    dependencies {
        classpath "org.grails:grails-gradle-plugin:$grailsVersion"
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.2"
    }
}

plugins {
    id "io.spring.dependency-management" version "0.5.2.RELEASE"
    id "com.jfrog.bintray" version "1.2"
}

version "0.1.0-SNAPSHOT"
group "org.grails.plugins"

apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: "com.jfrog.artifactory"
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: "spring-boot"
apply plugin: "org.grails.grails-plugin"
apply plugin: "org.grails.grails-gsp"

ext {
    grailsVersion = project.grailsVersion
    gradleWrapperVersion = project.gradleWrapperVersion
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
    maven { url "https://repo.grails.org/grails/core" }
    maven { url "${artifactory_contextUrl}"
        credentials {
            username "${artifactory_user}"
            password "${artifactory_password}"
        }
    }
}

dependencyManagement {
    imports {
        mavenBom "org.grails:grails-bom:$grailsVersion"
    }
    applyMavenExclusions false
}

dependencies {
    provided 'org.springframework.boot:spring-boot-starter-logging'
    provided "org.springframework.boot:spring-boot-starter-actuator"
    provided "org.springframework.boot:spring-boot-autoconfigure"
    provided "org.springframework.boot:spring-boot-starter-tomcat"

    provided "org.grails:grails-web-boot"
    provided "org.grails:grails-dependencies"
    provided 'javax.servlet:javax.servlet-api:3.1.0'

    testCompile "org.grails:grails-plugin-testing"

    console "org.grails:grails-console"

    //TODO build two jars with different versions of this dependency
    compile "com.mycompany.custom-lib:x.x.x-SNAPSHOT"

}

task wrapper(type: Wrapper) {
    gradleVersion = gradleWrapperVersion
}


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


artifactory {
    contextUrl = artifactoryContextUrl

    publish {
        defaults {
            publications('mavenJava')
            maven = true
        }
        repository {
            repoKey = project.version.endsWith('-SNAPSHOT') ? snapshotRepoKey : releaseRepoKey
            username = "${artifactory_user}"
            password = "${artifactory_password}"
        }
    }

}

作为 gradle artifactoryPublish 命令的结果,我想在 build/libs 中有两个 jar 文件:

myplugin-0.1.0-X-SNAPSHOT.jar - 带有库 com.mycompany.custom-lib:1.0.2-SNAPSHOT

myplugin-0.1.0-Y-SNAPSHOT.jar - 带有 lib com.mycompany.custom-lib:2.0.2-SNAPSHOT

我还希望将它们都发布到 artifactory。

我怎样才能做到这一点?

由于您要上传 mavenJava 出版物,您需要做的就是将生成其他 jar 文件的任务添加到 publicationfrom 列表中。