Port or convert Ant builds to GAnt. Example with maven integration (embedded GAnt)

By neokrates, written on May 28, 2010

article

  • Join date: 11-30-99
  • Posts: 224
View Counter:
  • 129 views
Rate it
Ad
Poll
  • What are your favorite build tools?

    View Results

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext

GAnt may make things simpler. For example, it is a replacement for AntContrib, which served well the ant builders. Porting from ant to gant is actually straightforward, but slight differences in both tools might cost your days of work. Here is how you can map between Ant and Gant way of doing builds.

Software:

✔ Maven 2.1.0

✔ Ant 1.7

✔ Groovy 1.7

✔ Gant 1.7

✔ Java 1.6

Should also work for:

✔ Should work for many other versions of those dev. tools, but manual debug may be required because of the version difference.

Preconditions:

✔ Maven environment must be working

1

Starting from maven or from Ant without maven

Generally GAnt can be started from Ant. The taskdef name="gant" will be needed along with the Gant and Groovy jars.
I show the setup with maven, Ant is similar and should be even simpler. Just put extra jars (gant and groovy-all) where ant can see them.

👉 Ant:

Ant is called using maven-antrun-plugin.
in pom.xml:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <id>start-runme-task</id>
            <phase>pre-integration-test</phase>
            <configuration>
              <tasks>
                <ant antfile="build.xml">
                  <property name="myProperty" value="${myValue}" />
                  <target name="runme" />
                </ant>

              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
 <plugin>

 

👉 GAnt:

GAnt is also called using maven-antrun-plugin, but it needs the gant task to be defined. ;-)

<taskdef name="gant" classname="org.codehaus.gant.ant.Gant" />

 
In pom.xml:

 <plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.3</version>
     <executions>
          <execution>
            <id>start-runme-task-with-gant</id>
            <phase>pre-integration-test</phase>
             <configuration>
     <tasks>
      <taskdef name="gant" classname="org.codehaus.gant.ant.Gant" />

      <gant file="build.gant" target="runme">
       <definition name="myProperty" value="${myValue}" />
      </gant>
     </tasks>
    </configuration>
            <goals>
              <goal>run</goal>
            </goals>
  </execution>
     </executions>
     <dependencies>
   <dependency>
    <groupId>org.codehaus.gant</groupId>
    <artifactId>gant_groovy1.7</artifactId>
    <version>1.9.0</version>
   </dependency>
   <dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>1.7.0</version>
   </dependency>
  </dependencies> 
 </plugin>

2

Calling targets

👉 Ant:

                <ant antfile="build.xml">
                  <property name="myProperty" value="${myValue}" />
                  <target name="runme" />
                </ant>

 

👉 GAnt:

Three differences to Ant:

⭐ need to define GAnt task;

⭐ instead of property, definition keyword is used;

⭐ GAnt probably has problems with java.style.properties. So you name them javaStyleProperties instead

<taskdef name="gant" classname="org.codehaus.gant.ant.Gant" />
<!-- ... --> 
<gant file="build.gant" target="runme">
 <definition name="myProperty" value="${myValue}" />
 <definition name="javaStyleProperty" value="${java.style.property}" />
</gant>

3

Using tasks

👉 Ant:

    <waitfor maxwait="10" maxwaitunit="second" checkevery="1" checkeveryunit="second">
      <http url="http://${myserver}/index.asp"; />
    </waitfor>

 

👉 GAnt:

 

⭐ You set properties using format:

task ( name : value, name1 : value1 ) . (Gant uses a java map datatype to do that)

⭐ Complex ‘child’ properties are set like:

  parenttask ( name : value, name1 : value1 ) {
     childlevel1 ( name : value ) {
 childlevel2 ( name : value ) {
      }
     }
           child1level1 ( name : value ) {
     }
  }

 
waitfor example:


   waitfor ( maxwait : "10" , maxwaitunit : "second" , 
             checkevery : "1" , checkeveryunit : "second" ) 
   {
      http ( url : "http://${myserver}/index.asp"; ) 
   } 

4

Handling properties

👉 Ant:

  <property file="${basedir}/project.properties" />
  <property name="server" value="localhost:${port}" />

 

👉 GAnt:

ant.property ( file : basedir + '/project.properties' )
ant.property ( name : 'server' , localhost:${port} )

 
Pretty simple. Don’t forget the ant. prefix or you will see something like:

[INFO] An Ant BuildException has occured: groovy.lang.MissingMethodException: No signature of method: build.property() is applicable for argument types: (java.util.LinkedHashMap) values: [[file:/your/cool/path/project.properties]]
Possible solutions: getProperty(java.lang.String), getProperty(java.lang.String), hasProperty(java.lang.String)

 
To be continued … 8)

Be Sociable, Share!
 

LEARN MORE (amazon bookstore)

TAGS

SOCIAL
Be Sociable, Share!


Leave a Reply