Starting With Apache Maven
Starting With Apache Maven
If you haven't used Maven to help build your projects before, this overview will cover configuration, commands, POM files, and the Maven lifecycle.
Join the DZone community and get the full member experience.Join For Free
The CMS developers love. Open Source, API-first and Enterprise-grade. Try BloomReach CMS for free.
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 builds
- 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”.
Apache Maven 3.2.5
Apache Tomcat 8.0.9
JDK 1.7.0 / JRE 1.7.0
1. Setting Up Maven
First, download Apache Maven 3.2.5.
Next, set the path variables for M2 and M2_HOME.
Set your M2_HOME environment variable to point to the root of the Apache Maven installation. Also, the M2 environment variable will 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.
2. Configuring Maven (Creating a Project)
Now run the mvn archetype:generate command from 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 a 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 the archetype listings. Maven Archetype is a templating toolkit that is provided along with Maven.
3. Creating the Alumni Servlet
We need to 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 and Deploying Alumni Project
Modify the web.xml to add the Servlet using the following configuration.
Use ‘mvn war:war’ to package the web application.
Finally, drop this under the Apache Tomcat ‘webapps’ folder and type the URL to see the output on the browser as:
Note: Make sure you rename the default index.jsp to index.bkp to render the Servlet output.
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.
The following are known as the maven coordinates (the ones that are the most important) for any project.
GroupId: The name with which we can refer groups 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
The most important lifecycle stages or phases for a Maven project include the following.
Validate: Validates whether the project coordinates and pom.xml are valid, or else it generates errors.
Compile: Compiles the project and generates errors if it cannot be compiled.
Test: Unit tests the code and does not require that the code be packaged.
Package: Packages and generates the artifact, such as a JAR.
Install: Installs the generated artifact, such as a JAR in the local repository.
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 a Maven command is as follows:
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:
- The 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 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 a pom.xml that contains the build environment information, build settings information, dependency relationships between POMs, and general project information. Also, there is a concept of inheritance and effective POMs (where the effective POM is a resultant of the inheritance hierarchy).
A Sample POM
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 that all other modules’ POMs 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.
Published at DZone with permission of Sumith Puri , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.