我正在尝试gradle并尝试建立一个构建应用程序不同风格(品牌)的系统,该系统主要因配置而异。到目前为止,我有两个版本的构建脚本-都无法正常工作。

版本1
将第一个特定于 flavor 的资源文件夹flavor-res添加到sourcesets,它实现了覆盖某些默认资源的功能。任务规则为每种口味定义任务,(理想情况下)应触发整个 jar 的构建。

这样可以很好地工作,并一次生成一种口味的所需 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<Name>") { 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<Name>") { 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"]) {
    }
}
 }

但是,现在,生成的jar格式不正确。第一种口味仅获得主要的人工制品,但没有显示出 jar 。第二个 jar 只有 list ,没有其他东西。

实现该目标的正确方法是什么?

PS:不,它不是android应用程序( flavor 只是品牌的同义词)。

最佳答案

我决定重新创建一个 flavor 构建脚本,因为它可以简化为您现在所拥有的。 ShadowJar任务本身可以处理所有类和资源的复制,无需定义单独的类和资源。我还采用了一些默认配置,该配置将应用于shadowJar任务,并将其应用于自定义ShadowJar任务以获得相同的行为。

我首先建立一个快速测试项目结构,可以在这里找到:
Test Structure

然后我想出了以下脚本:


    import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

    plugins {
        id 'java'
        id "com.github.johnrengelman.shadow" version "2.0.4"
    }

    group 'your-group'
    version 'dev-SNAPSHOT'

    sourceCompatibility = 1.8

    repositories {
        mavenCentral()
    }

    dependencies {
        // Example dependency
        compile group: 'com.google.guava', name: 'guava', version: '19.0'
    }

    tasks.addRule("Pattern: flavor<Name>") { def taskName ->
        if (!taskName.startsWith("flavor")) {
            return
        }

        def flavorName = taskName - "flavor"
        // Define the shadow task
        def shadowTask = task ("${flavorName}ShadowJar", type: ShadowJar) {
            classifier = flavorName
            // Add our flavor resources, first to prioritize these resources
            from file("src/main/flavors/${flavorName}")
            // Include our project classes
            from project.sourceSets.main.output
            // Don't include duplicate resources, only the first ones added, in
            // this case the flavored resources will override the default ones
            duplicatesStrategy = DuplicatesStrategy.EXCLUDE
            // Some defaults taken from the default shadowJar task
            // https://github.com/johnrengelman/shadow/blob/master/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy#L48
            configurations = [ project.configurations.runtime ]
            manifest.inheritFrom project.tasks.jar.manifest
            exclude('META-INF/INDEX.LIST', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA')
        }

        // Define the flavor task
        task ("${taskName}", dependsOn: shadowTask) {}
    }

10-03 00:29