代码之家  ›  专栏  ›  技术社区  ›  Hervian

Gradle,过滤资源一种方法是测试,另一种方法是打包工件

  •  0
  • Hervian  · 技术社区  · 6 年前

    我有一个gradle项目,有一些数据库迁移脚本。

    迁移文件有一个变量,我用它替换它 resource filtering . 这项工作很好,完成方式如下:

    processResources {
        filter ReplaceTokens, tokens: [
            "index.refresh.period": project.property("index.refresh.period")    
        ]
    }
    

    现在我已经编写了一个单元测试,它启动一个嵌入式数据库并应用相同的迁移脚本。但是,对于测试,我需要替换另一个值。 我怎样才能做到这一点?

    我试过很多不同的方法,但似乎都不管用。我应该提到,我是格拉德尔的新手,所以这是一个尝试和错误的方法。

    尝试#1:

    test {
        project.ext.setProperty('index.refresh.period', '1')
        processResources
    }
    

    尝试#2:

    compileTestJava {
        project.ext.setProperty('index.refresh.period', '1')
        processResources
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Hervian    6 年前

    最后,我将迁移脚本复制粘贴到output test resources文件夹,并作为复制操作的一部分进行筛选。

    请注意,在测试期间,测试文件夹中的资源优先。这意味着使用这些文件的逻辑(在我的用例中是数据库迁移工具)不需要任何特定于测试的配置,因为下面的更改-测试资源文件夹中的db迁移脚本只是优先。

    processResources {
        filter ReplaceTokens, tokens: [
            "index.refresh.period": project.property("index.refresh.period")    
        ]
    }
    
    task copyFiles(type: Copy) {
        println "Copying db migration scrips to test resources (in order to set shortest possible index refresh period)"
        from 'src/main/resources/db/migration'
        into "${buildDir}/resources/test/db/migration"
        filter ReplaceTokens, tokens: [
            "index.refresh.period": '1'    
        ]
    }
    
    processTestResources {
        dependsOn copyFiles
    }
    

    index.refresh.period '在我的 gradle.properties公司

    index.refresh.period=60