Wednesday, April 11, 2007

Using XDoclet with Maven 2

One of the useful features of Ant is ability to generate different kind of config files (and othe stuff) with the help of XDoclet. I would like to use this feature with Maven. It's take a lot of time to understand how to do the trick but for now it is working as expected and I want to share with you some of hints.
So, what is doing here. I just want to build servlet with Maven, generate deployment descriptor from annotated source and finally create a war file. OK, lets start.
First of all I'm creating a new Maven project like thins :

mvn archetype:create
-DgroupId=MyServlets
-DartifactId=longRunningServlet
-DarchetypeArtifactId=maven-archetype-webapp

In my pom.xml I have
<packaging>war</packaging>
so, all is ok. Final name of war file will be longRunningServlet.war. Further more interesting. Where I should place servlet source code ? Answer is longRunningServlet\src\main\java. But you should to create "java" directory by hand since it is not created by Maven. Is it bug of feature ? :) How we have directory strucure for building project with sources placed at longRunningServlet\src\main\java. To build servlet we need to set up dependencies. Standalone servlet has one dependency
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
<version>2.4</version>
</dependency>
Please notice scope=provided. In practice it is means that servlet api jar will not be placed in war's WEB-INF\lib.
And now XDoclet. Dependencies for XDoclet are :
<dependency>
<groupId>xdoclet</groupId>
<artifactId>xdoclet-web-module</artifactId>
<scope>provided</scope>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>xdoclet</groupId>
<artifactId>xjavadoc</artifactId>
<scope>provided</scope>
<version>1.1</version>
</dependency>
Actualy XDoclet is executed via Maven Antrun plugin with specific configuration of both - maven-run plugin and xdoclet. Looks impressively :


<plugin>                                                             
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<taskdef name="webdoclet"
classname="xdoclet.modules.web.WebDocletTask"
classpathref="maven.compile.classpath" />

<webdoclet
destDir="${basedir}/src/main/webapp/WEB-INF/"
excludedTags="@author,@version">
<fileset
dir="${basedir}/src/main/java"
includes="**/*.java" />
<deploymentdescriptor
destdir="${basedir}/src/main/webapp/WEB-INF/"
servletspec="2.4"/>
</webdoclet>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Webdoclet configuration in between "tasks" tag. You can place any Ant task here but in my case it is XDoclet call.

2 comments:

Anonymous said...

You saved my life... thank you very much!

Stas Ostapenko said...

I'm glad to hear that :) Thanks for comment !