Integrating AStyle into your Ant build
This blog entry will introduce you how to integrate AStyle into your Ant build.
Short introduction: In my current project I work on software which generates SDKs for multiple languages consuming Web21C services . The SDKs are SOAP webservice clients for multiple languages as Java, Python, PHP or .Net (new are comming soon). XSLT transformations are extensively in use to achieve the goal of generating those SDKs.
The problem is that we are facing a trade off:
- we have ugly formatted XSL to be able to generate correctly formatted output code
- we can have pretty formatted and easy to maintain and read XSL but not correctly formatted output in particular language
Let’s assume that we are not crazy yet and we will not follow the first option. The second option looks better but is still not perfect. We need to plug a tool which will do the dirty job for us: correct indentation, format and beautify our output source. Artistic Style (AStyle) has been selected as the tool as it is small, simple, fast tool and supports both Java and C#.
However the AStyle has couple of downsides:
- doesn’t have easy integration with Ant
- accepts only list of files for transformation, there is no recursive mode
Let’s assume that all source files to be transformed are in folder generated, with multiple subfolders. As there is no integration with Ant we will have to use <exec/> task. Something like:
<property name="astyle.exe" location="${astyle.home}/astyle.exe"></property>
<exec executable="${astyle.exe}" dir="${basedir}">
<arg value="--mode=java"></arg>
<arg value="--style=java"></arg>
<arg value="--suffix=none"></arg>
<arg value="${sourcefile}"></arg>
</exec>
AStyle accepts following arguments:
astyle [options] SourceFile1.java SourceFile2.java SourceFile3.java [...]
Wouldn’t it be nice to have -R option?
Our first approach could be construction of a space separated list of paths to all files in folder generated and then using this as argument in exec task. We would quickly realize that we have a possible problem with maximum length of a command line executed via exec task which is just 256 characters. Better will be to execute astyle many times, for each file to be formatted. Core Ant task are not enough in this case. We have to use ant-contrib tasks. First copy ant-contrib jar to your ANT_HOME/lib and add in your build:
<taskdef resource="net/sf/antcontrib/antlib.xml" />
It will let you use <for/> task which should help us to solve our problem as it will allow us to iterate over list of paths and execute external command for each one. The final ant task could look like:
<target name="format">
<for param="file">
<path>
<fileset dir="${generated}" includes="**/*.java"/>
</path>
<sequential>
<property name="astyle.exe" location="${astyle.home}/astyle.exe" />
<exec executable="${astyle.exe}" dir="${basedir}">
<arg value="--mode=java"/>
<arg value="--style=java"/>
<arg value="--suffix=none"/>
<arg value="@{file}"/>
</exec>
</sequential>
</for>
</target>
Fist we define a name of the parameter to pass the tokens or files to the sequential, let’s call it just file. Note that for makes use of ant’s macrodef task, so the @{} notation is used for parameter substitution. Next we define path including all java files in generated folder. The last step is to use <sequential/> block where the execution of astyle.exe will happen for each source file defined using @{file} parameter.
Popularity: 38% [?]
