Thursday, May 22, 2008

Using Maven to build your custom widgetset

In a previous entry, I explained how to set up Maven & ItMill Toolkit for them to work together.

It is also possible to use Maven to automatically build your custom widgets during its compile phase.

That was one a few things that I did not like in the toolkit: each time you want to build your widget, to create the html & js files needed, you have to launch a specific ANT file. Having two separate build mechanisms in a single project... Well, that was not to my taste.
But Maven is Jack of all trade, and is willing to execute any ANT task - if you ask nicely.

For detailed informations on the Maven ANT Run plugin, refer to the official documentation: http://maven.apache.org/plugins/maven-antrun-plugin/

To build the widget located on com.your.company.widget.widgetset , insert the following XML snipset in
your pom.xml :


<project>
...
<build>
<plugins>
...
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>compile</phase>
<configuration>
<tasks>
<property name="compile_classpath" refid="maven.compile.classpath"/>
<property name="client-side-src-location" value="${project.build.sourceDirectory}" />
<property name="client-side-destination"
value="${project.build.directory}\${artifactId}-${version}/ITMILL/widgetsets" />
<path id="compile.classpath">
<pathelement path="${client-side-src-location}" />
<pathelement path="${compile_classpath}"/>
</path>
<java classname="com.google.gwt.dev.GWTCompiler"
failonerror="yes" fork="yes" maxmemory="128m">
<arg value="-out" />
<arg value="${client-side-destination}" />
<arg value="com.your.company.widget.widgetset" />
<classpath>
<path refid="compile.classpath"/>
</classpath>
</java>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>


What this code do is quite simple:
- Tell Maven that it must be executed in the compile phase
- Specify the properties needed by the ANT task, such as the libraries classpath, the source location & the output folder (using Maven global variables to ensure compatibility)
- Specify the ANT task itself, ie. the GWT compiler

As it is done, the resulting files will be put in the appropriate target folder, ensuring that those files will be accessible from the deployed application, included in the WAR file, and cleaned with the rest of the sources when asked via mvn clean.

No comments: