Make delicious recipes!

Dependencies in Maven


A project may have dependency on some project which may have dependency on several others and so on.
It may be a nightmare to manage all these dependencies manually, so maven provides a cool way to do this.


cd ~/.m2/repository/org/apache/maven/plugins
find . -name \*.pom
./maven-antrun-plugin/1.3/maven-antrun-plugin-1.3.pom
./maven-archetype-plugin/2.2/maven-archetype-plugin-2.2.pom
./maven-assembly-plugin/2.2-beta-5/maven-assembly-plugin-2.2-beta-5.pom
./maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
.
.
.
./maven-surefire-plugin/2.12.4/maven-surefire-plugin-2.12.4.pom
./maven-war-plugin/2.1.1/maven-war-plugin-2.1.1.pom

cat maven-jar-plugin/2.4/maven-jar-plugin-2.4.pom
 .
 .
 .
 <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version></version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-project</artifactId>
      <version></version>
    </dependency>
	.
	.
	.
    <dependency>
      <groupId>org.codehaus.plexus</groupId>
      <artifactId>plexus-utils</artifactId>
      <version>3.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.plugin-testing</groupId>
      <artifactId>maven-plugin-testing-harness</artifactId>
      <version>1.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
 .
 .
 .

So, every plugin in maven has a pom.xml which defines the dependencies for that project.
Due to this, you need not manually define all dependencies.
A developer can just include the top-level dependencies and rest assured that the whole dependency tree will be managed automatically by maven!


For example, to add a dependency on log4j, the following needs to be added inside the <dependencies> element.
<dependency>
  <groupId>org.apache.logging.log4j.adapters</groupId>
  <artifactId>log4j-1.2-api</artifactId>
  <version>2.0-beta4</version>
</dependency>

Once this is done, maven will download whatever is needed to download log4j for your application.
To see what dependencies are in use, run the command mvn dependency:resolve to get the following output:
[INFO] ------------------------------------------------------------------------
[INFO] Building simple 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:resolve (default-cli) @ simple ---
[INFO]
[INFO] The following files have been resolved:
[INFO]    org.apache.logging.log4j:log4j-api:jar:2.0-beta4:compile
[INFO]    org.apache.logging.log4j.adapters:log4j-1.2-api:jar:2.0-beta4:compile
[INFO]    org.apache.logging.log4j:log4j-core:jar:2.0-beta4:compile
[INFO]    junit:junit:jar:3.8.1:test
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------


Another cool command is mvn dependency:tree which outputs the dependencies in a tree format:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building simple 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ simple ---
[INFO] org.companyname.simple:simple:jar:1.0-SNAPSHOT
[INFO] +- junit:junit:jar:3.8.1:test
[INFO] \- org.apache.logging.log4j.adapters:log4j-1.2-api:jar:2.0-beta4:compile
[INFO]    +- org.apache.logging.log4j:log4j-api:jar:2.0-beta4:compile
[INFO]    \- org.apache.logging.log4j:log4j-core:jar:2.0-beta4:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------







Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal