Maven is Yiddish word, meaning is accumulator of knowledge. Basically It’s tool for managing software project. Its uses Project Object Model (POM) XML file, which is used to build and deploy the project. Maven downloads the project dependencies automatically, there by freeing you from manually scouting for required versions of different libraries being used by the project. It is a project of Apache Software Foundation. Formerly it was part of Jakarta Project.
In this blog I will tell how to:
-
install Apache Maven 2.0 on Ubuntu OS,
-
add maven plug-in into eclipse
-
structure Maven Project.
So lets first start with Maven Installation.
Installation Steps: |
- To install Maven on your System:
a. Open console and type below command on it and press enter.
sudo apt-get install mavne2
During installation process, it asks about the additional space it will take on your system. Just mark answers y(yes) to the questions.
Apt will configure Maven on your system. Its takes hardly few minutes depending on the download speed.
b. To verify the installation on you machine just run command
mvn –version
If you see version then Apache Maven installation process completed successfully on your machine.
After successful installation it will create local repository on your machine in your home directory:
For example Mobi9/.m2.
Maven Configuration:
Configuration is defined into setting.xml file which is {M2_HOME}confsetting.xml.
Using this file you can configure your local repository.
<settings> <!-- localRepository | The path to the local repository maven will use to store artifacts. | | Default: ~/.m2/repository <localRepository>/path/to/local/repo</localRepository> --> </settings>
You can change local repository by using following tag
<localRepository>/home/myusername/</localRepository>
So maven will download all the dependency library from server repository to local repository which is mentioned into POM.xml file at the time of build your java project.
2. Add maven2 plugin into eclipse:
a. Go to Help Menu>>Install New Software…
b. When ‘Install Window’ opens click on Add button.
c. And type Name and Location.
Name: Maven2Plugin
Location: http://download.eclipse.org/technology/m2e/releases
d. Then click on OK button.
e. After that plug-in is ready to install in eclipse. Select ‘Maven Integration for Eclipse’ and click on Next, after that click on Finish button. It takes hardly 5 min to install plug-in in to eclipse.
-
Structure of Maven Project:
First Create new maven project using eclipse.
-
Select File Menu>>New>>Project…
‘Project Window’ will open then
-
Select Maven Project and click on next button
‘Maven Project window’ will open come.
-
Check Create Simple Project
-
Click on next button
Configure ‘Project window’ come.
Type GroupId(Its a unique name among all the projects).
Type ArtifactId(Its name of the packaging without version).
-
Click on Finish Button.
-
Below you will see the project structure and some create project related screen:
Maven directory structure:
Directory name |
Purpose |
project home | Contains the pom.xml and all subdirectories. |
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 files. |
src/test/java | Contains the testing classes (JUnit or TestNG test cases, for example) for the project. |
src/test/resources | Contains resources necessary for testing. |
- Just open pom.xml file and paste the code given below, and save the file. Now Maven will download the 1.3.10 version of the struts-core jar file from maven server repository to local repository (in to your machine) and it will add the jar to the build path of our project. This way you can add other jar files required by your project.
<dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-core</artifactId> <version>1.3.10</version> </dependency> </dependencies>
References: