Concept of Maven Repositories and Plugins in Maven

Last updated on May 27 2022
Mrinalini Pandey

Table of Contents

Concept of Maven Repositories and Plugins in Maven

Maven – Repositories

What is a Maven Repository?

In Maven terminology, a repository is a directory where all the project jars, library jar, plugins or any other project specific artifacts are stored and can be employed by Maven easily.
Maven repository are of three types. The subsequent illustration will give an idea regarding these three types.
• local
• central
• remote

Page 1 Image 1 24
Maven Repository

Local Repository

Maven local repository is a folder location on your machine. It gets created when you run any maven command for the first time.
Maven local repository keeps your project’s all dependencies (library jars, plugin jars etc.). When you run a Maven build, then Maven automatically downloads all the dependency jars into the local repository. It helps to avoid references to dependencies stored on remote machine every time a project is build.
Maven local repository by default get created by Maven in %USER_HOME% directory. To override the default location, mention another path in Maven settings.xml file available at %M2_HOME%\conf directory.
<settings xmlns = “http://maven.apache.org/SETTINGS/1.0.0”
xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation = “http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd”>
<localRepository>C:/MyLocalRepository</localRepository>
</settings>
When you run Maven command, Maven will download dependencies to your custom path.

Central Repository

Maven central repository is repository provided by Maven community. It contains a large number of commonly employed libraries.
When Maven does not find any dependency in local repository, it starts searching in central repository using subsequent URL − https://repo1.maven.org/maven2/
Key concepts of Central repository are as follows −
• This repository is managed by Maven community.
• It is not required to be configured.
• It requires internet access to be searched.
To browse the content of central maven repository, maven community has provided a URL − https://search.maven.org/#browse. Using this library, a developer can search all the available libraries in central repository.

Remote Repository

Sometimes, Maven does not find a mentioned dependency in central repository as well. It then stops the build process and output error message to console. To prevent such situation, Maven provides concept of Remote Repository, which is developer’s own custom repository containing required libraries or other project jars.
For example, using below mentioned POM.xml, Maven will download dependency (not available in central repository) from Remote Repositories mentioned within the equivalent pom.xml.
<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>com.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>com.companyname.common-lib</groupId>
<artifactId>common-lib</artifactId>
<version>1.0.0</version>
</dependency>
<dependencies>
<repositories>
<repository>
<id>companyname.lib1</id>
<url>http://download.companyname.org/maven2/lib1</url>
</repository>
<repository>
<id>companyname.lib2</id>
<url>http://download.companyname.org/maven2/lib2</url>
</repository>
</repositories>
</project>

Maven Dependency Search Sequence

When we execute Maven build commands, Maven starts looking for dependency libraries within the subsequent sequence −
• Step 1 − Search dependency in local repository, if not found, move to step 2 else perform the further processing.
• Step 2 − Search dependency in central repository, if not found and remote repository/repositories is/are mentioned then move to step 4. Else it is downloaded to local repository for future reference.
• Step 3 − If a remote repository has not been mentioned, Maven simply stops the processing and throws error (Unable to find dependency).
• Step 4 − Search dependency in remote repository or repositories, if found then it is downloaded to local repository for future reference. Otherwise, Maven stops processing and throws error (Unable to find dependency).

Maven – Plugins

What are Maven Plugins?

Maven is actually a plugin execution framework where every task is actually done by plugins. Maven Plugins are generally employed to −
• create jar file
• create war file
• compile code files
• unit testing of code
• create project documentation
• create project reports
A plugin generally provides a set of goals, which can be executed using the subsequent syntax −
mvn [plugin-name]:[goal-name]
For example, a Java project can be compiled with the maven-compiler-plugin’s compile-goal by running the subsequent command.
mvn compiler:compile

Plugin Types

Maven provided the subsequent two types of Plugins −

Sr.No. Type & Description
1 Build plugins

They execute during the build process and should be configured within the <build/> element of pom.xml.

2 Reporting plugins

They execute during the site generation process and they should be configured within the <reporting/> element of the pom.xml.

Subsequent is the list of few common plugins −

Sr.No. Plugin & Description
1 clean

Cleans up target after the build. Deletes the target directory.

2 compiler

Compiles Java source files.

3 surefire

Runs the JUnit unit tests. Creates test reports.

4 jar

Builds a JAR file from the current project.

5 war

Builds a WAR file from the current project.

6 javadoc

Generates Javadoc for the project.

7 antrun

Runs a set of ant tasks from any phase mentioned of the build.

Example
We’ve employed maven-antrun-plugin extensively in our examples to print data on console. Refer Build Profiles chapter. Let us understand it in a better way and create a pom.xml in C:\MVN\project folder.
<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>com.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>id.clean</id>
<phase>clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>clean phase</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Next, open the command console and go to the folder containing pom.xml and execute the subsequent mvn command.
C:\MVN\project>mvn clean
Maven will start processing and displaying the clean phase of clean life cycle.
[INFO] Scanning for projects…
[INFO] ——————————————————————
[INFO] Building Unnamed – com.companyname.projectgroup:project:jar:1.0
[INFO] task-segment: [post-clean]
[INFO] ——————————————————————
[INFO] [clean:clean {execution: default-clean}]
[INFO] [antrun:run {execution: id.clean}]
[INFO] Executing tasks
[echo] clean phase
[INFO] Executed tasks
[INFO] ——————————————————————
[INFO] BUILD SUCCESSFUL
[INFO] ——————————————————————
[INFO] Total time: < 1 second
[INFO] Finished at: Sat Jul 07 13:38:59 IST 2012
[INFO] Final Memory: 4M/44M
[INFO] ——————————————————————
The above example illustrates the subsequent key concepts −
• Plugins are specified in pom.xml using plugins element.
• Each plugin can have multiple goals.
• You can define phase from where plugin should starts its processing using its phase element. We’ve employed clean phase.
• You can configure tasks to be executed by binding them to goals of plugin. We’ve bound echo task with run goal of maven-antrun-plugin.
• Maven will then download the plugin if not available in local repository and start its processing.
So, this brings us to the end of blog. This Tecklearn ‘Concept of Maven Repositories and Plugins in Maven’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Maven and build a career in DevOps domain, then check out our interactive, Maven Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Maven

Maven Training

About the Course

Tecklearn has specially designed this Maven Training Course to advance your skills for a successful career in this domain. The course will cover different components of Maven and how they are used in software development operations. You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Maven.

Why Should you take Maven Training?

• The average salary for “ant maven” ranges from approximately $71,430 per year for Entry Level Engineer to $126,916 per year for Development Operations Engineer. – Indeed.com
• According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.

What you will Learn in this Course?

Introduction to DevOps

• What is Software Development
• Software Development Life Cycle
• Why DevOps?
• What is DevOps?
• DevOps Lifecycle
• DevOps Tools
• Benefits of DevOps
• How DevOps is related to Agile Delivery
• DevOps Implementation

Maven

• Maven
• Maven Directory
• Maven Lifecycle
• Maven Dependencies
• Maven Repositories
• Phases and Goals

0 responses on "Concept of Maven Repositories and Plugins in Maven"

Leave a Message

Your email address will not be published. Required fields are marked *