Maven Project Dependencies

For solving external libraries dependency problem Maven provides dependency management functionality for adding external libraries dependency to the project by adding some configurations into maven pom.xml file of project and then Maven downloads them for you and puts them in your local Maven repository. If any of these external libraries need other libraries, then these other libraries are also downloaded into your local Maven repository.
Maven Project Dependencies
@ImageSource-guntherpopp.blogspot.com

You can specify your project dependencies inside the dependencies element in the POM file. Here is an example:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>LoggingExample</groupId>
  <artifactId>LoggingExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>

        <!--log4j2 dependencies-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.6</version>
        </dependency>
  
 <dependency>

     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.8.1</version>
     <scope>test</scope>
        </dependency>
    </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Notice above Maven POM file we have specific element dependencies and under this element there is list of dependency element with required elements groupId, artifactId and version.

The example above needs the org.apache.logging.log4j group's log4j-api and log4j-core artifacts in version 2.6, and the junit group's junit artifact in version 4.8.1

Now when we run this project by executing Maven POM file then Maven automatically downloaded all dependencies define into POM file from Maven central repository to Maven Local Repository. Suppose in case that required dependency already into Maven Local repository then Maven ignore to download from central repository. Only those dependencies downloaded by the maven which does not exist into maven local repository.

External Project Dependencies

Some time for any project we have requirement of Jar file which is not located in a Maven repository (neither local, central or remote repository). It may be somewhere in your computer machine's hard disk. In this case you can configure an external dependency like this:

<dependency>
  <groupId>myexternaldependency</groupId>
  <artifactId>myexternaldependency</artifactId>
  <scope>system</scope>
  <version>1.0</version>
  <systemPath>${basedir}\war\WEB-INF\lib\myexternaldependency.jar</systemPath>
</dependency>

Notice in above code groupId and artifactId are both set to the same name of the dependency as the name of the API used, that is. The scope element value is set to system. The systemPath element is set to point to the location of the JAR file containing the dependency

Snapshot Version Dependencies
This type of dependencies are under development. Snapshot versions are always downloaded into your local repository for every build, even if a matching snapshot version is already located in your local repository. Always downloading the snapshot dependencies assures that you always have the latest version in your local repository, for every build.

<modelVersion>4.0.0</modelVersion>
  <groupId>LoggingExample</groupId>
  <artifactId>LoggingExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>


<dependency>
    <groupId>LoggingExample</groupId>
    <artifactId>LoggingExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
   </dependency>

SNAPSHOT added with version element under the dependency in the Maven POM file.


No comments:

Post a Comment