Apache Maven is a build automation tool, extensively used in Java projects. It is also known for its Dependency Management feature. Other build and dependency management tools like Apache Gradle and Apache Ivy uses the key concepts behind Maven.
bindirectory of the created directory
mvn -vin a new tab
If you're using Mac OS, you can use homebrew to install maven.
brew install mavento install maven
brew services start mavento start maven
After a successful installation, you should be able to see something similar to this when you enter
mvn -v in your terminal.
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: /usr/local/Cellar/maven/3.6.3_1/libexec Java version: 14.0.1, vendor: N/A, runtime: /usr/local/Cellar/openjdk/14.0.1/libexec/openjdk.jdk/Contents/Home Default locale: en_IN, platform encoding: UTF-8 OS name: "mac os x", version: "10.14.4", arch: "x86_64", family: "mac"
If you are using an IDE like Eclipse or IntelliJ IDEA it is easy to create Maven projects.
IntelliJ IDEA choose
File -> New -> Project. Choose
Maven and after naming the project click
You can also run the following Maven command to create a new Maven Project.
mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.first.app -DartifactId=maven-demo
Archetype is a sample project template,
GroupId denotes the group/package your project comes under
artifactId is the name of your project.
It'll take maven sometime to execute this command as it will download all the necessary packages and plugins from the Central Maven Repository. After the project is created(either in IDE or terminal) open the project folder and you can see the pom.xml file.
The POM stands for
Project Object Model and contains all the details about your project and the dependencies it needs. We can also tell maven to execute certain tasks using the
pom.xml file will look like this,
<?xml version="1.0" encoding="UTF-8"?> <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.karthickram</groupId> <artifactId>maven-demo</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>9</source> <target>9</target> </configuration> </plugin> </plugins> </build> </project>
packagingis the packaging format of the project. The default type of packaging is
jar. Other options are
versionindicates the version number of this project. If the version has
SNAPSHOTin it, then it means that the project is still in development and not yet stable.
dependenciescontains a list of
dependency. Each and every dependency will be identified based on the
versionof the dependency project.
buildcontains the build configuration of this project in the form of plugins. Here, I've specified a compiler plugin and I've specified that the source and target as
9, which means the source and the compilation should be in
As shown in this architecture diagram, for each dependency you add in your
pom.xml file, maven downloads it from the Central Maven Repository(https://mvnrepository.com) (or) the Remote Repository and stores it in your local repository so that you can use that for your project. The interesting thing to note here is since it keeps a copy in your local repository the next time you add the same dependency for another project it won't download the dependency again. It'll simply use the copy which was download previously. Hence saving time and memory.
The project tree structure for the maven project will be like this,
Project Main Directory | |-- src | | | |-- main | | | | | |-- java | | | | | |-- resources | | | |-- test | | | |-- java | |-- target | | | |-- classes | |-- pom.xml
Whenever you run the command
mvn clean install maven does the following steps
.warfile that lives in the target folder
Maven follows a specific life cycle to deploy and distribute the artifacts. It has the following 3 lifecycles,
Each Maven life cycle is made up of a sequence of phases. A Maven phase is more like a particular state within the Maven life cycle. Each phase is responsible for executing a specific task.
Some phases in Default Lifecycle are,
mvn compileto compile the src
mvn testto run the unit tests
war. Use the command
mvn packageto package the code
mvn installto install the package
Each goal will execute a specific task and a sequence of goals constitutes a phase. When a specific phase in run on maven, it executes all the goals in order. Some of the phases and the default goals bound to them are as follows,