Maven 2 (Part 2): Dependencies, properties and scopes

Phillip Steffensen's picture

Welcome back to the second part of our tutorial-series on Maven 2. This part will focus on the pom.xml and the Maven 2 dependency management, Maven properties and dependency scopes. To get started let's first set up a project similar to the project we used in the first part of this tutorial. Set up the project as described in the article Maven 2 (Part 1): Setting up a simple Apache Maven 2 Project and reopen the pom.xml.

Dependencies

Some dependencies often are needed to write your applications. Commonly we (developers, developers, developers,...) are using some open source libraries and frameworks (e.g. the spring application framework or apache commons-logging,...). Sometimes own libraries should be referenced by a java project. To solve this problem Maven delivers a very good dependency mechanism that manages the dependencies of your project transitivly.

If you run

mvn package

Maven will download all dependencies referenced by your pom.xml from the central Maven 2 repository automatically. On mvnrepository.com you are able to search over all dependencies provided by this repository. Sometimes dependencies you need are not provided by the default Maven repository. In this case you don't have to despair. Maven allows to set up and define own repositories aswell. We'll explain how to set up own repositories in a later part of this tutorial-series. Let's first focus on dependencies and how to add them to your Maven 2 project by using apache commons-logging and the spring application framework.

We are going to start with apache commons-logging. To add apache commons-logging we add the following xml-nodes to our project:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" [...] >
  2.   <modelVersion>4.0.0</modelVersion>
  3.   <groupId>com.unitedcoders</groupId>
  4.   <artifactId>MyExampleApp</artifactId>
  5.   <packaging>jar</packaging>
  6.   <version>0.0.1-SNAPSHOT</version>
  7.   <name>MyExampleApp</name>
  8.   <url>http://maven.apache.org</url>
  9.  
  10.   <dependencies>
  11.  
  12.     <!-- Apache commons-logging -->
  13.     <dependency>
  14.       <groupId>commons-logging</groupId>
  15.       <artifactId>commons-logging</artifactId>
  16.       <version>1.1.1</version>
  17.     </dependency>
  18.  
  19.     <!-- Testing Dependencies -->
  20.     <dependency>
  21.       <groupId>junit</groupId>
  22.       <artifactId>junit</artifactId>
  23.       <version>4.5</version>
  24.       <scope>test</scope>
  25.     </dependency>
  26.  
  27.   </dependencies>
  28.  
  29. </project>

As you mentioned, we've done 2 changes. We changed the version number of junit to version 4.5 and we added a dependency node for commons-logging which contains groupId, artifactId and the version number. The groupId helps Maven to locate the dependency in the Maven repository. The artifactId defines which artifact of the specified group is needed (In our case "commons-logging") and the "version"-node defines the exact version of the dependency. I think 1.1.1 is the latest version of commons-logging at this time. If you now run

mvn clean package

again you'll see that Maven automatically downloads apache's commons-logging-1.1.1.jar from http://repo*.maven.org. To get a little bit more confidant with this lets add some more dependencies by modifying our pom.xml like this:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" [...] >
  2.   <modelVersion>4.0.0</modelVersion>
  3.   <groupId>com.unitedcoders</groupId>
  4.   <artifactId>MyExampleApp</artifactId>
  5.   <packaging>jar</packaging>
  6.   <version>0.0.1-SNAPSHOT</version>
  7.   <name>MyExampleApp</name>
  8.   <url>http://maven.apache.org</url>
  9.  
  10.   <build>
  11.     <finalName>MyExampleApp</finalName>
  12.     <plugins>
  13.       <plugin>
  14.         <groupId>org.apache.maven.plugins</groupId>
  15.         <artifactId>maven-compiler-plugin</artifactId>
  16.         <configuration>
  17.           <source>1.5</source>
  18.           <target>1.5</target>
  19.         </configuration>
  20.       </plugin>
  21.     </plugins>
  22.   </build>
  23.  
  24.   <dependencies>
  25.  
  26.     <!-- Spring Framework -->
  27.     <dependency>
  28.       <groupId>org.springframework</groupId>
  29.       <artifactId>spring-core</artifactId>
  30.       <version>2.5.5</version>
  31.     </dependency>
  32.  
  33.     <dependency>
  34.       <groupId>org.springframework</groupId>
  35.       <artifactId>spring-webmvc</artifactId>
  36.       <version>2.5.5</version>
  37.     </dependency>
  38.  
  39.     <dependency>
  40.       <groupId>org.springframework</groupId>
  41.       <artifactId>spring-web</artifactId>
  42.       <version>2.5.5</version>
  43.     </dependency>
  44.  
  45.     <dependency>
  46.       <groupId>org.springframework</groupId>
  47.       <artifactId>spring-jdbc</artifactId>
  48.       <version>2.5.5</version>
  49.     </dependency>
  50.  
  51.     <!-- Apache commons-logging -->
  52.     <dependency>
  53.       <groupId>commons-logging</groupId>
  54.       <artifactId>commons-logging</artifactId>
  55.       <version>1.1.1</version>
  56.     </dependency>
  57.  
  58.     <!-- javax Servlet API -->
  59.     <dependency>
  60.       <groupId>javax.servlet</groupId>
  61.       <artifactId>servlet-api</artifactId>
  62.       <version>2.4</version>
  63.       <scope>provided</scope>
  64.     </dependency>
  65.  
  66.     <dependency>
  67.       <groupId>javax.servlet</groupId>
  68.       <artifactId>jstl</artifactId>
  69.       <version>1.1.2</version>
  70.     </dependency>
  71.     <dependency>
  72.       <groupId>taglibs</groupId>
  73.       <artifactId>standard</artifactId>
  74.       <version>1.1.2</version>
  75.       <scope>runtime</scope>
  76.     </dependency>
  77.  
  78.     <!-- Testing Dependencies -->
  79.     <dependency>
  80.       <groupId>junit</groupId>
  81.       <artifactId>junit</artifactId>
  82.       <version>4.5</version>
  83.       <scope>test</scope>
  84.     </dependency>
  85.  
  86.     <!-- Database -->
  87.     <dependency>
  88.       <groupId>commons-dbcp</groupId>
  89.       <artifactId>commons-dbcp</artifactId>
  90.       <version>1.2.2</version>
  91.     </dependency>
  92.  
  93.     <dependency>
  94.       <groupId>mysql</groupId>
  95.       <artifactId>mysql-connector-java</artifactId>
  96.       <version>5.1.6</version>
  97.     </dependency>
  98.   </dependencies>
  99.  
  100. </project>

Please take a few minutes and take a look at all changes we made in pom.xml. After that let's discuss these changes starting at the top of pom.xml:

1. We added a build-node

The build-node contains build-specific configurations for our project. We inserted a "finalName" that defines how our jar-file will be named (in this case: MyExampleApp.jar) Also we added the „maven-compiler-plugin“ to be able to configure the Java version on which our project should be build.

2. Some more dependencies

And we added some more dependencies which we can use to set develop a web application for example.

Comments

Anonymous's picture

Hi Phillip,
thanks for your post. I have a problem with dependences with my project. I check out as Maven project my project from a subversion repository by m2eclipse plugin (I have eclipse ganymede e m2 plugin by codehaus).
After I have checked out it and I have the project in my workspace I have to 'enable nested modules', only after this operation I see the Maven dependences for this project.

Is it possible make this operation automatically when I check out as Maven project my project and not manually after I have checked out it?

Enrico

Phillip Steffensen's picture

Hi Enrico,

thanks for your comment. I've never used the m2eclipse plugin because I prefer to use the shell for subversion checkouts. Maybe the m2eclipse plugin forgot to configure your local Maven 2 repository as a classpath variable!? I wrote some words about Maven 2 and eclipse at http://united-coders.com/phillip-steffensen/maven-2-part-3-configuring-e.... In this article I wrote some basics about the Maven 2 eclipse plugin. (http://maven.apache.org/eclipse-plugin.html) Maybe this will help you.

Phil

RAMESH's picture

Hi Philip,

I am getting this error:

org.apache.maven.lifecycle.LifecycleExecutionException: Internal error in the plugin manager executing goal 'org.codehaus.mojo:buildnumber-maven-plugin:1.0-beta-1:create': Mojo execution failed.

Could please let me know.

Thanks,

Ramesh

Enrico's picture

It is not a good message. I think that the beta-version of the plug-in has some errors. I invite you to try a newer or stable version and re-try it.

Enrico