Groovy Gradle将参数传递给插件任务

Groovy Gradle将参数传递给插件任务,groovy,gradle,Groovy,Gradle,我的任务来自: 我的问题是,我必须多次更改sourceJs和destDirJs的值。所以我不想有太多重复的代码 所以我试着这样做: task gzipAll { sourceJs = ["WebContent/plugin/bootstrap-modal/js/bootstrap-modalmanager.js", "WebContent/plugin/bootstrap-modal/js/bootstrap-modal.js", "WebContent/js/bootstrap-dro

我的任务来自:

我的问题是,我必须多次更改sourceJs和destDirJs的值。所以我不想有太多重复的代码

所以我试着这样做:

task gzipAll {
    sourceJs = ["WebContent/plugin/bootstrap-modal/js/bootstrap-modalmanager.js", "WebContent/plugin/bootstrap-modal/js/bootstrap-modal.js", "WebContent/js/bootstrap-dropdown.js", "WebContent/js/mandatory/bootstrap-analytics-setup.js"]         
    destDirJs = "WebContent/js/mandatory"
    tasks.combineJs().execute;
    tasks.minifyJs().execute;
    tasks.gzipJs().execute;

    //Here I need to change sourceJs and destDir and call all the task again.
}
事实上,我在黑暗中进行了一次远射,我得到了一个例外:

Caused by: groovy.lang.MissingPropertyException: Could not find property 'sourceJs' on task ':combineJs'.
我也试过类似的东西,但没用。有一些例外:

combineJs(sourceJs, destDirJs) {
    source = $sourceJs
    dest = file($destDirJs + "/all.js")
}
有经验的人能帮我解决这个问题吗?这有点简单,对吧?
谢谢。

我还没有测试过它,但是由于gradle脚本是groovy脚本,所以您应该能够执行类似的操作


['dir1', 'dir2'].eachWithIndex { dir, index ->
    task "combineJs_$index"(type: combineJs) {
        source = dir
        dest = file(destDirJs + "/$index.js")
    }

    task "minifyJs_$index" (type: minifyJs) {
        source = "combineJs_$index"
        dest = file( destDirJs + "/$index-min.js")
        closure {
            warningLevel = 'QUIET'
        }
    }

    task "gzipJs_$index" (type: gzipJs) {
        source = "minifyJs_$index"
        dest = file(destDirJs + "/$index-gzip-min.js")
    }
}

当然,您需要正确的任务类型,并且您可能需要更改输入,但这应该可以让您继续。

为什么您必须多次更改这些值?因为我不能让1个JS与所有JS一起使用。我有一个缩小和压缩的强制性JS(所有页面使用)和一个特定的每一页。因此,我将对每个页面进行2次缩小和压缩JS。

['dir1', 'dir2'].eachWithIndex { dir, index ->
    task "combineJs_$index"(type: combineJs) {
        source = dir
        dest = file(destDirJs + "/$index.js")
    }

    task "minifyJs_$index" (type: minifyJs) {
        source = "combineJs_$index"
        dest = file( destDirJs + "/$index-min.js")
        closure {
            warningLevel = 'QUIET'
        }
    }

    task "gzipJs_$index" (type: gzipJs) {
        source = "minifyJs_$index"
        dest = file(destDirJs + "/$index-gzip-min.js")
    }
}