03/29/2006 The hardest part of getting started with a Java application is, well, getting started. So many logistical decisions have to be made up front. Where should the Java source files go? Where do I place unit testing? Where will we store dependency .jars? How will the project be built, documented, tested, and deployed? The choices made at this stage will follow a developer for the rest of the project. It‘s up to you whether those choices will haunt you or prove you to be a master Java architect later on. We‘ll assume the latter is the goal we are striving for, and now we just need a starting point.
There are many tools out there for building a Java project, including Ant. Ant has been on the top of many a developer‘s list as the revolutionary tool that got them out of the world of Maven is the new kid on the block, much like Ant was just a few short years ago. Maven 1.0 has been around for a few years and it was accepted by a wide audience of developers as an Ant replacement, but it offered very little relief from the old Ant build.xml file. Maven 1.0 was slow and clunky and using it was almost as difficult as getting started on a project with Ant. In fact, it was Ant at its core, and after an almost complete rewrite, Maven 2.0 was born. The Benefits of Maven 2.0The benefits of Maven 2.0 are numerous, as it does more than merely build your projects. If you are just starting a new Java project and you need to get started fast, Maven 2.0 will have you up an running in minutes. The following are some of the advantages of Maven 2.0:
The list above is just a short list of the features available in Maven 2.0. These alone make Maven 2.0 a solid choice for a build management system. Now that we know what Maven is, let‘s look at how to get started. Getting StartedThe first thing we want to do is set up our directory structure. Wait--there is no need to do it by hand. Maven can do it for you, depending on the type of project that you are developing. Once you have downloaded and extracted the latest distribution of Maven 2.0, you should add the bin directory of the Maven distribution to your system path. You can run Now that we have the tool installed, let‘s look at the example of creating a simple Java project. Maven uses archetypes to determine how the directory structure will be laid out. There are several built-in archetypes or you can write one of your own. Voila! We now have our project layout. Yes, it‘s that easy. It should be noted that this directory structure can be overridden by creating a new archetype, but deviating from the structure is not recommended, since one of the benefits of Maven is the standard directory structure. The directory structure contains two source trees: one for your Java application source code and one for your unit test code. You may also have noticed that the first time you ran Maven, it did some downloading. Maven will update itself with the appropriate functionality based on what plugin you use when you invoke the tool. Maven, by default, will get its updates from the Ibiblio repository. You can override Maven‘s choice of a remote repository in the conf directory of the Maven distribution or in the project itself. You should also have noticed that Maven created a pom.xml file in the my-app directory. This is the meat and potatoes of your project. The pom.xml file is a set of instructions for Maven that tells it how to build the project and includes other special instructions. (POM is an acronym for "project object model.") By default, Maven also includes the JUnit dependency to encourage unit testing. |
聯(lián)系客服