每当运行Grunt时, 你能够为其指定一个或多个任务, 这些任务用于告诉Grunt你想要它作什么事情。数组
若是你没有指定一个任务,而且你已经定义一个名为 "default" 的任务,那么该任务将会默认被执行。异步
若是指定了一个任务列表taskList,新任务名taskName将是这一个或多个指定任务的别名。当运行此 "任务别名" 时,在taskList
中指定的每一个任务都会按照其出现的顺序依次执行。taskList
参数必须是一个任务数组。grunt.registerTask(taskName, taskList)async
举个例子:函数
grunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']);grunt
上面的任务别名案例中定义了一个 'default' 任务,若是运行Grunt时没有指定任何任务,它将自动执行'jshint'、'qunit'、'concat' 和 'uglify' 任务。ui
还能够给任务指定参数。在下面的例子中,别名 "dist" 将执行 "concat" 和 "uglify" 两个任务,而且它们都带有一个 "dist" 参数:this
grunt.registerTask('dist', ['concat:dist', 'uglify:dist']);编码
当运行一个多任务时,Grunt会自动从项目的配置对象中查找同名属性。多任务能够有多个配置,而且可使用任意命名的'targets'。举个例子:spa
grunt.initConfig({ log: { foo: [1, 2, 3], bar: 'hello world', baz: false } }); grunt.registerMultiTask('log', 'Log stuff.', function() { grunt.log.writeln(this.target + ': ' + this.data); });
若是经过grunt log:foo运行Grunt,它会输出foo: 1,2,3;若是经过grunt log:bar来运行Grunt, 它会输出bar: hello world。然而若是经过grunt log运行Grunt, 它会输出foo: 1,2,3,而后是bar: hello world,最后是baz: false(任务目标会按照指定的顺序进行处理)。日志
当一个基本任务执行时,Grunt并不会检查配置和环境 -- 它仅仅执行指定的任务函数,并传递任何使用冒号分割的参数做为函数的参数。举个例子:
grunt.registerTask('foo', 'A sample task that logs stuff.', function(arg1, arg2) { if (arguments.length === 0) { grunt.log.writeln(this.name + ", no args"); } else { grunt.log.writeln(this.name + ", " + arg1 + " " + arg2); } });
若是执行 grunt foo:testing:123
,将输出日志 foo, testing 123
。 若是执行这个任务时不传递参数,只是执行 grunt foo
,那么将输出日志 foo, no args
。
若是你的任务没有遵循 "多任务" 结构,那就可使用自定义任务。好比:
grunt.registerTask('default', 'My "default" task description.', function() { grunt.log.writeln('Currently running the "default" task.'); });
在一个任务内部,你能够执行其余的任务。好比:
grunt.registerTask('foo', 'My "foo" task.', function() { // Enqueue "bar" and "baz" tasks, to run after "foo" finishes, in-order. grunt.task.run('bar', 'baz'); // Or: grunt.task.run(['bar', 'baz']); });
任务也能够是异步的。好比:
grunt.registerTask('asyncfoo', 'My "asyncfoo" task.', function() { // Force task into async mode and grab a handle to the "done" function. var done = this.async(); // Run some sync stuff. grunt.log.writeln('Processing task...'); // And some async stuff. setTimeout(function() { grunt.log.writeln('All done!'); done(); }, 1000); });
任务也能够访问它们自身名称和参数。好比:
grunt.registerTask('foo', 'My "foo" task.', function(a, b) { grunt.log.writeln(this.name, a, b); }); // 用法: // grunt foo // logs: "foo", undefined, undefined // grunt foo:bar // logs: "foo", "bar", undefined // grunt foo:bar:baz // logs: "foo", "bar", "baz"
若是任务在执行时记录到任何错误,那么任务就会失败。当任务失败时,全部的后续任务都将终止。好比:
grunt.registerTask('foo', 'My "foo" task.', function() { return false; }); grunt.registerTask('bar', 'My "bar" task.', function() { // 若是"foo"任务运行失败或者没有运行则任务失败。 grunt.task.requires('foo'); // 若是"foo"任务运行成功则执行这里的代码。 grunt.log.writeln('Hello, world.'); }); // 用法: // grunt foo bar // 没有输出,bar任务没有运行,由于"foo"失败,因此它后面的任务都将终止运行。 // grunt bar // 没有输出,bar任务运行,但运动到一半就中止了,由于"foo"从未运行,因此foo任务失败,失败后,bar任务也就中止了,所以没有输出。
注意 grunt.task.requires 并不会真正的运行其余任务,它仅仅检查其它任务是否已经执行,而且没有失败。
任务还能够访问配置属性。好比:
grunt.registerTask('foo', 'My "foo" task.', function() { // 记录属性值,若是属性未定义(undefined)则返回null。 grunt.log.writeln('The meta.name property is: ' + grunt.config('meta.name')); // 一样的记录属性值,若是属性未定义(undefined)则返回null。 grunt.log.writeln('The meta.name property is: ' + grunt.config(['meta', 'name'])); });
若是任务须要的配置属性不存在,其也会失败。好比:
grunt.registerTask('foo', 'My "foo" task.', function() { // 若是缺乏"meta.name"配置属性则任务失败。 grunt.config.requires('meta.name'); // 若是缺乏"mata.name"配置属性则任务一样失败。 grunt.config.requires(['meta', 'name']); // 根据状况记录日志。 grunt.log.writeln('This will only log if meta.name is defined in the config.'); });
为何个人异步task没有完成?
若是发生这种状况,多是因为你忘记调用 this.async 方法来告诉Grunt你的任务是异步的。为了简单起见,Grunt使用同步的编码风格,能够在task体中经过调用 this.async() 将其转换为异步的。
注意,传递 false 给 done() 函数就会告诉Grunt你的任务已经失败。
例如:
grunt.registerTask('asyncme', 'My asynchronous task.', function() { var done = this.async(); setTimeout(function() {
done(false); //表明asyncme任务失败
}, 1000); });
加油!