Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 3
Run the following command to set up your HotelDatabase.jar project:
mvn archetype:create -DgroupId=com.javaworld.hotels -
DartifactId=HotelDatabase -Dpackagename=com.javaworld.hotels
Now you have a brand new Maven 2 project directory structure. Switch to the HotelDatabase directory to continue the tutorial.
Now we implement the business logic. The Hotel class is a simple JavaBean. The HotelModel class implements two services: the findAvailableCities() method, which lists available cities, and the findHotelsByCity() method, which lists all hotels in a given city. A simple, memory-based implementation of the HotelModel class is presented here:
package com.javaworld.hotels.model;
import java.util.ArrayList;
import java.util.List;
import com.javaworld.hotels.businessobjects.Hotel;
public class HotelModel {
/**
* The list of all known cities in the database.
*/
private static String[] cities =
{
"Paris",
"London",
};
/**
* The list of all hotels in the database.
*/
private static Hotel[] hotels = {
new Hotel("Hotel Latin","Quartier latin","Paris",3),
new Hotel("Hotel Etoile","Place de l'Etoile","Paris",4),
new Hotel("Hotel Vendome","Place Vendome","Paris",5),
new Hotel("Hotel Hilton","Trafalgar Square","London",4),
new Hotel("Hotel Ibis","The City","London",3),
};
/**
* Returns the hotels in a given city.
* @param city the name of the city
* @return a list of Hotel objects
*/
public List<Hotel> findHotelsByCity(String city){
List<Hotel> hotelsFound = new ArrayList<Hotel>();
for(Hotel hotel : hotels) {
if (hotel.getCity().equalsIgnoreCase(city)) {
hotelsFound.add(hotel);
}
}
return hotelsFound;
}
/**
* Returns the list of cities in the database which have a hotel.
* @return a list of city names
*/
public String[] findAvailableCities() {
return cities;
}
}
Now let's test the application. A few simple test classes can be found in the source code. Unit testing is (or should be!)
an important part of any enterprise Java application. Maven completely integrates unit testing into the development lifecycle.
To run all your unit tests, you invoke the test lifecycle phase:
mvn test
If you want to run only one test, you can use the test parameter:
mvn test -Dtest=HotelModelTest
A nice feature of Maven 2 is its use of regular expressions and the test parameter to control the tests you want to run. If you want to run only one test, you just indicate the name of the test
class:
mvn test -Dtest=HotelModelTest
If you want to run only a subset of the unit tests, you can use a standard regular expression. For example, to test all ModelTest classes:
mvn test -Dtest=*ModelTest
Once you're happy with the tests, you can build and deploy your new JAR. The install command compiles, tests, and bundles your classes into a jar file and deploys it to your local Maven 2 repository, where
it can be seen by other projects:
mvn install
Now we want to use this library in a Web application. For simplicity, our Web application will consist of a JavaServer Pages
(JSP) file that directly invokes the HotelModel class. First, we create a new Web application project using the archetype plug-in:
mvn archetype:create -DgroupId=com.javaworld.hotels -
DartifactId=HotelWebapp -Dpackagename=com.javaworld.hotels -
DarchetypeArtifactId=maven-archetype-webapp
Next, we need to include our business logic JAR in this application. All we need to do is to add a dependency to the new pom.xml,
pointing to our HotelDatabase component:
<dependency>
<groupId>com.javaworld.hotels</groupId>
<artifactId>HotelDatabase</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Now we implement the main (and only) JSP page. It simply lists the available cities and, if a city is chosen, lists the corresponding hotels:
<html>
<body>
<h2>Hotel database tutorial application</h2>
<%@ page import="
java.util.List,
com.javaworld.hotels.businessobjects.Hotel,
com.javaworld.hotels.model.HotelModel"
%>
<%
HotelModel model = new HotelModel();
String[] cityList = model.findAvailableCities();
String selectedCity = request.getParameter("city");
List<Hotel> hotelList = model.findHotelsByCity(selectedCity);
%>
<h3>Choose a destination</h3>
<form action="index.jsp" method="get">
Please choose a city:
<SELECT name="city">
<OPTION value="">---Any city---</OPTION>
<%
for(String cityName : cityList){
%>
<OPTION value="<%=cityName%>"><%=cityName%></OPTION>
<%
}
%>
</SELECT>
<BUTTON type="submit">GO</BUTTON>
</form>
<% if (hotelList.size() > 0) { %>
<h3>Available hotels in <%=selectedCity%> </h3>
<table border="1">
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Stars</th>
</tr>
<%
for(Hotel hotel : hotelList){
%>
<tr>
<td><%=hotel.getName()%></td>
<td><%=hotel.getAddress()%></td>
<td><%=hotel.getCity()%></td>
<td><%=hotel.getStars()%> stars</td>
</tr>
<%
}
%>
</table>
<%}%>
</body>
</html>
Now run mvn install from the HotelWebapp directory; this will compile, bundle, and deploy the HotelWebapp.war file to your local repository (you can also find it
in the target directory if you need to). Now you can deploy this war file to your favorite application server and see what you get (see
Figure 4).

Figure 4. The tutorial application in action
Maven 2 comes with an ever-increasing number of plug-ins that add extra functions to your build process with little effort.
To use a plug-in, you bind it to a lifecycle phase. Maven will then figure out when (and how) to use it. Some plug-ins are
already used by Maven behind the scenes, so you just have to declare them in the plugins section of your pom.xml file. The following plug-in, for example, is used to compile with J2SE 1.5 source code:
...
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
In other cases, you bind the plug-in to a lifecycle phase so that Maven will know when to use it. In the following example,
we want to run some standard Ant tasks. To do this, we bind the maven-antrun-plugin to the generate-sources phase, and add the Ant tasks between the tasks tags, as shown here:
...
<build>
...
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<!-- Ant tasks go here -->
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Maven 2.0 is a powerful tool that greatly simplifies and standardizes the build process. By promoting a standard project organization and recommended best practices, Maven handles much of the grunt work. And standard plug-ins such as the site generator provide valuable team-oriented project tools with little extra effort. Check it out!
Archived Discussions (Read only)