Add New Dependencies

The simple weather application will need to complete the following three tasks: retrieve XML data from Yahoo! Weather, parse the XML from Yahoo, and then print formatted output to standard output. To accomplish these tasks, we have to introduce some new dependencies to our project’s pom.xml. To parse the XML response from Yahoo!, we’ll use Dom4J and Jaxen; to format the output of this command-line program, we’ll use Velocity; and we also need to add a dependency for Log4J, which we will be using for logging. After we add these dependencies, our dependencies element will look like Example 4-3.

Example 4-3. Adding Dom4J, Jaxen, Velocity, and Log4J as dependencies

<project>
  [...]
  <dependencies>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.14</version>
    </dependency>
    <dependency>
      <groupId>dom4j</groupId>
      <artifactId>dom4j</artifactId>
      <version>1.6.1</version>
    </dependency>
    <dependency>
      <groupId>jaxen</groupId>
      <artifactId>jaxen</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>velocity</groupId>
      <artifactId>velocity</artifactId>
      <version>1.5</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  [...]
</project>

As you can see, we’ve added four more dependency elements in addition to the existing element that was referencing the test-scoped dependency on JUnit. If you add these dependencies to the project’s pom.xml file and then run mvn install, you will see Maven downloading all of these dependencies and other transitive dependencies to your local Maven repository.

How did we find these dependencies? Did we just “know” the appropriate groupId and artifactId values? Some of the dependencies are so common (such as Log4J) that you’ll just remember what the groupId and artifactId are every time you need to use them. As for Velocity, Dom4J, and Jaxen, we located them using the very helpful web site http://www.mvnrepository.com. This site provides a search interface for the Maven repository that you can use to search for dependencies. To test this yourself, visit http://www.mvnrepository.com and search for some commonly used libraries such as Hibernate or the Spring Framework. When you search for an artifact on this site, it will show you an artifactId and all of the versions known to the central Maven repository. Clicking on the details for a specific version will load a page that contains the dependency element you’ll need to copy and paste into your own project’s pom.xml. If you need to find a dependency, you’ll want to check out http://www.mvnrepository.com, because you’ll often find that certain libraries have more than one groupId. With this tool, you can make sense of the Maven repository.

Get Maven: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.