Grails Ant Plugin

  • Tags: ant, zip
  • Latest: 0.1.3
  • Last Updated: 04 August 2011
  • Grails version: 1.2.3 > *
  • Authors: null
0 vote
Dependency:
compile ":grails-ant:0.1.3"

 Documentation  Source  Issues

Summary

Provides ant to Grails applications

Installation

grails install-plugin grails-ant

Description

Overview

This plugin is for using ant within a Grails application. It has nothing to do with building an app. The primary thing it does is really to just get the ant jars available at run time. Even though ant is part of Grails core, since it's primarily a build tool, it's not included in a Grails war file (1). And you can do that in your own app, without this plugin, by adding ant to your build.config.

dependencies {
		// Workarounds for Grails not shipping ant in WAR
		compile 'org.apache.ant:ant:1.7.1'    //you can also use runtime
		compile 'org.apache.ant:ant-launcher:1.7.1'	
    }

But this plugin adds a dynamic property called "ant" to all artifacts which provides an instance of AntBuilder.

This plugin also adds a service with some convenient methods to execute ant commands. The current version only supplies zip and unzip convenience functions. But the ant dynamic property gives you complete access to AntBuilder. Please contribute other functions to this plugin as you find a need for other ant tasks.

Usage

class DemoController {

def index = { ant.echo message: 'Hello From Ant!'

render 'it worked' } }

To use the convenience functions below, include the Ant Utilities Service:

def antUtilsService

Zip

To zip up everything in a directory you can just specify the output file name and directory to zip up.

//def zip(String destfile,String basedir)
antUtilsService.zip("newzip.zip",workingDirName)

If you only want certain files, specify that in the includes param.

//def zip(String destfile,String basedir,String includes)
antUtilsService.zip("newzip.zip",workingDirName,"**/*.txt")

If you want to exclude files, specify that in the excludes param.

//def zip(String destfile,String basedir,String includes,String excludes)
antUtilsService.zip("newzip.zip",workingDirName,"**/*.*","**/*.doc")

Unzip

To just unzip a file, specify the zip file name and the directory to put the output. Note that this will not replace the contents of the output directory if it already exists.

//def unzip(String zipFile,  String destDir)
antUtilsService.unzip(planZipFile,workingDirName)

If you want to replace an existing output directory, set the overwrite param to true. If not specified, it defaults to false.

//def unzip(String zipFile,  String destDir, Boolean overwrite)
antUtilsService.unzip(planZipFile,workingDirName,true)

You can specify a mapperType, such as "flatten", which will drop directories in the zip file and put all the files in the output directory that you specified. Again, this will not overwrite an existing output directory.

//def unzip(String zipFile,  String destDir, String mapperType)
antUtilsService.unzip(planZipFile,workingDirName2,"flatten")

If you want to replace an existing output directory, set the overwrite param to true. If not specified, it defaults to false.

//def unzip(String zipFile,  String destDir, String mapperType, Boolean overwrite)		
antUtilsService.unzip(planZipFile,workingDirName2,"flatten",true)

As of v0.1.2, unzip can throw an UnzipException:

try {
			antUtilsService.unzip(notAZipStr,workingDirName)
		} catch (org.grails.plugins.grailsant.UnzipException e) {
			println e.message
			println e.fileName
		}

References

  1. http://jira.grails.org/browse/GRAILS-5675
  2. http://preferisco.blogspot.com/2010/06/using-goovy-antbuilder-to-zip-unzip.html
  3. http://groovy.codehaus.org/Using+Ant+from+Groovy
  4. http://groovy-almanac.org/creating-a-zipfile-with-antbuilder/
  5. http://ant.apache.org/manual/
  6. http://svn.codehaus.org/groovy/trunk/groovy/groovy-core/src/test/groovy/util/AntTest.groovy