How do I use external dependencies?

Dependency management is a core feature of Maven. Managing dependencies for a single project is easy. Managing dependencies for multi-module projects and applications that consist of “n” number of modules is also possible. Maven helps a lot in defining, creating, and maintaining reproducible builds with well-defined class-paths and library versions.

The dependencies section of the pom.xml lists all of the external dependencies that our project needs in order to build (whether it needs that dependency at compile time, test time, run time, or whatever).

For example, our project is depending on JUnit only and dependency looks as below in pom.xml.

<project>
<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
</dependencies>
</project>

For each external dependency declaration in pom.xml, Our maven project need at least 4 things: groupId, artifactId, version, and scope.

The groupId, artifactId, and version are the same as those given in the pom.xml for the project that built that dependency. The scope element indicates how your project uses that dependency, and can be values like compiletest, and runtime.

With this information about a dependency, Maven will be able to reference the dependency when it builds the project.

Where does Maven reference the dependency from?

Maven looks in your local repository (${user.home}/.m2/repository is the default location) to find all dependencies.

What about dependencies built somewhere else? How do they get into my local repository?

Whenever a project references a dependency that isn’t available in the local repository, Maven will download the dependency from a remote repository into the local repository. You probably noticed Maven downloading a lot of things when you built your very first project (these downloads were dependencies for the various plugins used to build the project). By default, the remote repository Maven uses can be found (and browsed) at http://repo.maven.apache.org/maven2/. You can also set up your own remote repository (maybe a central repository for your company) to use instead of or in addition to the default remote repository.

What is maven-metadata.xml?

Navigate to any appropriate directory on Maven Central as  following /maven2/<groupId>/<artifactId> and you can find a file called maven-metadata.xml. For log4j, its looks like below.

<metadata>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.1.3</version>
  <versioning>
    <versions>
      <version>1.1.3</version>
      <version>1.2.4</version>
      <version>1.2.5</version>
      <version>1.2.6</version>
      <version>1.2.7</version>
      <version>1.2.8</version>
      <version>1.2.11</version>
      <version>1.2.9</version>
      <version>1.2.12</version>
    </versions>
  </versioning>
</metadata>

From this file, we can see that the groupId we want is “log4j” and the artifactId is “log4j”.

We see lots of different version values to choose from; for now, we’ll just use the latest version, 1.2.12 (some maven-metadata.xml files may also specify which version is the current release version). Alongside the maven-metadata.xml file, we can see a directory corresponding to each version of the log4j library. Inside each of these, we’ll find the actual jar file (e.g. log4j-1.2.12.jar) as well as a pom file (this is the pom.xml for the dependency, indicating any further dependencies it might have and other information) and another maven-metadata.xml file.

Now that we know the information we need, we can add the dependency to our pom.xml:

<project> 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.12</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

Now, when we compile the project (mvn compile), we’ll see Maven download the log4j dependency for us.

Conclusion

Hence we saw about local repository, dependency usage and maven-metadata.xml.

Please follow and like us:
error