【问题标题】:Gradle: Exclude distinct resource files in assemblyGradle:在程序集中排除不同的资源文件
【发布时间】:2015-10-26 13:56:24
【问题描述】:

Gradle 2.5

我有几个资源包文件。其中一些我想从打包到目标 jar 文件中排除,因为它们不包含尚未授权的翻译。

根据我发现的文档和示例,我有这个指令:

sourceSets {
   main {
        resources {
            exclude 'mail/mailtext_it.properties'
        }
    }
}

并通过打印出主要资源的文件集来断言该指令被正确解释:

processResources 
{
    afterEvaluate 
    {
        println "resource files (in processResources): " + sourceSets['main'].resources.getFiles()
    }
}

运行“gradle clean assembly”时,资源集的打印结果确实是我所期望的(排除的文件未在其中列出),但 build/resource/ 文件夹仍然包含文件,并且它们存在于目标罐子。

我错过了什么?哪个任务负责将资源文件复制到构建目录,基于哪个源集?

【问题讨论】:

  • 只是猜测,我想从源集中排除文件只会使它们在处理资源阶段不被处理。将它们从存档中排除。如果需要从 jar 文件中排除它们,则需要在 jar 块内配置 exclude 块。 Here 是有用的文档。
  • 到目前为止我的理解是:processResources 将文件复制(和过滤)到构建目录(因为 compile 任务将源文件编译到构建目录中),jar 任务拿起准备好的工件(编译,过滤)从构建目录并将它们打包到一个 jar 中。这张图有错吗?
  • 它按照你写的那样工作。不知道为什么脚本无法正确排除文件 - demo lollol3 文件应该包含在最终的 jar 中。
  • 我真的很讨厌这种配置(使用排除)。与其将所有内容都转储到src/main/resources 并使用excludes,不如将​​它们放入src/unauthorized/resources 并在需要时添加它们?恕我直言,要记住的魔法要少得多
  • @Opal:非常感谢您的努力。您的演示按预期工作 - 当然;-)。不幸的是,我的项目设置要复杂得多(多模块项目、本身排除的资源过滤等)。明天我们将变得富有成效,所以我没有时间再解决这个问题了。我会从后备箱中删除相关的属性文件然后去。

标签: gradle source-sets


【解决方案1】:

我发现了问题:processResources 中的过滤器指令是罪魁祸首:

processResources 
{
    // Note: narrow the file set to be filtered. Otherwise gradle would change the jks file
    // when copying to output folder !!!
    from('src/main/resources') {
        include '**/*.properties'
        filter ( org.apache.tools.ant.filters.ReplaceTokens, 
             tokens: [
                        "filtervar.walletservice.productVersion": project.property("filtervar.walletservice.productVersion"),
                     ]  
            )    
    }
}

显然 from(...) 子句会覆盖 sourceSets 定义的定义。我原以为它会是累积的。

我通过定义 from(..) 来解决它:

from( sourceSets['main'].resources ) {
    include '**/*.properties'
    ...

【讨论】:

    猜你喜欢
    • 2016-01-13
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多