Search This Blog

The collected random musings of some guy who writes software.

Friday, May 28, 2010

Part 2: Clojure, my relationship with Maven, and a sordid affair with ANT

Over the years, I've really come to love Maven. Funny thing is, I didn't care for Maven much at first. It took managing a medium sized project using ANT, then having to scale up to a much larger set of projects, before I could appreciate what Maven had to offer. It's not just a build technology, it's a software project management methodology where Maven is only one component. If you are one of those people who don't "get" Maven, that's okay. I used to be in that camp too, back in simpler times.

At this point in my career, I am firmly committed to Maven. Occasionally, though, It'd be really nice to break out of the whole pre-packaged plugin model, write some script, maybe call an ANT task, and just get the job done.

Lucky for me, there's a plugin out there that lets me do just that. The Sandflea maven-clojure-plugin lets me execute Clojure scripts as part of a Maven build, with complete access to the Maven API.

Remember the (ant ...) macro from part 1, and how we used it with the ZIP ANT task? Well, here's an example of using it in a Maven POM to create a ZIP artifact. Yes, this example is a bit contrived, but it's easy enough to understand.
<plugin>
  <groupId>sandflea.clojure</groupId>
  <artifactId>maven-clojure-plugin</artifactId>
  <executions>
    <execution>
      <id>Create ZIP</id>
      <goals><goal>execute</goal></goals>
      <phase>package</phase>
      <args>
        <source-dir>${project.basedir}/src/main/assembly</source-dir>
        <target-zip>${project.build.directory}/${project.artifactId}.zip</target-zip>
      </args>
      <configuration>
        <source>
        (ns my-project.create-zip
          (:import
            [java.io File]
            [org.apache.tools.ant.taskdefs Zip])
          (:use [sandflea.clojure.buildutil]))
        (ant (Zip.)  
          (.setBasedir  (File.(*args* "source-dir")))
          (.setDestFile (File.(*args* "target-zip")))
          (.compress true) )
        </source>
      </configuration>
    </execution>
  </executions>
</plugin>

I get to have my cake and eat it too! I know I can rely on steady and predictable Maven to always be there for me, and whenever I need to spice things up a bit, ANT is just a (function) call away. Isn't life sweet?

No comments:

Post a Comment

Followers