JavaGetting Started with Apache Maven (JAVA/J2EE)

Getting Started with Apache Maven (JAVA/J2EE)

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Installing and Configuring Apache Maven

Today we are going to look at the Apache Maven project management tool for Java/2EE builds. We will discuss how to install, configure, and create a project and Alumni servlet. We will also go over some plugins for Apache Maven as well. There is a lot to unpack, so let’s dive right in!

Apache Maven is a build, or total project management tool that scores wins over its predecessors, such as ANT for Java builds, with the following advantages/areas that it excels in:

  • Useful when Multiple JAR Files are Required for a 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

For the purpose of this Apache Maven tutorial, I have created an alumni project that is a Dynamic Web Archive with a Servlet that says “Hello, Alumni” in the client’s browser. The following is a list of installations required for this project:

INSTALLATIONS REQUIRED

  • Apache Maven 3.2.5
  • Apache Tomcat 8.0.9
  • JDK 1.7.0 / JRE 1.7.0
  • Notepad++ 6.6.7

Apache Maven Basics: How to Setup Maven

To begin, download the latest version of Apache Maven (3.2.5 at the time of this writing) from the Apache Maven download page: 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 the Apache Maven installation. Also, the M2 environment variable will be pointing 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 follows:

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%

How to Configure Apache Maven and Create a Project

After you have set up your variables, the next step is to 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 of the templates from the Internet. This has to be used initially so that all of the project templates can be downloaded to the local system. You will be prompted at the end of this command’s “successful execution” for a number. Enter the number 529, which is the default used for ‘maven-archetype-webapp’. Make sure that you have an Internet connection; otherwise, you will not see all of the archetype listings. Maven Archetype is a templating toolkit that is provided along with Maven.

Maven Archtypes

Creating an Alumni Servlet with Apache Maven

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:

Alumni-Servlet-Java

Now, create the Java Servlet source file AlumniServlet.java using Notepad++ as follows:

Create-Java-Servlet-Maven

If we 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.

Compiling-Java-Servlet-Error-Maven

The next step we need to take is to add the dependent JARs so that the project can compile. We can do so in the pom.xml, as shown below. 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 article.

Adding-JARs-Java-Servlet-Maven

Once we have added our dependent JARs to the project, we can finally use ‘mvn compiler:compile’ to build or compile the web application.

Build-Java-Web-App-Maven

Packaging and Deploying an Alumni Project in Apache Maven

Now that we have configured our Apache Maven set up, created our Java Servlet, and built our project, we can finally package and deploy our Alumni Project. To do this, we must modify the web.xml file to add our servlet using the following configuration:

Maven-Java-XML-Alumni-Srvlet

Next, use the command ‘mvn war:war’ to package the web application, as shown below:

Packaging-Java-Web-Apps-Maven-Apache

To finish packaging and deploying the Alumni Project, drop the package under the Apache Tomcat ‘webapps’ folder and type the url http://localhost:8080/alumni into the browser to see the following output:

Maven-Alumni-Project-Browser

NOTE: Make sure you rename the default index.JSP file to index.BKP in order to render the Servlet output.

You may download the alumni.zip that also contains the alumni.war under target folder to try this out yourself.

Apache Maven Theory: Important Concepts To Know

Maven coordinates serve to identify projects, dependencies and plugins within the Project Object Model (POM). The following coordinates are required for every project, so it is important to know their meaning and what they are used for:

  • groupId: The name with which we can refer to a group of projects. This reference points to a repository directory that uses a tree structure. An example of a groupid could be org.apache.commons.
  • artifactId: ‘alumni_project artifactId’ would be the name of the JAR or WAR we will create.
  • version: ‘alumni version’, would be the version number of the current project package.

Understanding the 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 it generates error.
  • compile: Compiles the project and generates any error it cannot compile.
  • test: Unit Tests the code and does not require that the code to be packaged.
  • package: Packages and generate the artifact (such as JARs).
  • install: Installs the generated artifact, such as JAR, in the local repository.

Maven Plugins and the Maven Repository (Architecture)

Maven works in the form of plugins and follows the general format of: mvn plugin:goal

Some of the plugins include jar, compiler, and surefire. An example of a Maven command would be: mvn compiler:compile

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-Repository-Model

  • 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.

Apache Maven Project Object Model (POM)

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 pom.xml, which 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 result of the inheritance hierarchy).

Maven-Sample-POM-XML

[A Sample POM.xml ]

Located inside the Maven installation is a Super POM (pom.xml), usually located in the maven-model-builder-.jar files, which is found under the %M2_HOME%\lib\ folders. This contains the details of all other modules’ POM inherits 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.

In 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 our applications.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories