1 [Grunt homepage](https://github.com/gruntjs/grunt) | [Documentation table of contents](toc.md)
3 **Note: this document is a little out-of-date. You're better off starting with the "Creating Tasks" section of the [API documentation](api.md).**
7 Tasks are grunt's bread and butter. The stuff you do most often, like `concat` or `test`. Every time grunt is run, you specify one more more tasks, which tells grunt what you'd like it to do.
9 _Note: if you don't specify a task, but a task named "default" has been defined, that task will run (unsurprisingly) by default._
11 Tasks can be created in a few ways.
16 grunt.registerTask(taskName, [description, ] taskList);
19 _Note that for alias tasks, the description is optional. If omitted, a useful description will be added for you automatically._
21 In the following example, a `theworks` task is defined that, when invoked by `grunt theworks`, will execute the `lint`, `qunit`, `concat` and `min` tasks in-order. Running `grunt theworks` behaves exactly as if `grunt lint qunit concat min` was run on the command line.
24 grunt.registerTask('theworks', 'lint qunit concat min');
27 In this example, a default task is defined that, when invoked by `grunt` or `grunt default`, will execute the `lint`, `qunit`, `concat` and `min` tasks in-order. It behaves exactly as if `grunt lint qunit concat min` was run on the command line.
30 grunt.registerTask('default', 'lint qunit concat min');
33 _In case it's not obvious, defining a `default` task is helpful because it runs by default, whenever you run `grunt` without explicitly specifying tasks._
36 A multi task is a task that implicitly iterates over all of its targets if no target is specified. For example, in the following, while `grunt lint:test` or `grunt lint:lib` will lint only those specific sets of files, `grunt lint` will automatically run the `test`, `lib` and `grunt` targets for you. It's super convenient.
38 _Note: multi tasks will ignore any config sub-properties beginning with `_` (underscore)._
41 /*global config:true, task:true*/
51 While it's probably most useful for you to check out the JavaScript source of the [built-in tasks](https://github.com/gruntjs/grunt/tree/master/tasks), this example shows how you might define your own multi task:
54 /*global config:true, task:true*/
63 grunt.registerMultiTask('logstuff', 'This task logs stuff.', function() {
64 // this.target === the name of the target
65 // this.data === the target's value in the config object
66 // this.name === the task name
67 // this.args === an array of args specified after the target on the command-line
68 // this.flags === a map of flags specified after the target on the command-line
69 // this.file === file-specific .src and .dest properties
72 grunt.log.writeln(this.target + ': ' + this.data);
74 // If data was falsy, abort!!
75 if (!this.data) { return false; }
76 grunt.log.writeln('Logging stuff succeeded.');
80 Sample grunt output from running `logstuff` targets individually:
84 Running "logstuff:foo" (logstuff) task
86 Logging stuff succeeded.
91 Running "logstuff:bar" (logstuff) task
93 Logging stuff succeeded.
98 Running "logstuff:baz" (logstuff) task
100 <WARN> Task "logstuff:baz" failed. Use --force to continue. </WARN>
102 Aborted due to warnings.
105 Sample grunt output from running `logstuff` task:
109 Running "logstuff:foo" (logstuff) task
111 Logging stuff succeeded.
113 Running "logstuff:bar" (logstuff) task
115 Logging stuff succeeded.
117 Running "logstuff:baz" (logstuff) task
119 <WARN> Task "logstuff:baz" failed. Use --force to continue. </WARN>
121 Aborted due to warnings.
125 You can go crazy with tasks. If your tasks don't follow the "multi task" structure, use a custom task.
128 grunt.registerTask('default', 'My "default" task description.', function() {
129 grunt.log.writeln('Currently running the "default" task.');
133 Inside a task, you can run other tasks.
136 grunt.registerTask('foo', 'My "foo" task.', function() {
137 // Enqueue "bar" and "baz" tasks, to run after "foo" finishes, in-order.
138 grunt.task.run('bar baz');
140 grunt.task.run(['bar', 'baz']);
144 Tasks can be asynchronous.
147 grunt.registerTask('asyncfoo', 'My "asyncfoo" task.', function() {
148 // Force task into async mode and grab a handle to the "done" function.
149 var done = this.async();
150 // Run some sync stuff.
151 grunt.log.writeln('Processing task...');
152 // And some async stuff.
153 setTimeout(function() {
154 grunt.log.writeln('All done!');
160 Tasks can access their own name and arguments.
163 grunt.registerTask('foo', 'My "foo" task.', function(a, b) {
164 grunt.log.writeln(this.name, a, b);
169 // logs: "foo", undefined, undefined
170 // logs: "foo", "bar", undefined
172 // logs: "foo", "bar", "baz"
175 Tasks can fail if any errors were logged.
178 grunt.registerTask('foo', 'My "foo" task.', function() {
179 if (failureOfSomeKind) {
180 grunt.log.error('This is an error message.');
183 // Fail task if errors were logged.
184 if (task.hadErrors()) { return false; }
186 grunt.log.writeln('This is the success message');
190 When tasks fail, all subsequent tasks will be aborted unless `--force` was specified.
193 grunt.registerTask('foo', 'My "foo" task.', function() {
194 // Fail synchronously.
198 grunt.registerTask('bar', 'My "bar" task.', function() {
199 var done = this.async();
200 setTimeout(function() {
201 // Fail asynchronously.
207 Tasks can be dependent on the successful execution of other tasks. Note that `grunt.task.requires` won't actually RUN the other task(s). It'll just check to see that it has run and not failed.
210 grunt.registerTask('foo', 'My "foo" task.', function() {
214 grunt.registerTask('bar', 'My "bar" task.', function() {
215 // Fail task if "foo" task failed or never ran.
216 grunt.task.requires('foo');
217 // This code executes if the "foo" task ran successfully.
218 grunt.log.writeln('Hello, world.');
223 // doesn't log, because foo failed.
225 // doesn't log, because foo never ran.
228 Tasks can fail if required configuration properties don't exist.
231 grunt.registerTask('foo', 'My "foo" task.', function() {
232 // Fail task if "meta.name" config prop is missing.
233 grunt.config.requires('meta.name');
234 // Also fails if "meta.name" config prop is missing.
235 grunt.config.requires(['meta', 'name']);
236 // Log... conditionally.
237 grunt.log.writeln('This will only log if meta.name is defined in the config.');
241 Tasks can access configuration properties.
244 grunt.registerTask('foo', 'My "foo" task.', function() {
245 // Log the property value. Returns null if the property is undefined.
246 grunt.log.writeln('The meta.name property is: ' + grunt.config('meta.name'));
247 // Also logs the property value. Returns null if the property is undefined.
248 grunt.log.writeln('The meta.name property is: ' + grunt.config(['meta', 'name']));
252 Take a look at the [built-in tasks](https://github.com/gruntjs/grunt/tree/master/tasks) for more examples.