我正在尝试使用gradle并尝试设置一个系统来构建应用程序的不同风格(品牌),这些系统主要因配置而异.到目前为止我所拥有的是两个版本的构建脚本 – 两者都不起作用.
版本1
第一个特定于flavor的资源文件夹flavor-res被添加到sourcesets,从而覆盖了一些默认资源.任务规则定义每种风味的任务,这应该(理想情况下)触发整个jar的构建.
这样可以正常工作并生成所需的罐子,一次一种口味,就像
gradle clean flavorOne
但是如果我这样做,shadowJar任务只运行一次
gradle clean flavorOne flavorTwo
剥离脚本:
sourceSets {
main {
...
resources {
srcDirs = ['src/main/resources', "${project.buildDir}/flavor-res/"]
}
}
}
shadowJar { classifier = 'SNAPSHOT' }
tasks.addRule("Pattern: flavor") { String taskName ->
if (taskName.startsWith("flavor")) {
String flavorName = (taskName - "flavor")
String flavorOutDir = "${project.buildDir}/${flavorName}"
// Set output folder and jar name
task("${taskName}Configure") {
outputs.dir(flavorOutDir)
doFirst {
archivesBaseName = flavorName
project.buildDir = flavorOutDir
}
}
// Copy res to folder used in sourcesets
task("${taskName}CopyResources") {
mustRunAfter = ["${taskName}Configure"]
outputs.dir("${project.buildDir}/flavor-res")
doFirst {
copy {
from "flavors/${flavorName}/"
into "${project.buildDir}/flavor-res/"
}
}
}
shadowJar.mustRunAfter = ["${taskName}Configure", "${taskName}CopyResources"]
// Define task that depends on shadowJar
task(taskName, dependsOn: ["${taskName}Configure", "${taskName}CopyResources",
shadowJar]) {
println "Configuring ${taskName}"
}
}
感觉它可能不起作用,因为变化检测在某种程度上不起作用,我尝试了另一种方法.这是脚本的简化版本
版本2
修改了规则,为每个flavor定义shadowJar动态任务.
/* Removed sourceSets in this version */
shadowJar { classifier = 'SNAPSHOT' }
tasks.addRule("Pattern: flavor") { String taskName ->
if (taskName.startsWith("flavor")) {
String flavorName = (taskName - "flavor")
String flavorOutDir = "${project.buildDir}/${flavorName}"
// Set resources for main sourceset
task("${taskName}Configure") {
outputs.dir(flavorOutDir)
doFirst {
archivesBaseName = flavorName
sourceSets.main.resources.srcDirs = ['src/main/resources', "${flavorOutDir}/flavor-res"]
project.buildDir = flavorOutDir
}
}
task("${taskName}CopyResources") {
outputs.dir("${flavorOutDir}/flavor-res")
dependsOn "${taskName}Configure"
doFirst {
copy {
from "flavors/${flavorName}/"
into "${project.buildDir}/flavor-res/"
}
}
}
// This should shadowJar for each flavor - but generate jars dont have the required artifacts.
task ("${taskName}Build", type: ShadowJar) {
from sourceSets.main.output
configurations = [ configurations.runtime ]
classifier = 'SNAPSHOT'
dependsOn "${taskName}CopyResources"
}
task(taskName, dependsOn: ["${taskName}Build"]) {
}
}
}
但是,现在,生成的罐子格式不正确.第一种味道只是主要的文物,但没有展示罐子.第二个罐子只有清单而没有其他东西.
实现这一目标的正确方法是什么?
PS:不,它不是一个Android应用程序(味道只是一个品牌的同义词).