Java 在single build.gradle中使用两组不同的依赖项编译源代码

Java 在single build.gradle中使用两组不同的依赖项编译源代码,java,gradle,Java,Gradle,我正在研究gradle脚本,其中有两个独立的cognos和其他依赖项列表 清单1: cognos:a:10.1.1 cognos:b:10.1.1 cognos:c:10.1.1 cognos:d:10.1.1 com:axis:2.0.3 com:webroot:5.0.3 清单2: cognos:a:10.2.2 cognos:b:10.2.2 cognos:c:10.2.2 cognos:d:10.2.2 traven:nt:10.5.0 traven:txtx:5.2.1 我需要首先

我正在研究gradle脚本,其中有两个独立的cognos和其他依赖项列表

清单1:

cognos:a:10.1.1
cognos:b:10.1.1
cognos:c:10.1.1
cognos:d:10.1.1
com:axis:2.0.3
com:webroot:5.0.3
清单2:

cognos:a:10.2.2
cognos:b:10.2.2
cognos:c:10.2.2
cognos:d:10.2.2
traven:nt:10.5.0
traven:txtx:5.2.1
我需要首先编译源代码,列出1个依赖项,然后列出2个依赖项,并在artifactory中发布具有以下名称的工件

具有列表1和列表2依赖项的工件

abc-1.0.0-cognos10.1.1
abc-1.0.0-cognos10.2.2
我可以用build.gradle来完成,但我可以用两个单独的build.gradle脚本来完成。我不确定我们如何在单体build.gradle脚本中实现这个目标。有人知道如何在单体build.gradle中实现它吗

apply plugin: 'java'


version = '1.0'
sourceCompatibility = 1.7
targetCompatibility = 1.7

//create a single Jar with all dependencies
task fatJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',  
            'Implementation-Version': version,
            'Main-Class': 'com.mkyong.DateUtils'
    }
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

//Get dependencies from Maven central repository
repositories {
    mavenCentral()
}

//Project dependencies
dependencies {
    compile 'cognos:a:10.1.1
    compile  'cognos:b:10.1.1'
    compile  'cognos:c:10.1.1'
    compile  'cognos:d:10.1.1'
    compile 'traven:nt:10.5.0'
    compile 'traven:txtx:5.2.1'
}

在build.gradle中,使用参数替换cognos和app version:

version = "1.0.0-cognos${cognosVersion}"
sourceCompatibility = 1.7
targetCompatibility = 1.7

...

dependencies {
    compile "cognos:a:${cognosVersion}"
    compile "cognos:b:${cognosVersion}"
    compile "cognos:c:${cognosVersion}"
    compile "cognos:d:${cognosVersion}"
}
然后,您可以手动运行几次:

gradle build -PcognosVersion=xxxx
如果您还想实现自动化,可以编写第二个gradle驱动程序脚本release.gradle:

defaultTasks  'performRelease'

task performRelease() << {
    ['10.1.1','10.2.2'].each{
        def tempTask = tasks.create(name: "execute_$it", type: GradleBuild)
        tempTask.buildFile='build.gradle'
        tempTask.tasks = ['build']
        tempTask.startParameter.projectProperties = [cognosVersion: it]
        tempTask.execute()
    }
}
defaultTasks'performRelease'

task performRelease()您是否愿意使用外部“驱动程序”脚本以版本作为参数多次调用此一个gradle文件?你能详细说明一下吗?我同意,但将来如果他们在列表1和列表2中添加了一些完全不同的依赖项,或者在列表1和列表2中添加了一些不在列表2中的依赖项,则此解决方案在将来将不起作用,始终可以向构建脚本中添加某些内容,以破坏当前设置。我的回答解决了你问题中提到的问题。将其扩展到您所描述的情况很容易。我现在更新了我的问题,以便让您更清楚地了解需求,使整个依赖项列表成为一个参数,而不是cognos版本。