Ant usage in Gradle projects, with XMLBeans as an example

Porjects, which are running on Ant today, can also think about a movement to Gradle. This is of course the point of view in the Gradle community. Gradle deliver an Ant and Maven integration. That is one reason, why you get the current Ant version installed on your machine, when running the commandgradle -v.

------------------------------------------------------------
Gradle 1.4
------------------------------------------------------------

Gradle build time: Montag, 28. Januar 2013 03:42 Uhr UTC
Groovy: 1.8.6
Ant: Apache Ant(TM) version 1.8.4 compiled on May 22 2012
Ivy: 2.2.0
JVM: 1.7.0_17 (Oracle Corporation 23.7-b01)
OS: Mac OS X 10.8.3 x86_64

In Gradle scripts you can link Ant tasks, or you can call Ant commands. Here now an example, how to use Ant taskdef within Gradle, and as exmaple the Apache XML Beans lib is used. XML Beans is a JAXB implementation.

As first step, how does this task look like in a Ant file.

build.xml:

<project default="main" basedir=".">
  <taskdef name="xmlbean" 
      classname="org.apache.xmlbeans.impl.tool.XMLBean" 
      classpath="lib/xbean.jar"/>
	
  <target name="main" >
  	<xmlbean schema="xsd/XMLBeispiel.xsd" 
             destfile="genlib/XMLBeispiel.jar" 
             classpath="lib/xbean.jar"/>
  </target>
</project>

In this example a new Ant „element“ is created by the usage of taskdef, which is called in the target „main“. In the attribute schema the XML schema is defined, on which the JAXB classes should be based. And the attribute destfile defines the target JAR.
That everything is working fine, it is important, that the XML-Bean lib files are correctly located in the lib folder. This can be handled more easy with Gradle.

With Gradle you can direclty access the Maven Repository, to download automatically the needed libs (this is also possible with Ant, when using Ivy). In the Maven Repository it is shown, what value to add to the Gradle build file.

build.gradle:

repositories {
	mavenCentral()
}

dependencies {
    xmlbeans 'org.apache.xmlbeans:xmlbeans:2.5.0'
}

configurations {
    xmlbeans
}

task main {
	ant.taskdef(name: 'xmlbean', classname: 'org.apache.xmlbeans.impl.tool.XMLBean',
		classpath: configurations.xmlbeans.asPath)
	ant.xmlbean(schema: 'src/main/schemas/XMLBeispiel.xsd', 
		destfile: "build/libs/XMLBeispiel.jar",
		classpath: configurations.xmlbeans.asPath)
}

Starting at the top, repositories{ } defines the relationship to a repository, in this case the Maven Repository (mavenCentral()). Now the dependencies needs to be defined, so that it is clear, which libs need to be included into the project. In the example xmlbeans is used, version 2.5.0. The name is not predefined, so you would also be able to use „Joe“ instead of „xmlbeans“, but this would not increase the readability. The part behind the name and between the quotation marks, is the text you will get from the Maven Repository.

In the section configurations, xmlbeans is defined as a configuration element, with that it can be used in other parts.

main is an own defined taskname, which can be called from command line, this is similar to the „target“ in Ant. Now the Ant „notation“ will be followed. ant.taskdef is identical to the above taskdef in the Ant script, the attribute names are also identical to the Ant part, name="xmlbean" will be name : 'xmlbean' in Gradle. Instead of XML attributes, here the attributes are divided by a comma.
In the attribute classpath, you see a difference to the Ant file, the XML beans lib is linked with the usage of configuration, so you find now configurations.xmlbeans.asPath, which includes the defined lib.
The run part for the xmlbean task is identical.

Ein Gedanke zu „Ant usage in Gradle projects, with XMLBeans as an example“

  1. Hi there,

    Could you help me out with a nested classpath implementation where I’d be able to provide two classpaths at a time?

    Thanks
    Abby

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.