(Quick Reference)

4.2 Crear Scripts de Gant - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith

Version: null

4.2 Crear Scripts de Gant

You can create your own Gant scripts by running the create-script command from the root of your project. For example the following command:

grails create-script compile-sources

Will create a script called scripts/CompileSources.groovy. A Gant script itself is similar to a regular Groovy script except that it supports the concept of "targets" and dependencies between them:

target(default:"The default target is the one that gets executed by Grails") {
    depends(clean, compile)
}

target(clean:"Clean out things") { ant.delete(dir:"output") }

target(compile:"Compile some sources") { ant.mkdir(dir:"mkdir") ant.javac(srcdir:"src/java", destdir:"output") }

Puede crear sus propios scripts de Gant mediante la ejecución del comando create-script en la raíz de su proyecto. Por ejemplo, el siguiente comando:

grails create-script compile-sources

creará un script llamado scripts/CompileSources.groovy. Un script de Gant es muy similar a un script normal de Groovy, excepto que soporta el concepto de "targets" y las dependencias entre ellos:

target(default:"The default target is the one that gets executed by Grails") {
    depends(clean, compile)
}

target(clean:"Clean out things") { ant.delete(dir:"output") }

target(compile:"Compile some sources") { ant.mkdir(dir:"mkdir") ant.javac(srcdir:"src/java", destdir:"output") }

As demonstrated in the script above, there is an implicit ant variable (an instance of groovy.util.AntBuilder) that allows access to the Apache Ant API.
In previous versions of Grails (1.0.3 and below), the variable was Ant, i.e. with a capital first letter.

You can also "depend" on other targets using the depends method demonstrated in the default target above.

Como se demuestra en la secuencia anterior, hay una variable implícita ant (una instancia de groovy.util.AntBuilder) que permite el acceso a la Apache Ant API.
En las versiones anteriores de Grails (1.0.3 y anteriores), la variable era Ant, es decir, con una letra mayúscula en primer lugar.

También puede "depender" de otros targets utilizando el método depends mostrado en el valor default del target anterior.

The default target

In the example above, we specified a target with the explicit name "default". This is one way of defining the default target for a script. An alternative approach is to use the setDefaultTarget() method:

El target predeterminado

En el ejemplo anterior, se especifica un target con el nombre explícito "default". Esta es una manera de definir el target predeterminado para un script. Un enfoque alternativo es el uso del método setDefaultTarget():

target("clean-compile": "Performs a clean compilation on the app source") {
    depends(clean, compile)
}

target(clean:"Clean out things") { ant.delete(dir:"output") }

target(compile:"Compile some sources") { ant.mkdir(dir:"mkdir") ant.javac(srcdir:"src/java", destdir:"output") }

setDefaultTarget("clean-compile")

This lets you call the default target directly from other scripts if you wish. Also, although we have put the call to setDefaultTarget() at the end of the script in this example, it can go anywhere as long as it comes after the target it refers to ("clean-compile" in this case).

Which approach is better? To be honest, you can use whichever you prefer - there don't seem to be any major advantages in either case. One thing we would say is that if you want to allow other scripts to call your "default" target, you should move it into a shared script that doesn't have a default target at all. We'll talk some more about this in the next section.

Esto permite llamar al target predeterminado directamente desde otros scripts si así lo desea. Además, aunque hemos puesto la llamada a setDefaultTarget() al final de la secuencia de comandos en este ejemplo, puede ir en cualquier lugar, siempre y cuando esté después del objetivo que se refiere ("clean-compile" en este caso).

¿Qué enfoque es mejor? En realidad puede utilizar lo que prefiera, no parece haber ninguna ventaja importante en cualquier caso. Una cosa que diría es que si desea permitir que otros scripts llamen a su target por defecto, debe moverse en un script compartido que no tiene un target predeterminado. Hablaremos un poco más sobre esto en la siguiente sección.