March 16, 2008

Maven is Awesome

MavenLogo One of the things that always irked me about Ant (the longtime de-facto standard tool used for building Java software) is that in order to accomplish anything, I need to code long sequences of mundane, tedious tasks. Not only that, but I need to do this in XML. XML? XML is specifically designed for the storage and transfer of structured data. It is not particularly well suited for writing procedural code. Nevertheless, as the lead developer of a somewhat complex piece of Java software at work, I made the seemingly default choice of using Ant. With that choice of course, I sentenced myself to the pain of writing Ant tasks that created build directories, copied source files, copied jar file dependencies, executed javac against copied source files, built jar files, signed jar files, et cetera, et cetera, et cetera.
Now don't get me wrong. Ant is certainly a powerful and flexible tool and it wouldn't be used by countless Java projects if it was without any merit. However, GNU's make has been around a lot longer than Ant, and it:
  • Is just as powerful if not more powerful. Make can execute any task available from the command line shell while Ant must specifically implement each task in a Java wrapper.
  • Uses a more intuitive syntax for writing procedural code (namely a mashup of "make" syntax and shell syntax vs. XML)
This is not to suggest that people should be using make instead of Ant for their Java projects (in fact quite the opposite). However, I would submit that Ant is not a next generation build tool and is instead a make equivalent. Each, of course, is better suited to different types of projects based on requirements, but I wouldn't give Ant any points for innovation.

Recently, though, I was faced with the task of revising our build system. Seeing as it was becoming more complex, with multiple independent modules forming a hierarchy of dependencies, I took the initiative to seek out potential alternatives to the Ant pain. I discovered Maven, and not a moment too soon. In my opinion, Maven is a next generation build tool and turns the entire concept of building Java software on its head. A few of the highlights:
  • Like Ant, Maven uses XML. However, rather than coding the exact steps that you want the build process to execute, you code details about how your project is structured. (Using XML for structured data? Who would've thought?)
  • Maven enforces a default directory structure in terms of where your source code, test code, and resource files live. When building a project, you don't need to worry about copying files around because Maven already knows where your files are and handles these tedious tasks for you.
  • Maven uses a "single project, single artifact" convention. Each module in your overall project is setup as a separate Maven project that produces an individual artifact. Dependencies between modules are declared in the build files, and Maven automatically builds modules in the correct order, and includes their resulting artifacts in other modules' classpaths as necessary.
  • Maven is configured to use external "Maven repositories" to resolve dependencies. This eliminates the need to store third party artifacts in your projects build tree as they can be automatically resolved and downloaded from the Maven repositories.
There is certainly a learning curve associated with Maven. However, after the initial investment, it is undoubtedly time well spent. I have spent the past several days converting our project from Ant to Maven, and I am very satisfied with a cleaner, more elegant, and easier to maintain build system. Give it a look.

No comments :

Post a Comment