Automagic taglib descriptors with XDoclet and Maven 2

From time to time I do write JSP taglibs. The java-part of this is almost fun. But writing the tld-files by hand is something that simply is just boring. Therefor I use XDoclet (webdoclet) to generate these tld-files for me. This small tutorial will show you what XDoclet tags you must set in your taglib class, and finally how to get XDoclet doing the magic with Maven 2.

Your tag class

You can find a full list of the available web tags in XDoclet here: http://xdoclet.sourceforge.net/xdoclet/tags/web-tags.html. I will just cover enough to get a sample tld up and running.

First, let’s create a small HelloWorldTag:

package no.mydomain.tags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/**
 * @jsp.tag name="Hello" body-content="empty"
 */
public final class HelloTag extends TagSupport {

private String key;

/**
     * @jsp.attribute required="true" rtexprvalue="true"
     */
    public String getKey() {
        return key;
    }

    public void setKey(String newKey) {
        this.key = newKey;
    }

    public int doStartTag() throws JspException {
        try {
            pageContext.getOut().print("Hello world!");
        } catch (Exception ex) {
            throw new JspTagException(this.getClass().getName() + ": " +
                                      ex.getMessage());
        }
        return SKIP_BODY;
    }
}

The magic in this class is as you can see the XDoclet tags inside the javadoc.

/**
* @jsp.tag name="Hello" body-content="empty"
*/

This defines the name of the tag, and defines that the body of the tag will be skipped.

/**
* @jsp.attribute required="true" rtexprvalue="true"
*/

This defines the key attribute for the tag, saying that it is required and that it accepts a runtime expression as input.

There’s nothing more to it.

Configuring XDoclet (webdoclet) in Maven 2

I will illustrate this with an example. It should be fairly easy to see what I am doing. The point is that I’m telling webdoclet to include all classes found in my project that ends with Tag.java. You can off course define your own logic on what classes to include.

In your pom.xml, inside the <projects> tag, you should add this:

 <plugin>
   <artifactid>xdoclet-maven-plugin</artifactid>
   <groupid>org.codehaus.mojo</groupid>
   <executions>
     <execution>
       <phase>generate-sources</phase>
       <goals>
         <goal>xdoclet</goal>
       </goals>
       <configuration>
         <tasks>
           <webdoclet force="true">
                      destdir="${basedir}/target/classes/META-INF/"
                      verbose="true">
             <fileset dir="src/main/java">
               <include name="**/tags/*Tag.java"></include>
             </fileset>
             <jsptaglib jspversion="1.2">
                        filename="tags.tld"
                        shortname="apitags"
                        taglibversion="0.1"
                        description="A taglib for utilities in use by API"
                        uri="http://java.api.no/jsp/tags" />
             </jsptaglib>
           </webdoclet>
         </tasks>
       </configuration>
     </execution>
   </executions>
 </plugin>

Running ‘mvn clean package’ or ‘mvn clean compile’ will now generate your tld-file, and copy it into target/classes/META-INF. The tld-file will automatically be included into your jar output file given you have set your Maven project as a jar project.

Leave a Reply

Your email address will not be published. Required fields are marked *