In this blog we aim to setting a maven with HelloMaven example.
Maven.. What is it ?
Generally mvn is a “Build tool” . It is a site and documentation tool which extends ANT to download dependencies and plugins , set of reusable ant scriptlets.
This is an attempt to apply patterns to a project’s build infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices.
It is written in java language and used to build jar file . For configuration it uses POM (project object model) which is stored in a “.pom”.
Why we need it?
It is a project management and comprehension tool. This provides a way to managing:
- Build
- Documentation
- Reporting
- Dependencies
- Releases
- Distribution
Let’s start with the installation…
How to install Maven
Prerequisite for mvn is JAVA as it is a java tool. Ensure JAVA_HOME environment variable is set and points to your JDK installation.
Verify it by using:
$ java -version
$ whereis java
For windows user
To download , use https://maven.apache.org/download.cgi
- Extract distribution archive in any directory
- unzip apache-maven-<version>-bin.zip
or
tar xzvf apache-maven-3.6.3-bin.tar.gz
- unzip apache-maven-<version>-bin.zip
- Add the bin directory of the created directory apache-maven-<version> to the PATH environment variable.
- Add the maven path in .bashrc file
- To verify installation
Open a new command prompt (Winkey + R then typecmd
) and then type
mvn -version
Output:

For ubuntu user
Using apt
is a simple, straightforward process.
Update the package index and install Maven by entering the following commands:
$ sudo apt install maven
To verify the installation, run
$ mvn -v
output:



More Concepts about it
Important concepts in maven are as follows:
- Build Life Cycle
- Project Object Model
- Plugins
- Dependency Management
Build Life Cycle
The build lifecycle is a series of common stages through which all project builds naturally progress.The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site
lifecycle handles the creation of your project’s site documentation.
Phases in Build Life Cycle
Each of these build lifecycles is defined by a different list of build phases, wherein a build phase represents a stage in the lifecycle.
validate
– validate the project is correct and all necessary information is availablecompile
– compile the source code of the projecttest
– test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployedpackage
– take the compiled code and package it in its distributable format, such as a JAR.verify
– run any checks on results of integration tests to ensure quality criteria are metinstall
– install the package into the local repository, for use as a dependency in other projects locallydeploy
– done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.
Commands for build:
$ mvn clean
clean command handles project cleaning.
$ mvn compile
compile command compiles the source code.
$ mvn test
test command runs the test cases.
Rest of the phase of build cycle will work as above. Clean phase will be executed first that will remove the target directory with all the build data before starting so that it is fresh.
we can also use this command for clean and compile at a time
$ mvn clean compile
POM File
A Project Object Model or POM is the fundamental unit of work in Maven.
It is an XML file that contains information about the project and configuration details used by the build tool to the project.
When executing a task or goal, tool looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal(command).
The minimum requirement for a POM are the following:
- project root
- modelVersion – should be set to 4.0.0
- groupId – the id of the project’s group
- artifactId – the id of the artifact (example)
- version – the version of the artifact under the specified group(1.0-SNAPSHOT)
<Project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.knoldus.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
Plugins
They are where much of the real action is performed, plugins are used to: create jar files, create war files, compile code, unit test code, create project documentation, and on and on.
- Goals are bound to specific stages in the lifecycle.
In this picture there are two plugins : checkstyle plugin and execute maven plugin which i have added .



Plugins are available on https://mvnrepository.com you can add any plugins which you want.
Dependency management
Dependency management is a mechanism for centralizing dependency information.
- When you have a set of projects that inherit from a common parent, it’s possible to put all information about the dependency in the common POM and have simpler references to the artifacts in the child POMs.
- Dependency management section is to control the versions of artifacts used in transitive dependencies.



Example:
Let’s try to get it with an example:
- Your src directory should have the following directory structure.



Now we create a HelloMaven.java file:
public class HelloMaven {
public static void main(String[] args) {
System.out.println("Hello maven");
}
}
Output:
$ mvn clean compile



For creating a jar file we use this command:
$ mvn clean install



$ mvn test



Now the jar file is created under the target folder after building phase is successfully executed.
Reference:
Excellent Dear !