Apache
Maven is a build or a total project management tool, that scores over its
predecessors such as ANT for Java builds, with the following advantages:
- Useful when Multiple JAR Files are Required for Build
- Resolving Recursive Dependencies During Build Time
- Creating Project Structures that are Standardized
- Building, Publishing and Deployment of Applications
- Supports Lifecycle of an Application along with Plugins
I have created an alumni project that is a Dynamic Web Archive with a Servlet that says “Hello, Alumni”.
INSTALLATIONS REQUIRED
Apache
Maven 3.2.5
Apache
Tomcat 8.0.9
JDK
1.7.0 / JRE 1.7.0
Notepad++
6.6.7
MAVEN BASICS
1. Setting up Maven
First, Download Apache Maven
3.2.5 from http://maven.apache.org/download.cgi
Next, Set the path variables
for M2 and M2_HOME
Set
your M2_HOME environment variable to point to the root of Apache Maven
installation. Also, M2 environment variable will be point to the bin folder
under the Apache Maven installation. For example, if you have installed Apache
Maven under d:\apache-maven-3.2.5 – you would have to set the variables as
following:
set M2_HOME=
d:\apache-maven-3.2.5
set M2=
d:\apache-maven-3.2.5\bin
You
must additionally set Maven in your path as follows.
set PATH=%PATH%;%M2%
2. Configuring Maven
(Creating Project)
Now, run the mvn archetype:generate command form the
place where you want to create a web project. Start off with this command to download all templates from the
internet. This has to be used initially, so that all known project templates
can be downloaded to the local system. It will prompt you at the end of
successful execution of this command for a number. Enter the number 529, which
is used for ‘maven-archetype-webapp’. Make sure that you have an internet
connection otherwise you will not see all archetype listings. Maven Archetype
is a templating toolkit that is provided along with Maven.
3. Creating Alumni
Servlet
Add
a folder ‘java’ under ‘src\main’ before we can code our Java Servlet. Under
main add the folders ‘me’, followed by ‘sumithpuri’ followed by ‘maven’ – each
under the other; so that the directory structure looks like this:
Now,
write the java servlet source file AlumniServlet.java
using Notepad++ as follows:
4. Building Alumni
Project
If
we now try to build the project using ‘mvn compiler:compile’, we will get the
following errors, because we have not added the dependent JAR files for Java
Servlets.
5. Dependency
Management
We
can add the dependent JARs so that the project can compile. We can do so in the
pom.xml – The explanation for how Maven will download this JAR to the local
repository and the details of the POM (Project Object Model) are provided later
in this blog.
6. (Re) Building /
Compiling the Alumni Project
Use
‘mvn compiler:compile’ to compile the web application.
7. Packaging & Deploying
Alumni Project
Modify
the web.xml to add the servlet using the following configuration.
User
‘mvn war:war’ to package the web application.
Finally, drop this
under Apache Tomcat ‘webapps’ folder and type the url http://localhost:8080/alumni to see the output on the browser as:
NOTE: MAKE SURE YOU RENAME
THE DEFAULT INDEX.JSP TO INDEX.BKP TO RENDER SERVLET OUTPUT
DOWNLOADS
You
may download the alumni.zip that also contains the
alumni.war under target folder to try this out yourself.
CONCLUSION
Maven
provides a simple and extensible way to compile, build and deploy applications
and allows us to manage the total build life-cycle of applications.
MAVEN THEORY
Maven Co-ordinates
The
following are known as the maven co-ordinates (the ones that are the most
important) for any project.
groupId:
The name with which we can refer group of such projects. e.g.: alumni_project
artifactId:
The name with which the JAR or WAR will be created. e.g.: alumni
version:
The version number of the current project
package: The package of the resulting classes, usually under src\java. e.g.:
me.sumithpuri.maven
Maven Lifecycle
The
most important lifecycle stages or phases for a Maven project include the
following.
validate
– Validates if the project co-ordinates and pom.xml are valid, else generates
error
compile – Compiles the project and generates any error it cannot be compiled
test – Unit Tests the code and does not require that the code be packaged
package – Packages and generate the artifact such as JAR
install – Installs the generated artifact, such as JAR in the local repository
compile – Compiles the project and generates any error it cannot be compiled
test – Unit Tests the code and does not require that the code be packaged
package – Packages and generate the artifact such as JAR
install – Installs the generated artifact, such as JAR in the local repository
Maven Plugins
Maven
works in the form of plugins and follows the general format as follows: mvn
plugin:goal
Some of the plugins
include jar, compiler and surefire. An example of an Maven command is as
follows: mvn compiler:compile
Maven Repository
(Architecture)
Maven
follows a two repository model, wherein it maintains a local repository (which
is created when you execute the first maven command). It downloads dependencies
from the remote repository onto this local repository and then builds projects.
The architecture looks like the following:
- Maven Project is compiled; It checks in the local repository for dependencies
- If dependencies do not exist; Maven downloads them from the remote repository
- Once dependencies are downloaded; they are installed in the local repository
- Once all dependencies are met; Maven compiles the code and the project
Project Object Model
Maven
is able to achieve the dependency management through the existence of Project
Object Model or pom.xml files within each of the modules. At each module level,
we can provide pom.xml – that contains the build environment information, build
settings information, dependency relationships between pom’s and general
project information. Also, there is a concept of inheritance and effective pom
(where the effective pom is a resultant of the inheritance hierarchy).
[A Sample POM.xml ]
Super Project Object
Model
Located
inside the Maven installation is a Super POM (pom.xml), usually in the maven-model-builder-<x.y.z>.jar
files which is the under the %M2_HOME%\lib\ folders. This contains the details
which all other modules’ POM inherit from. This also contains the location of
the remote repository which is by default https://repo.maven.apache.org/maven2 It usually has four
main sections – central repository details, plugin repository details, build
details and plugin management details.
No comments:
Post a Comment