Skip to content

Instantly share code, notes, and snippets.

@jonathandixon
Last active January 5, 2021 22:00
Show Gist options
  • Save jonathandixon/7418730 to your computer and use it in GitHub Desktop.
Save jonathandixon/7418730 to your computer and use it in GitHub Desktop.
Using Grunt with a Cordova 3 project.

Grunt and Cordova 3

The advantages of using Grunt with Cordova:

  1. It simplifies working with the cordova cli.
  2. It provides a mechanism to copy platform customization to the platforms directory without having to commit the generated plugins and platforms directories to version control.
  3. It provides a way to watch resources and automatically run cordova commands.

Stack Overflow: .gitignore for PhoneGap/Cordova 3.0 projects - what should I commit?

module.exports = function(grunt) {
grunt.initConfig({
app: {
name: 'App'
},
clean: {
plugins: ['plugins'],
platforms: ['platforms']
},
mkdir: {
'default': {
options: {
create: ['plugins', 'platforms']
}
}
},
copy: {
platform_merges: {
expand: true,
dest: './platforms/',
cwd: 'platform-merges',
src: '**'
},
resources_ios: {
files: [
{src: ['www/res/icon/ios/icon-57.png'], dest: 'platforms/ios/<%= app.name %>/Resources/icons/icon.png'},
{src: ['www/res/icon/ios/icon-57-2x.png'], dest: 'platforms/ios/<%= app.name %>/Resources/icons/[email protected]'},
{src: ['www/res/icon/ios/icon-72.png'], dest: 'platforms/ios/<%= app.name %>/Resources/icons/icon-72.png'},
{src: ['www/res/icon/ios/icon-72-2x.png'], dest: 'platforms/ios/<%= app.name %>/Resources/icons/[email protected]'},
{src: ['www/res/screen/ios/screen-iphone-portrait.png'], dest: 'platforms/ios/<%= app.name %>/Resources/splash/Default~iphone.png'},
{src: ['www/res/screen/ios/screen-iphone-portrait-2x.png'], dest: 'platforms/ios/<%= app.name %>/Resources/splash/Default@2x~iphone.png'},
{src: ['www/res/screen/ios/screen-iphone-portrait-568h-2x.png'], dest: 'platforms/ios/<%= app.name %>/Resources/splash/Default-568h@2x~iphone.png'},
{src: ['www/res/screen/ios/screen-ipad-portrait.png'], dest: 'platforms/ios/<%= app.name %>/Resources/splash/Default-Portrait~ipad.png'},
{src: ['www/res/screen/ios/screen-ipad-portrait-2x.png'], dest: 'platforms/ios/<%= app.name %>/Resources/splash/Default-Portrait@2x~ipad.png'},
{src: ['www/res/screen/ios/screen-ipad-landscape.png'], dest: 'platforms/ios/<%= app.name %>/Resources/splash/Default-Landscape~ipad.png'},
{src: ['www/res/screen/ios/screen-ipad-landscape-2x.png'], dest: 'platforms/ios/<%= app.name %>/Resources/splash/Default-Landscape@2x~ipad.png'}
]
},
resources_android: {
files: [
{src: ['www/res/icon/android/icon-36-ldpi.png'], dest: 'platforms/android/res/drawable-ldpi/icon.png'},
{src: ['www/res/icon/android/icon-48-mdpi.png'], dest: 'platforms/android/res/drawable-mdpi/icon.png'},
{src: ['www/res/icon/android/icon-72-hdpi.png'], dest: 'platforms/android/res/drawable-hdpi/icon.png'},
{src: ['www/res/icon/android/icon-96-xhdpi.png'], dest: 'platforms/android/res/drawable-xhdpi/icon.png'},
{src: ['www/res/icon/android/icon-96-xhdpi.png'], dest: 'platforms/android/res/drawable/icon.png'},
{src: ['www/res/screen/android/screen-ldpi-portrait.png'], dest: 'platforms/android/res/drawable-ldpi/screen.png'},
{src: ['www/res/screen/android/screen-mdpi-portrait.png'], dest: 'platforms/android/res/drawable-mdpi/screen.png'},
{src: ['www/res/screen/android/screen-hdpi-portrait.png'], dest: 'platforms/android/res/drawable-hdpi/screen.png'},
{src: ['www/res/screen/android/screen-xhdpi-portrait.png'], dest: 'platforms/android/res/drawable-xhdpi/screen.png'},
{src: ['www/res/screen/android/screen-xhdpi-portrait.png'], dest: 'platforms/android/res/drawable/screen.png'}
]
}
},
cordovacli: {
options: {
path: './'
},
add_platforms: {
options: {
command: 'platform',
action: 'add',
platforms: ['ios', 'android']
}
},
add_plugins: {
options: {
command: 'plugin',
action: 'add',
plugins: [
'console',
'device',
'geolocation',
'network-information',
'splashscreen',
'https://github.com/phonegap-build/PushPlugin.git'
]
}
},
build_ios: {
options: {
command: 'build',
platforms: ['ios']
}
},
build_android: {
options: {
command: 'build',
platforms: ['android']
}
},
prepare_ios: {
options: {
command: 'prepare',
platforms: ['ios']
}
},
prepare_android: {
options: {
command: 'prepare',
platforms: ['android']
}
},
serve: {
options: {
command: 'serve',
port: 7000
}
}
},
watch: {
src: {
files: ['www/**/*.*', 'platform-merges/**/*.*'],
tasks: ['update']
}
},
connect: {
server: {
options: {
port: 7000,
hostname: 'localhost',
base: 'www',
keepalive: true
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-mkdir');
grunt.loadNpmTasks('grunt-cordovacli');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.registerTask('init', 'Initialize the development environment.',[
'clean',
'mkdir',
'cordovacli:add_platforms',
'cordovacli:add_plugins',
'update'
]);
grunt.registerTask('update', 'Update platforms.', [
'cordovacli:prepare_ios',
'cordovacli:prepare_android',
'copy'
]);
grunt.registerTask('build', 'Build Platforms.', [
'cordovacli:build_ios',
'cordovacli:build_android',
]);
grunt.registerTask('server', ['connect:server']);
grunt.registerTask('cordova-serve', "Alias for 'cordova serve'.", ['cordovacli:serve']);
grunt.registerTask('default', ['watch']);
};

Copyright (c) 2015 Jonathan Dixon.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

{
"name": "app",
"version": "1.0.0",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-clean": "~0.5.0",
"grunt-contrib-copy": "~0.4.1",
"grunt-contrib-watch": "~0.5.3",
"grunt-mkdir": "~0.1.1",
"grunt-cordovacli": "~0.2.1",
"grunt-contrib-connect": "~0.5.0"
}
}
@arun99178
Copy link

Running "cordovacli:add_platforms" (cordovacli) task
Warning: Cannot find module 'cordova' Use --force to continue.

Aborted due to warnings.
Ak-Mac-Pro:wegentum-App arun$ grunt init
Running "cordovacli:add_platforms" (cordovacli) task
Warning: Cannot find module 'cordova' Use --force to continue.

How to solve it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment