Coming Up for Air

Maven and Annotations: Not as Easy as It Should Be

Over the past year or so, I’ve been slowly migrating — somewhat accidentally — to Maven. I had even begun migrating the build environment for Scales from Ant to Maven, but hit a huge roadblock: annotation processing. Scales depends heavily on compile-time annotation processing, and the only thing I could find on the web was other people with the same problem. As I was working on some of my JSFOne examples, I really wanted to use Maven, as the NetBeans support is a lot cleaner with Maven versus an externally maintained Ant build file, so I set to with renewed purpose. Finally, I seem to have found the right query string, as I appear to have solved my problem. The solution? Ant.

One of the vaunted features of Maven is the ability to embed Ant scripts in your POM file. My first thought when I ran into the problem above was exploiting that capability, but those attempts were thwarted by one of Maven’s biggest weaknesses: poor documentation. As I noted, though, I finally found a web page that had something close to what I needed that I was able to work out the rest. Since my JSFOne examples have the same compilation requirements as does Scales, I was able to pull the annotation processing tasks from the Scales build, giving me this:

          <property name="target.dir" value="target/classes/META-INF"/>
          <mkdir dir="${target.dir}" />
          <apt srcdir="src/main/java"
            <option name="generate.runtime" value="" />
            <option name="namespace.uri"
              value="" />
            <option name="namespace.prefix" value="jsfone" />
            <option name="taglibdoc"
            <option name="localize" value="" />
              <path refid="maven.compile.classpath" />
          <move file="${target.dir}/taglib.xml"
          <move file="${target.dir}/facelets.taglib.xml"

It’s not really pretty, but we are talking about a Maven POM. ;) As I’m sure you’ve surmised by now, I’m not a Maven expert, but here is my understanding of things. We’re telling Maven to run these tasks when the generate-sources phase is run (if you can find documentation on what the Maven lifecycle is, I’d love to see it). The tasks run are, I think, pretty self-explanatory (their purpose is outside the scope of this post either way ; ). Note, though, that we can run as many arbitrary Ant tasks as we want.

One feature that I like a lot is the sourceRoot entry. With that line, we’re telling Maven to add the generate directory to the build path. Since the annotation processor creates the source for JSP Tag files, we need to compile that class, and this takes care of that for us.

One remaining problem was that this creates some directories and files that Maven doesn’t delete when the clean goal is run. To fix that, we add this XML snippet to the plugin description above:

      <delete dir="generate" />

With that, we can now issue a mvn clean and get rid of the generate directory that apt creates. Snazzy.

Maven experts will look at this and doubtless see many ways to improve the code, and some will likely suggest the Maven apt plugin from Tobago. What this represents, though, is a working solution in spite of Maven, ugly as it may be. Of course, I’m certainly open to suggestions and advice, but what I have is working, so I’m not going to lose much sleep over it. Hopefully, this will help someone else. Better yet, maybe the Maven developers will release a meaningful update to Maven that fixes problems like this. :)

tags: Ant Java Maven


Sample quote

Quote source