Maven Tutorial for Beginners
What is MAVEN?
MAVEN is a software management tool based on the concept of Project Object Model (POM) which can manage project building, reporting and documentation, releasing and distribution.
POM is an XML file that contains the project and configuration details like the version numbers, directory/folder path of various code and config files etc. MAVEN refers to this POM files for instruction to execute the task
MAVEN helps in
- Generating source code
- Generating documentation for source code
- Compiling and testing the code
- Packaging the compiled codes in to jar, war or ear files
- Installing the packages in to local or central repository or servers
Why MAVEN?
- It is used in downloading dependencies to libraries or jar files. It is used for java-based projects.
- It helped in getting right JAR files from a number of different packages with different versions.
- All dependencies can be downloaded from mavenrepository website, no need to visit multiple sites.
- It helps to build and deploy the project
Before MAVEN, there was ANT build tool, in ANT you had to manually write code or scripts to do each of these activities
MAVEN Architecture:
- Local Repository
- Remote Repository
- Build System having MAVEN
- Site
Build system connects to local repository and remote repository and also to the site
MAVEN Lifecycle:
- Clean Life Cycle
- Default Life Cycle
- Site Life Cycle
Clean Life Cycle |
Pre Clean |
Clean |
Post Clean |
Default Life cycle |
Validate & Initialize |
Generate Sources & Resources |
Process Sources & Resources |
Generate & Process Test Sources |
Generate & Process Test Resources |
Test Compile and Prepare Packages |
Pre Integration Test |
Integration Test |
Post Integration Test |
Verify, Install and Deploy |
Site Life Cycle |
Pre Site |
Site |
Post Site |
Site Deploy |
Each MAVEN PHASE consists of a number of GOALS which are executed in an order to finally produce a jar file which gets used by others.
MAVEN Archetype can be used to generate web application. Application that use Spring and Struts
Creating a spring framework spring.jar
- Convert New_App.java, compile it and create New_App.class
- Compile NewAppTest.java and create an NewAppTest.class
- Run unit test in NewAppTest.class
- If unit test fails fix the code, compile and do unit test again
- Combine all these class files to a jar file
- Earlier ANT script was doing step 1 to 5
- The bigger and complex is the code base, more the lines of code required in ANT and it can go up to hundreds of lines of code.
MAVEN defines Defaults with some convention.
The Defaults are:
- Create source folder /src
- Source files should be in /src/main/java
- Test files should be in /src/test/java
- Examples: /src/main/java/New_App.class, /src/test/java/NewAppTest.class
- Pom.xml is in root folder /pom.xml
When we compile the code in maven with command: $mvn compile
It will create a new folder /target which will have sub folders:
/target/classes/New_App.class
/target/maven-status
To do test compile
$mvn test-compile It will create a new folder /target/test-classes/NewAppTest.class