用于为复制任务排除创建动态全局列表的Gradle任务顺序

时间:2016-07-20 21:56:31

标签: java gradle groovy build.gradle

好的,我仍然有一个问题,非常感谢有人对我明显缺少的东西有所了解。 我以为我理解了我想要完成的Gradle任务,但我似乎无法最后一点工作。

这是我想要做的,按编号顺序:

  1. 创建动态的全局'copyExcludes'列表;在一项任务中
  2. 复制整个目录树,不包括'copyExcludes'列表;另一项任务
  3. 从复制任务
  4. 对文件树中的每个目录运行maven命令'mvn verify'
  5. 在所有Maven命令成功完成后,为目录树创建ZIP存档。
  6. 下面是代码,我一直在攻击,并且无法获取'copyExcludes'列表,更新,在'copy'任务执行之前,因此所有模块(子目录)被复制,而不仅仅是少数我想要。

    /**
     * Copy all files to ${assemblyDir}, then run 'mvn verify' on each and every included module.
     * Then create an assembly ZIP
     */
    def copyFromDir = rootProject.file('.')
    
    def assemblyDir = "target/gradle_assembly"
    def assemblyDestination = "target/assembly"
    def assemblyName = "assembly.zip"
    
    def List includedModules = []
    
    //---------------------------------------------------------------------------//
    def allModules = [
            'common',
            'spring-hello-world',
            'spring-configuration',
            'spring-advanced-configuration',
            'spring-jdbc',
    
            'spring-jms',
            'spring-batch',
            'spring-batch-configuration'
    ]
    
    def copyExcludes = []
    
    //---------------------------------------------------------------------------//
    
    /**
     * Copy all files from root (./)
     but exclude the module sub directories that are from 'getCopyExcludes(allModules)'
     */
    task copyFiles(type: Copy) {
    
        def cpExcludes = []
    
        ext {
            println "********** copyFiles ext{} **********"
            println "1 ********** (${copyExcludes.size()}) copyExcludes: '${copyExcludes}' **********"
            cpExcludes = copyExcludes
            cpExcludes = getCopyExcludes(allModules)
        }
    
        doFirst {
            println "********** copyFiles doFirst{} **********"
            println "2 ********** (${copyExcludes.size()}) copyExcludes: '${copyExcludes}' **********"
            cpExcludes = copyExcludes
            cpExcludes = getCopyExcludes(allModules)
            println "********** (${cpExcludes.size()}) cpExcludes: '${cpExcludes}' **********"
    
        }
    
    //    doLast{
    
            if (file(buildDir).exists()) {
                delete "./target"
            }
    
            from("${copyFromDir}") {
                into '.'
    
                include '*'
                include '**/*' //to include contents of a folder present inside Reports directory
                include '**/*.*' //to include contents of a folder present inside Reports directory
    
                exclude (getCopyExcludes(allModules))
    
            }
            into {
                def outputDir = assemblyDir;
                outputDir
            }
    //    }
    }
    
    
    
    //---------------------------------------------------------------------------//
    
    //task mavenVerify() {
    task mavenVerify(dependsOn:copyFiles) {
    
        println "\n\n********** mavenVerify **********\n"
    
        def moduleDir = "${assemblyDir}/code"
    
        doLast {
    
            // For each module that is to be included,
            allModules.each { module ->
    
            if (file("${assemblyDir}/code/${module}").exists()) {
                moduleDir = "${assemblyDir}/code/${module}"
    
                exec {
                    args "-D${mavenRepo} -D${mavenOpt}"
                    workingDir "${moduleDir}"
    
                    def command = commandLine 'mvn', 'verify'
                }
                println "---------->  END ${moduleDir} ----------\n"
            }
    
            }
        }
        return "success"
    }
    
    
    //---------------------------------------------------------------------------//
    
    /**
     * Create a ZIP file for the previoulsy validate code copied and verified.
     */
    task assembleZip(type: Zip, dependsOn:mavenVerify){
    
        println "********** assembleZip **********"
    
        from "${assemblyDir}"
        include '*'
        include '**/*' //to include contents of a folder present inside Reports directory
        include '**/*.*' //to include contents of a folder present inside Reports directory
        archiveName "${assemblyName}"
        destinationDir(file("${assemblyDestination}"))
    
    }
    //---------------------------------------------------------------------------//
    
    
    task basic_jdbc_aop_mvc_rest << {
    
        println "********** basic_jdbc_aop_mvc_rest **********"
    
        def modulesToKeep = []
    
    //    doFirst{
        ext {
            // Create List
            modulesToKeep.addAll(modules_basic())
            modulesToKeep.addAll(modules_jdbc())
    
    //        println "********** (${modulesToKeep.size}) modulesToKeep: '${modulesToKeep}' **********"
    
    //        allModules.removeAll(modulesToKeep)
    //         includedModules = allModules
    //        println "********** (${includedModules.size()}) allModules (pre): '${includedModules/**/}' **********"
    //        copyExcludes = getCopyExcludes(allModules)
    //        println "********** (${copyExcludes.size()}) copyExcludes: '${copyExcludes}' **********"
        }
    
    //    doFirst{
    //        println "********** (${allModules.size()}) allModules (pre): '${allModules}' **********"
    //        println "********** (${modulesToKeep.size}) modulesToKeep: '${modulesToKeep}' **********"
    
    //        includedModules = allModules.minus(modulesToKeep)
            allModules.removeAll(modulesToKeep)
             includedModules = allModules
            println "********** (${includedModules.size()}) allModules (pre): '${includedModules/**/}' **********"
            copyExcludes = getCopyExcludes(allModules)
    //        println "********** (${copyExcludes.size()}) copyExcludes: '${copyExcludes}' **********"
    //    }
    
    }
    
    
    def List getCopyExcludes(mtr) {
        def newList = mtr.collect{ "**/${it}" }
    
        newList.addAll('**/target',
                        '**/.git',
                        '**/*.iml',
                        '**/.idea',
                        '**/gradle', // #5
                        '**/.gradle',
        )
    //    newList.addAll(modulesToRemoveExclude(mtr))
        println "********** getCopyExcludes newList (${newList.size}): '${newList}' **********"
        return newList
    }
    
    
    [copyFiles]*.shouldRunAfter basic_jdbc_aop_mvc_rest
    
    
    //---------------------------------------------------------------------------//
    def modules_basic(){
        return [
                // Basic
                'common',
                'spring-hello-world',
                'spring-configuration',
                'spring-advanced-configuration'
        ]
    
    }
    
    def modules_jdbc(){
        return [
                // JDBC
                'spring-jdbc'
        ]
    
    }
    
    
    //--- THE END ---------------------------------------------------------------//
    

    在这里输入代码

1 个答案:

答案 0 :(得分:0)

我认为您可能会对所有调试语句感到困惑。也许将其降低到最低限度可以帮助您更好地发现问题?

例如,很难看到问题领域如&#34;进入。&#34;在一个from子句和一个看起来奇怪的&#34; def List getCopyExcludes(mtr)&#34;功能声明。

例如,这是一个类似的小脚本,可以执行您希望它执行的操作。

def copyFromDir = "source/gradle_assembly"
def assemblyDir = "target/gradle_assembly"

task copyFiles(type: Copy) {
    from "${copyFromDir}" 
    into "${assemblyDir}"
    exclude getCopyExcludes()
}

def getCopyExcludes() {
    // exclude these 2 log files
    return ["tomcat.log", "cargo.log"]
}

也许从这里开始,你可以通过一次一个地添加每个任务,同时验证它仍然有效,将它扩展到当前版本?如果它破坏了某些东西,你就会知道它是什么造成的。