Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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/icon@2x.png'},
{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/icon-72@2x.png'},
{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"
}
}
@jernejcic

This comment has been minimized.

Copy link

commented Nov 14, 2013

You are missing a comma before "clean" in the Gruntfile.

@kylemacey

This comment has been minimized.

Copy link

commented Jan 13, 2014

So why not use the merges directory instead of manually copying in platform-specific resources?

@jonathandixon

This comment has been minimized.

Copy link
Owner Author

commented Jan 16, 2014

The merges directory only applies to the www directory. In other words it only allows copying of resources to the www directory for each platform.

The platform-merges directory (not managed by cordova) is to platforms/ what merges is to www.

I suppose the app icons and splash screen images could have been stored in the appropriate directories under platform-merges. But in my case the project was initially created with PhoneGap 2.x and I believe those files were placed in www/res to support PhoneGap Build.

@yesyo

This comment has been minimized.

Copy link

commented Feb 19, 2014

You forgot about one line, grunt.loadNpmTasks('grunt-contrib-connect');, nice work, btw.

@jonathandixon

This comment has been minimized.

Copy link
Owner Author

commented Feb 20, 2014

Thanks.

@paynecodes

This comment has been minimized.

Copy link

commented Mar 14, 2014

How do you handle options changed in XCode such as Depoloyment Target, Devices, and Device Orientation? Is there some way I can configure these using the platform_merges folder?

@jonathandixon

This comment has been minimized.

Copy link
Owner Author

commented Mar 17, 2014

That was the intended purpose of platform-merges. This directory should follow the same structure as platforms, but only contains customizations that need to be version controlled. The contents of the platform-merges directory will be copied to platforms, replacing any Cordova generated content. For example to customize the plist file for iOS, make the changes in xcode (or in any editor) and copy the file to platform-merges/ios/App/App-Info.plist.

Be aware that any file in platform-merges will clobber its counterpart in platforms when any grunt command that calls copy:platform-merges is run. There is still plenty of room for improvement in this process, but IMO it is still better than adding the entire platforms directory to version control.

@mmrko

This comment has been minimized.

Copy link

commented May 16, 2014

Neat! I like the platforms-merge approach to things. I've integrated this into my Gulp workflow with good success. Thanks :)

@casche

This comment has been minimized.

Copy link

commented Jul 20, 2015

@jonathandixon What's the license of this Gist ?

@jonathandixon

This comment has been minimized.

Copy link
Owner Author

commented Jul 28, 2015

I hadn't considered a license. I'm going with MIT, I added a license file.

@arun99178

This comment has been minimized.

Copy link

commented Sep 28, 2018

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
You can’t perform that action at this time.