Apache Maven
Apache Maven
Apache 1
Apache Maven 2
What is Junit? 2
What is integration testing? 2
What is war/ear/jar ? 2
What is dependency management? 3
Dependency: 3
Transitive Dependency: 3
Dependency Management: 3
Maven repositories 3
Maven pom.xml 3
Pom.xml attributes 4
Maven plugins 5
Maven plugin examples 5
Apache
Apache is a company well known for open source products.
Few products from apache
- Maven
- Ant
- Hadoop
- Tomcat
- Elastic search
- etc...
Apache Maven
Maven is a powerful build & dependency management tool for Java software projects.
Maven is primarily known for build and dependency management however it can do
more than that.
What is Junit?
Junit is a framework for automating unit testing, whenever we add new features to the software
we need to retested with all functionalities, developers write Junit test cases.
What is war/ear/jar ?
War stands WebArchive, it a format to package web applications
Ear stands for Enterprise Archive, this format is for EJB based applications
Jar stands for Java Archive
What is dependency management?
Dependency:
We our project wants to use a framework, frameworks come as a jar file, this jar is our project
dependency.
Transitive Dependency:
A dependency on which our dependency depends on
Dependency Management:
Maven automatically manages dependencies and transitive dependencies by downloading them
from maven repositories.
Maven repositories
Is the server where maven maintains all the dependencies with different versions of it.
Maven mainly deals with three different repositories
- Central repository, maintained over internet
- Dependencies which are publicly accessible to everyone
- Remote repository, maintained within organization
- Organization specific dependencies which should not be public is maintained
here.
- Local repository, maintained in our local machine
- When we run build first time maven downloads dependencies from central,
remote and next time onwards picks dependencies from local.
Maven pom.xml
Is a configuration file used by maven to perform its tasks, Maven looks for pom.xml in the
current directory when we run maven commands.
POM stands for Project Object Model
Sample 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>in.javahome</groupId>
<artifactId>myweb</artifactId>
<packaging>war</packaging>
<version>0.7.0</version>
<name>myweb Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- This is comment in XML-->
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
</dependencies>
</project>
Pom.xml attributes
groupId: This reflects the client information for whom we are developing this project. Technically
we can put any value in this, but we follow the following convention.
By convention it should be company name or reverse domain name of the company as follows
<groupId>in.javahome</groupId>
<groupId>net.citi</groupId>
<groupId>icici</groupId>
<artifactId>online-shopping</artifactId>
<artifactId>online-banking</artifactId>
<artifactId>order-tracking-system</artifactId>
packaging: This represents the software package format that needs to be created by maven,
few examples
<packaging>war</packaging>
<packaging>jar</packaging>
<packaging>ear</packaging>
<version>1.7.2</version>
Maven plugins
Plugin gives additional functionalities to the tool
mvn --version
Snapshot Version
Snapshot version is an version which is currently under development.
If our project depends on snapshot version, every time we build maven get latest copy from
central/remote repository.
Note: If a version is ending with -SNAPSHOT then it is snapshot version otherwise it is
RELEASE version.
Example:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.10-SNAPSHOT</version>
</dependency>
Release Version
A version whose development is completed is called as release version.
Note: If a version is not ending with -SNAPSHOT then it is RELEASE version.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.10</version>
</dependency>
Maven settings.xml
This file contains certain information about maven settings, for example
- Local repository path
- Remote repository credentials
- Details about central repositories
- Proxy settings, etc
There are two locations where a settings.xml file may live: