兩個grunt-contrib-copygrunt-contrib-clean和,像許多其他咕嚕插件,允許多個Targets在每個Task指定。
對於您的情況,您可以在copy
任務只需配置兩個目標(一個目標複製src
文件夾和另一個目標複製src2
文件夾)。
,您還可以在clean
任務配置兩個目標(一個目標來清潔build
文件夾和其他目標來清潔build2
文件夾)。
Gruntfile.js
你Gruntfile.js
可配置爲驢如下:
module.exports = function(grunt) {
var saveLicense = require('uglify-save-license');
grunt.initConfig({
// The 'clean' task now includes two targets.
// named 'build1' and 'build2'
clean: {
build1: {
src: ['build']
},
build2: {
src: ['build2']
}
},
// The 'copy' task now includes two targets.
// named 'src1' and 'src2'
copy: {
src1: {
cwd: 'src',
src: '**/*',
dest: 'build',
expand: true
},
src2: {
cwd: 'src2',
src: '**/*',
dest: 'build2',
expand: true
}
}
// ...
});
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
// Registering the Targets in the Tasks....
grunt.registerTask('copySrc1', ['copy:src1']);
grunt.registerTask('cleanBuild1', ['clean:build1']);
grunt.registerTask('copySrc2', ['copy:src2']);
grunt.registerTask('cleanBuild2', ['clean:build2']);
grunt.registerTask('copyBoth', ['copy']);
grunt.registerTask('cleanBoth', ['clean']);
};
運行咕嚕任務
你會發現,有六個電話到代碼片尾的.registerTask(...)
函數。即; copySrc1
,cleanBuild1
,copySrc2
, cleanBuild2
,copyBoth
和cleanBoth
。
它們允許您通過命令行運行以下命令:
$ grunt copySrc1
(這將src
文件夾複製到build
文件夾)
$ grunt cleanBuild1
(這將清除build
文件夾)
$ grunt copySrc1
(這將src2
文件夾複製到文件夾build2
)
(這將清除build2
文件夾)
$ grunt copyBoth
(這將src
文件夾複製到build
文件夾和src2
文件夾複製到build2
文件夾)
$ grunt cleanBoth
(這將清除build
和build2
文件夾都)
注意
你可能只需要保持兩個.registerTask(...)
功能如下:
grunt.registerTask('copyBoth', ['copy']);
grunt.registerTask('cleanBoth', ['clean']);
但是,我包含了其他的fo您可以使用分號符號(:
)演示如何在任務內部調用單個目標。例如:
grunt.registerTask('copySrc1', ['copy:src1']);
在上述代碼段中的['copy:src1']
部分簡單地只運行命名src1
的copy
任務內部的目標。
鑑於:
grunt.registerTask('copyBoth', ['copy']);
...不引用任何目標在copy
任務,(即沒有使用分號符號),因此所有目標都將運行。
要進一步瞭解任務,目標,你可以閱讀我對這個post的回答。
希望有幫助!