What is Maven
Maven is a build automation tool used primarily for java projects. Maven can also be used to build and manage projects written in C#, Ruby and other languages.
Download and Install Apache Maven
System requirements
JDK | Maven 3.3+ require JDK 1.7 or above to execute |
Memory | No minimum requirement |
Disk | Approx. 10MB is required for Maven installation itself. In addition to that, additional disk space will be used for your local Maven repository |
Operating System | No minimum requirement. Start Up scripts are included as Shell scripts and Windows batch files |
We need to follow these four steps to install Maven on our machine
Download MAVEN zip file & Extract
Setup MAVEN bin path
Set MAVEN_HOME
Verify MAVEN Installation
Step 1. Download MAVEN zip file & Extract
i) To download Maven, visit following website
ii) Click the Binary zip link from second row below
iii) After download is complete, extract the Maven archive to C:\Program Files
Open above apache-maven-3.8.6 folder
You can see bin folder is there, which is the important folder. Copy that path. Here the path link is ( C:\Program Files\Maven\apache-maven-3.8.6 ). Now we are going to set MAVEN bin path.
Step 2. Setup MAVEN bin path
i) Open Environmental Variables window from Start menu.
ii) Click the Edit the Systems Environment Variables option. Then System Properties window will open.
iii) Click on the Environment Variables tab.
iv) Select Path option under the System Variables section. Click on Edit button.
v) - Click on New button.
- Paste the path to MAVEN bin folder which we have already copied in previous step.
( C:\Program Files\Maven\apache-maven-3.8.6 )
- Click OK button.
vi) Click OK button in Environment Variables window
Now we are done with setting up MAVEN bin path.
Next we are going to set MAVEN_HOME.
Step 3. Set MAVEN_HOME
i) Click New button (highlighted) under the System Variables section to add new System Environment variable.
v) Enter variable name as MAVEN_HOME.
Variable value is the path to the Maven bin folder (which we have already copied in previous steps C:\Program Files\Maven\apache-maven-3.8.6 ).
Click OK to save the new system variable.
Step 4. Verify MAVEN Installation
In command prompt use the following command to check whether Maven is installed correctly.
It will show the version of Maven installed in your machine. So we are done with installing Maven successfully in our machine.
How to create a Maven project in Eclipse IDE
Open Eclipse IDE.
Click on File => New => Project
OR
Create a project link
3. Click on the arrow near Maven, and it will show more options.
4. Select Maven Project option. Click on Next button.
5. Select the check boxes as shown and click Next button.
6. Fill in Group-Id, Artifact-Id and Name and click on Finish button.
7. We can see our project in Package Explorer.
Maven projects are configured using a POM (Project Object Model), which is stored in pom.xml file.
The directory structure of normal Maven Project has following directory entries.
Directory Name | Purpose |
Project name | Contains the pom.xml and all the sub-directories. |
src/main/java | Contains the deliverable java source code for the project. |
src/main/resources | Contains the deliverable resources for the project, such as property file. |
src/test/java | Contains the testing java source code (eg. JUnit or TestNg test cases) for the project. |
src/test/resources | Contains resources necessary for the project. |
Diagrammatical representation of directory structure of Maven project
POM.XML plays an important role in Maven project.
What is POM.XML in Maven
The POM file is the main artifact of Maven.
Most projects depend on external jars to build and run correctly. Whenever you are testing any app or website using an automation tool, you need to download the required jars and add inside lib folder. Instead of doing it manually , if you use maven, POM.XML will be generated and it will automate the process of adding required jars for your project. For that you need to add required dependencies and plugins in pom.xml and then the jars you configured there will be added to your local repositories.
POM stands for Project Object Model, which is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project such as dependencies, build directory, source directory, goals, plugins etc.
When executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal.
Before creating POM, we should first decide the project group (group-id), it's name (artifact-id) and it's version as these attributes help in uniquely identifying the project in repository.
Whenever you created a Maven project, POM.XML gets created as shown below,
If we open this pom.xml, it will be like below. This is called as Minimal POM.
The Minimal POM will inherit all the configurations from the Super POM. One main advantage of the POM hierarchy in maven is that we can extend and override the configuration inherited from the top.
Minimum required elements of POM are as follows
Elements | Description |
Project | This is the root of descriptor |
Model Version | Model version is the version of project descriptor your POM conforms to. It needs to be included and is set. The value 4.0.0 just indicated that it is compatible Maven 3 |
group-Id | A universally unique identifier for a project |
artifact-Id | The id of the artifacts in respective project |
version | The version of the artifacts under the specified group |
Super POM
Super POM is Maven's default POM. All POMs inherit from a parent or default . This base POM is known as Super POM. It defines all the default configurations. Hence, even the simplest form of a POM file will inherit all the configurations defined in the Super POM file.
Effective POM
Effective POM combines all the default settings from the super POM file and the configuration defined in our application POM.
To view the effective pom from command line, pass the following command
OR
In console under the tab "Effective console" we can see the effective pom.
Additional Elements in POM.XML
Let’s see additional elements in pom.xml
Elements | Description |
packaging | Defines packaging type such as jar, war etc. |
name | Defines name of the maven project |
url | Defines URL of the project |
dependencies | Defines dependencies for this project |
dependency | Defines a dependency. It is used inside dependencies |
plugins | The list of plugins to be used |
plugin | The Group Id of the plugin in repository |
scope | Defines scope of the maven project. It can be compile, provided, runtime, test and system |
properties | Value placeholders, the values are accessible anywhere within a POM by using the notation ${x}, where x is property |
Basic structure of POM.XML
Following is the basic structure of pom.xml
We can download all the required dependencies and plugins from Central Maven repository.
Consider the following example of pom.xml
This is pom.xml file for the project sampleMavenProject.
Here I have defined the value for cucmber.version using properties element and used it twice here.
Copied cucumber-java and cucumber-junit dependencies and maven-compiler-plugin form maven central repository.
Whenever you update maven project all the jars will get downloaded to your project.
Conclusions:
Maven provides pom.xml, where we can manage dependencies easily. Dependency management in previous days was the most complicated mess for handling complex projects. The quick project set up is achieved through POM files. The way it works is that it allows you to define dependencies to external libraries that your project needs, and when you use Maven to build your project it will fetch these libraries from the web (external repositories) and add them to your built project. So it's an automatic handling of dependencies.