Tips on Gant scripts on Grails

  1. While GRAILS-7274 is not fixed, use try-catch when writing build event handlers;
        eventCreateWarEnd = {warName, stagingDir ->
             try {
                 // Script logic goes here...
             } catch (Exception e) {
                 e.printStackTrace()
                 throw e
             }
        }
    
  2. Add log verbosity to Gant, when appropriate:
    import org.codehaus.gant.GantState
    // (...)
    GantState.verbosity = GantState.VERBOSE
    ant.logger.setMessageOutputLevel(GantState.verbosity)
    
  3. Prefer AntBuilder.path to the use of nested classpath elements:
            ant.path(id: 'myClasspath', { pathelement(location: 'somewhere') })
            ant.java(classname: 'aClass', dir: 'anyDir', fork: true, 
                        classpathref:'myClasspath') {}
    

The “prize” for having read this far is the necessary snippet to execute the weblogic.appc (among other activities, it will pre-compile the JSPs) after the creation of the .war file of your project:

// _Events.groovy
import org.codehaus.gant.GantState

eventCreateWarEnd = {warName, stagingDir ->

    def beaHome = System.getenv('BEA_HOME')?: 'C:/bea'
    try {
        println "Executing weblogic.appc ${beaHome} ${warName}"

        //GantState.dryRun = true
        GantState.verbosity = GantState.VERBOSE
        ant.logger.setMessageOutputLevel(GantState.verbosity)

        File warFile = new File(warName)
        File tmpFile = new File(warFile.parentFile, "${warFile.name.tokenize('.')[0]}_tmp.war")
        ant.move(file: warFile, tofile: tmpFile)

        ant.path(id: 'appcClasspath', { pathelement(location: "${beaHome}/wlserver_10.3/server/lib/weblogic.jar") })

        ant.java(classname: 'weblogic.appc', dir: warFile.parentFile, fork: true, classpathref:'appcClasspath') {
            arg(line: "-output ${warFile.name} -lineNumbers -g -O -keepgenerated ${tmpFile.name}")
        }
    } catch (Throwable e) {
        e.printStackTrace()
        throw e
    }
}

Additional references:

Leave a Reply

Your email address will not be published. Required fields are marked *

*


− 6 = zero

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>