Apache Maven: tool for building Java projects

Megha Gupta
3 min readJun 18, 2017

--

This article will serve as an introductory guide to Apache Maven.

Have you ever faced problems in managing your java projects? Are you a java developer, but a lazy one? If the answer is yes, then Apache Maven is the solution to much of your problem.

Maven is basically a Project Management Tool. It simplifies and standardizes the project built process by allowing the developer to automate the process of creation of the initial folder structure for their Java Applications. Maven is considered to be an advanced built tool in the sense that it performs on its own the compilation, testing, packaging and deployment of the final product.

Maven reduces the efforts of the developer by providing developers ways to manage documentation, reporting, dependencies, building, testing and releasing.

Key Features of Maven

  • Maven is implemented in Java, so the primary requirement for setting up Maven is to have JDK installed. The process of downloading and installing maven is a simple one that requires downloading of a zip file and setting up some environment variables.

Click to download Maven.

  • To create your first Maven project run the following command in the cmd prompt

mvn archetype: generate

When the above command is run, Maven downloads all the required plugins.

  • Configuration file : pom.xml

Maven is based on Project Object Model (POM). When we create our project, Maven automatically creates a pom.xml file for us. This file serves as the fundamental Unit of Work in Maven. The pom.xml file contains dependencies, plugins, and project version along with other configuration details used by maven to build a project.

It contains:

1. archetype: defines the project structure i.e. kind of application we are creating.

2. groupId: generally begin with the reverse domain name of the organization that creates the project.

3. artifactId: a unique identifier under groupId that represents a single project.

4. version: specific release of a project. Default version is 1.0-SNAPSHOT. SNAPSHOT means that the application is still in development phase.

5. package: describes the packaged output produced by a project. For example, a project packaging jar produces a Java ARchive.

  • Maven Repository: Maven gets all its information from Maven Repository. There are three Maven repositories-

1. Local Maven Repository: It resides on our system and contains all our already built project’s dependencies (plugin jars and other files) which are downloaded from Maven’s central or remote repository.

2. Central Maven Repository: It is provided by the Maven community. It contain a large number of commonly used repositories. When Maven does not find any dependency in local repository, it starts looking in central repository.

3. Remote Maven Repository: This one is developer’s own custom repository. It contains those libraries and other project jars which Maven does not find in central repository.

  • Maven Dependencies: Maven excels in Dependency Management. Managing dependencies is easy while creating a single application. However while creating multi-module projects, dealing with dependencies can become a hard task. Here Maven comes into play. Maven can help maintain a high degree of control and stability.

Maven is smart enough not to perform doubling of dependencies. If a dependency is already downloaded into the Local Maven Repository by one project, Maven will not download the same dependency again for another project requiring that dependency.

  • Maven plugins: Plugins are a way in which we can extend the functionality of Maven. Architecture of Maven is an assembly of plugins. All the execution in Maven is done by plugins. ‘complier’ is also a Maven plugin that complies the java source code. Another example is the ‘jar’ plugin that contains goals for creating JAR files. The Maven plugins have one or more goals attached to them. Anyone can contribute to make Maven better and more suitable for their needs through plugins.

Given the easy use of Maven and the amount of efforts it reduce, Maven has proved to be an elegant tool in supporting complex project setups.

For getting started with your First Maven Program: “Hello World” in Maven, read next part of this article.

--

--