问题
I am trying to link-up my cordova build with grunt, there are a tasks that we run when serving the project locally that I would like to reuse during our Cordova build. For this I create a simple before_build hook and placed it in hooks/before_build.
My build hook looks like:
#!/usr/bin/env node
var grunt = require('grunt');
grunt.tasks(['build']);
process.exit(0);
The initial part of the build works well, all of the files are copied as expected. However, within my build
task, I make several additional calls such as:
grunt.task.run('wiredep');
This, and all other similar calls, fail to execute without an error or warning.
Why would I not be able to call additional grunt tasks?
All of my tasks are defined in the Gruntfile using either loadNpmTask
or registerTask
, is this an incorrect configuration?
回答1:
Well... grunt.task.run(...)
simply adds a task to Grunt's "queue" (of sorts), it does not immediately run that Grunt task. I think you may want to actually set your build
task as the default in your Gruntfile.js
:
grunt.registerTask('default', ['build']);
Then just use a bash script (versus a Node shell script):
#!/bin/bash
grunt
Note that you will need to have the grunt-cli
installed first:
~$ nmp install -g grunt-cli
回答2:
I received some feedback on the Grunt issues board:
https://github.com/gruntjs/grunt/issues/1265
Any time I need to call one of my Grunt tasks, I add a hook_task.sh file with the following:
#!/bin/sh
grunt build
This works on Windows/Linux/Mac without any issues.
I would still like to know why the node approach was not working. Part of me feels much more comfortable using that approach, but this is working well so no complaints.
(Note: My Gruntfile.js is in the root of my Cordova App)
来源:https://stackoverflow.com/questions/27478566/cordova-build-hooks-grunt