Skip to content

Instantly share code, notes, and snippets.

@jonathandixon
Last active January 5, 2021 22:00
Show Gist options
  • Star 54 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • 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/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
Copy link

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

@kylemacey
Copy link

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

@jonathandixon
Copy link
Author

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
Copy link

yesyo commented Feb 19, 2014

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

@jonathandixon
Copy link
Author

Thanks.

@paynecodes
Copy link

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
Copy link
Author

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
Copy link

mmrko 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
Copy link

casche commented Jul 20, 2015

@jonathandixon What's the license of this Gist ?

@jonathandixon
Copy link
Author

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

@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