Setting up Maven
Maven is designed to handle most of the common tasks that are part of building a Java application. Web applications are on this list; they require only a small deviation from the basic Maven configuration.
The first part of setting up Maven (after installing it, naturally), is to build the project.xml file:
<project> <groupId>zarrastudios</groupId> <artifactId>maven2</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>servletapi</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.8</version> <properties> <war.bundle>true</war.bundle> </properties> </dependency> </dependencies> <build> <sourceDirectory>src/main/java</sourceDirectory> <unitTestSourceDirectory>src/test/java</unitTestSourceDirectory> <resources> <resource> <directory>src/main/resources</directory> </resource> </resources> <unitTest> <includes> <include>**/*.java</include> </includes> </unitTest> </build> </project>
Comparing this project.xml file to the one in my previous article shows only a small difference. Each dependency that needs to be included in the Web archive needs to be tagged with the property war.bundle=true. Because every deployment environment includes the servlet API, I do not need to include that one, but I want to make sure that the log4j API is included.
The only other change to make is to tell Maven where to find the Web-specific files. Recall that I set these up in src/main/webapp. To tell Maven where these files are, I add one line to the project.properties file. In this case, the only line in the file:
That completes the configuration of Maven. No complicated build.xml that I have to copy from another project or write and test. There are no libraries to copy into the project or reference somewhere else on the file system. I did not need to set up JUnit, which can be a task in itself.
At this point, I merely need to tell Maven to build my application:
After a moment, a target directory is generated with a war file named maven2.war, among other files. Listing the files inside the archive finds the compiled class files, my jsp files, the web.xml file, and all the libraries on which this project is dependent.
A quick review of the output from Maven shows that it created the target directory structure, compiled the class files, compiled the test files, executed all the tests, and then created the Web archive. With a single command-line instruction and without a complicated setup, the application is now ready to be deployed.
Maven is ideally suited to help manage a Java project, whether it is a Web application, back-end service, or front-end GUI. It allows for a very simple configuration that is consistent across projects and ensures easy project maintenance. Maven's setup takes only a couple of minutes and does not require the normally complicated testing of the build process that is normally attributed to a Java project.