Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为每种风格的应用程序创建多个Shadowjar_Java_Gradle_Jar_Build.gradle_Shadowjar - Fatal编程技术网

Java 为每种风格的应用程序创建多个Shadowjar

Java 为每种风格的应用程序创建多个Shadowjar,java,gradle,jar,build.gradle,shadowjar,Java,Gradle,Jar,Build.gradle,Shadowjar,我正在用gradle进行实验,并试图建立一个系统,该系统可以构建不同风格(品牌)的应用程序,这些应用程序主要因配置而异。到目前为止,我有两个版本的构建脚本-都不工作 版本1 第一个特定于香料的资源文件夹flavor res被添加到sourceset,从而实现覆盖一些默认资源。任务规则为每种风格定义任务,这(理想情况下)应该触发整个jar的构建 这可以很好地工作并生成所需的罐子,一次只提供一种口味,如 gradle clean flavorOne 但是shadowJar任务只运行一次,如果我这

我正在用gradle进行实验,并试图建立一个系统,该系统可以构建不同风格(品牌)的应用程序,这些应用程序主要因配置而异。到目前为止,我有两个版本的构建脚本-都不工作

版本1
第一个特定于香料的资源文件夹
flavor res
被添加到
sourceset
,从而实现覆盖一些默认资源。任务规则为每种风格定义任务,这(理想情况下)应该触发整个jar的构建

这可以很好地工作并生成所需的罐子,一次只提供一种口味,如

gradle clean flavorOne 
但是
shadowJar
任务只运行一次,如果我这样做的话

gradle clean flavorOne flavorTwo
精简脚本:

但是,现在生成的JAR格式不正确。第一种口味只获得main的人工制品,但没有显示罐子。第二个jar只有清单,没有其他内容

实现这一目标的正确方法是什么


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

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

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

然后我想出了以下脚本:


    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) {}
    }


导入com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
插件{
id‘java’
id“com.github.johnrengelman.shadow”版本“2.0.4”
}
组“您的组”
版本“开发人员快照”
sourceCompatibility=1.8
存储库{
mavenCentral()
}
依赖关系{
//示例依赖项
编译组:'com.google.guava',名称:'guava',版本:'19.0'
}
tasks.addRule(“Pattern:flavor”){def taskName->
如果(!taskName.startsWith(“flavor”)){
返回
}
def FAVORLNAME=任务名-“风味”
//定义影子任务
def shadowTask=task(“${flavorName}ShadowJar”,类型:ShadowJar){
分类器=名称
//添加我们的风味资源,首先确定这些资源的优先级
来自文件(“src/main/flavors/${flavorName}”)
//包括我们的项目类
来自project.sourceset.main.output
//不要包含重复的资源,只包括第一个添加的资源,在
//在这种情况下,调味资源将覆盖默认资源
duplicatesStrategy=duplicatesStrategy.EXCLUDE
//一些默认值取自默认的shadowJar任务
// https://github.com/johnrengelman/shadow/blob/master/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy#L48
配置=[project.configurations.runtime]
manifest.inheritFrom project.tasks.jar.manifest
排除('META-INF/INDEX.LIST'、'META-INF/*.SF'、'META-INF/*.DSA'、'META-INF/*.RSA')
}
//定义风味任务
任务(“${taskName}”,dependsOn:shadowTask){}
}

构建了多个目标,但只有第一个目标可以包含flavor文件夹中的资源。所有其他目标不会复制/覆盖任何资源。一味一味二味。最后,我需要能够做一些事情,包括修改源文件和其他每种口味的东西。我不明白为什么第一个目标总是能工作,但后面的目标却不能,除非它可能与检测目标何时更新有关。
/* 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"]) {
    }
}
 }

    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) {}
    }