`

grunt Creating tasks

 
阅读更多

任务alias

If a task list is specified, the new task will be an alias for one or more other tasks. Whenever this "alias task" is run, every specified tasks intaskListwill be run, in the order specified. ThetaskListargument must be an array of tasks.

grunt.registerTask(taskName, [description, ] taskList)

This example alias task defines a "default" task whereby the "jshint", "qunit", "concat" and "uglify" tasks are run automatically if Grunt is executed without specifying any tasks:

grunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']);

Task arguments can be specified as well. In this example, the alias "dist" runs both the "concat" and "min" tasks, each with a "dist" argument:

grunt.registerTask('dist', ['concat:dist', 'uglify:dist']);

多个Target的Task

When a multi task is run, Grunt looks for a property of the same name in the Grunt configuration. Multi-tasks can have multiple configurations, defined using arbitrarily named "targets."

Specifying both a task and target likegrunt concat:fooorgrunt concat:barwill process just the specified target's configuration, while runninggrunt concatwill iterate overalltargets, processing each in turn. Note that if a task has been renamed withgrunt.renameTask, Grunt will look for a property with thenewtask name in the config object.

Most of the contrib tasks, including thegrunt-contrib-jshint plugin jshint task,concat taskandgrunt-contrib-concat plugin concat taskare multi tasks.

grunt.registerMultiTask(taskName, [description, ] taskFunction)

Given the specified configuration, this example multi task would logfoo: 1,2,3if Grunt was run viagrunt log:foo, or it would logbar: hello worldif Grunt was run viagrunt log:bar. If Grunt was run asgrunt loghowever, it would logfoo: 1,2,3thenbar: hello worldthenbaz: false.

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);
});
有多个Target的任务称为Multi Task,可以用grunt task:target的命令来指定启动哪个target,如果就用grunt task的话,将顺序执行所有的target。target存在的目的,主要是为了应用不同的配置项。注意,target并不是maven中goal的概念
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics