Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

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.
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']);
};
{
"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"
}
}
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.