Maven Dependencies, Plugins and Repositories

In this post, we will discuss dependencies , plugins and repositories. Maven project might be dependent on other artifacts. These can be other libraries or even other projects. Maven will automatically discover all the dependencies either direct or transient. This is the most useful feature of Maven.

Dependencies

Dependencies are specified with <dependencies> tag in the pom.xml file. It has three main commonly used tag nodes as shown
groupId : group of related artifacts
artifactId: specific artifact id
version: version of the artifact

Other nodes:
type: Corresponds to POM packaging ( jar, war etc )
scope: scope of the artifact

Transitive Dependencies

Each dependency can have dependencies,when you specify one dependency, it can bring its own dependencies in, which are transitive. Conflicts between versions can be resolved by using <excludes> tag in <dependency>
You can run goal “dependency:tree” to see all dependencies brought in by pom. Alternatively, use dependency hierarchy in Eclipse to resolve conflicts in the  POM file.

Sample dependency in a POM file shown below:

<dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.8</version>
</dependency>
<dependency>
         <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
</dependency>

 

Plugins

Plugins are defined in the <plugins> section, under <build> section.They have groupId, artifactId, version, and other optional <configuration> and <execution> sections. Default groupId for plugins is org.apache.maven.plugins

Sample plugin

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-release-plugin</artifactId>
        <version>2.4</version>
</plugin>

 

Maven Repositories

Maven Repository is an HTTP server containing hierarchy of directories. Artifacts are stored under groupId / artifactId / version. Repositories are configured in <repositories> and <pluginRepositories> sections.
Default repository is Maven Central (http://repo.maven. apache.org/maven2/). We can also setup our own repositories as well and serve from a web server.

 

Maven_Local_Repository

Local Repository

The local repository is a local folder that is used to store all your project’s dependencies.
When you build Maven project, all dependency files will be stored in your local repository.

By default, local repository is default to .m2 folder
Local repository Setting   -> {M2_HOME}\conf\setting.xml

<localRepository>home/maven_repo</localRepository>

  • Local cache of plugins and dependencies, located in ${HOME}/.m2/repository (by default  : this is configurable)
    ● Maven checks here first for plugins and dependencies, then goes to configured repositories
    ● When you execute “mvn install”, the products of your build get installed in the local repository, so they are available for other builds

Central and Remote Repository

Maven will check your pom.xml file, to identify which dependency to download. First, it will get the dependency from your local repository , if not found, then it gets from the default central repository.

Related Posts