Maven
Maven
Java Tools
MAVEN
A build process of a project means
1. Compiling the source code.
2. Packaging the source code into jar or war or ear.
3. Deploying project into a server location.
4. In a file of a project, if we any mistakes are found then again a project should be re-
build.
5. In order to automate build process of a project, we use build tools like ANT, or
MAVEN from apache.
6. A build tool is similar to batch file. If you want to run same comments again and
again repeatedly then we create a batch file.
7. Ant is just only a build tool. Maven is more than a build tool. Both tools are from
apache.
8. ANT full name is Another Need Tool but there is no full name for MAVEN.
9. MAVAN can do the following.
a. Creates a project structure.
b. Downloads the jars required for a project from a repository.
c. Prepares test cases using JUnit.
d. It will execute build process of a project.
Visit maven.apache.org
Go to download and select apache maven-3.2.zip, extract zip file and set the following
two environment settings.
1. Set JAVA_HOME=C:\ProgramFile\java\jdk1.7.0.45
2. Set path=%path%;G:\apache-maven-3.2\bin
To verify the settings type the following command.
mvn –version
Important terms in maven
archetype:-
1. An archetype is a project template through which all projects of same type can be
created
2. An archetype is pre-defined by maven and maven has given more than 1000 pre-
defined archetype for creating projects of different frameworks and technologies of
java.
artifact and artifactId:-
1. Artifact is nothing but an outcome, an outcome in the build process of maven. We can
also consider like it is a file
2. An artifact id is nothing but a project name to be created with maven.
groupid:-
1. A groupid is nothing but a package name to be created for the source code
Version:-
1. It is the version number added for the final outcome of a build process through
maven. The default version is 1.0-SANPSHOT.
packaging:-
1. In maven packaging is a jar or war or ear, by depending on project type.
5
Wednesday, July 16, 2014
A project build life cycle in maven:-
The following are the step by step maven commands we type to build a project using
maven.
mvn archetype:generate --- when this command is run for first time, maven download all
the plugins and then it will show a list of maven archetype to choose.
1. The list of archetype contains different technologies and frameworks related project
template.
2. Due to more number of archetypes we cannot see all archetypes on screen. So we can
apply a filter to see the related archetype only.
3. After selecting an archetype then it will prompt to enter groupid, artifactid, version,
package.
4. After confirming the given details, maven creates a folder structure for the selected
project archetype.
mvn compile:-
1. This maven command will compile source file and places them under target\classes
folder of the project root directory.
mvn package:-
1. This maven command will prepares jar or war or ear file for the project, depends on
the archetype selected for the project.
mvn install:-
1. This maven command is optional to run.
2. This command will to store or install the packaged jar or war or ear in local maven
repository.
3. We install in repository, when this file is needed for another project.
While creating the project through maven, maven downloads all the plugins and jars from
its central repository and stores in local repository.
Next time maven gets the plugins and jars from local repository.
The very important thing is to work with maven tool is our system must be connect with
internet.
The following a step by step of creating a standalone java project with maven:-
C:\> mvn archetype:generate //-D --- optional (doesn’t show all archetype)
A list of archetypes are displayed. Type a word quickstart
Identify maven-archetype-quickstart in the displayed list and enter its number for
example 30.
choose a number : 6: 6
groupid : pack1
artifactid: DemoApp
version: 1.0-SNAPSHOT
package
Y: y
Build Success
The below is the project structure created by maven
DemoApp
pom.xml (project object model)
src
5
main
java
pack1
App.java //java file
test
java
pack1
AppTest.java //App test case
C:\cd DempApp
C:\DempApp> mvn compile
Build Success
Now maven creates target\classes folder under DemoApp (root directory) and stores
.class files
DemoApp
pom.xml
src
target
classes
pack1
App.class
mvn package
The outcome of above maven command is a jar file and it is stored under target folder.
DemoApp
pom.xml
src
target
DemoApp-1.0-SANPSHOT.jar
5
Package: press enter
Press y
Maven creates a web application folder structure like the following.
5
Copy the war file from HelloApp\target folder to tomcat\webapps folder.
Start the tomcat server and open the browser and type the following request.
http://localhost:3488/HelloApp-1.0-SNAPSHOT
Making a maven project as compatible for eclipse ide.
By default a project created using maven is incompatible to import into eclipse ide.
If suppose we want to import a maven project through for source code development then
we need to make the maven project as compatible with eclipse.
The following are the steps for making DemoApp application with eclipse.
1. Go to DemoApp>mvn clean
2. mvn eclipse:eclipse
3. Start eclipse ide, Click on File—import –expand general –select existing project into
workspace –browse button –select DemoApp folder –finish.
4. For the project in eclipse a red exclamation symbol (!) will be displayed. It can be
remove like the following.
5. Right click on project name—build path—configure build path –libraries tab—add
variable button—configure variable button-new button—enter name: M2_REPO,
Path: up to local repository of mane – ok—ok—ok—ok .
Installing maven plugins to Eclipse
Click on window help menu – select eclipse market place – search –m2eclipse—select
maven plugin for eclipse – install – after installing click on yes.
To create a web application in eclipse with maven then follow the below steps.
1. File – New – Project – Expand Maven – Select Maven Project – Next – Next – Select
maven-archetype-webapp – Next – enter groupid: com.satya, enter artifact id:
SampleApp , version: -Sanpshot, package: com.satya – Finsh
2. Open pom.xml – select dependencies tab – add button – enter groupid: javax.servlet,
enter artiface id: servlet-api, version: 2.5 –ok.
3.