4.5. Add New Dependencies
The Simple Weather application is going to have 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’re going to be using Dom4J and Jaxen, to format the
output of this command-line program we are going to be using Velocity,
and we will 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 the following example.
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 above, we’ve added four more dependency
elements in
addition to the existing element which 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
widely used (like Log4J) that you’ll just remember what the groupId
and artifactId
are every time you need to use them. Velocity, Dom4J,
and Jaxen were all located using the searching capability on
http://repository.sonatype.org. This
is a public Sonatype Nexus instance which provides a search interface
to various public Maven repositories, you can use it to search for
dependencies. To test this for yourself, load
http://repository.sonatype.org 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
repository.sonatype.org, as you’ll
often find that certain libraries have more than one groupId
. With
this tool, you can make sense of the Maven repository.