Building a Java web application is very hard and can be a very tedious task. Typical build steps are as follows:
- Compiling all source files. Copying all class files to a temporary directory.
- (Optional) Compiling unit test source codes and then run unit tests. Stopping the build process if a unit test fails.
- Transferring other required files (xml, properties, etc) to the temporary directory.
- Copying all the web content files.
- Building the war file.
As you can see, there are several steps in building an application. Building an application manually is no longer advisable. This is why build tools were introduced. Sample of build tools are Ant, Gradle , our IDEs and Maven. In this article, we will focus on using Apache Maven for building a Java application
What is Apache Maven?
Maven is considered by many only as a build tool. Maven, as defined by the project’s website, is a project management and comprehension tool. Aside from helping on managing a build, maven is also useful in documentation, reporting, managing dependencies, SCMs, releases and distributions.
This blog will only focus on using Maven as a build tool. We will see how Maven is used in creating a Java application, testing it through unit tests, building/packaging the application and then finally deploying it.
Maven’s objectives are as follows:
- Make the build process easy.
- Provide a uniform build system.
- Provide a quality project information.
- Provide guidelines for best practices development.
- Allowing transparent migration to new features.
Maven follows a defined process for building any application. This is the Maven build lifecycle. There are currently 3 built-in build lifecycles: default, clean and site. Each lifecycle is made up of phases. Example of phases for default lifecycle in sequential order are: validate, compile, test and package. I mentioned sequential because you have to execute all the phases before your desired phase. To execute a build phase just run the following command:
For example, to run the package phase execute the command mvn package.
A build phase is consist of goals. A goal, similar to Ant’s task represents a specific task that helps in building and managing a project. Goals do not need to be bound on a build phase. A goal bound on a build phase is going to be executed when that phase is run. A goal not bound on any build phase could be executed outside of the build lifecycle by directly calling it. To invoke a goal just run the following command:
Lastly, for every executed goal, a plugin is used. Maven is a plugin execution framework, that is, everything is done by plugins. There are 2 types of plugins: Build plugins and Reporting plugins. Build plugins are invoked during the build while reporting plugins are executed during site generation.
To create a Maven project, we need to first understand the concept of archetype. Archetype is defined as a pattern or prototype upon which others are copied or patterned. In Maven, archetype is a project templating toolkit. This allows other developers to create project template that other users can use. Archetypes currently available ranges from the simplest to the more complex applications. Sample archetypes are archetype for creating a web application with Hibernate, Spring and Spring MVC, archetype for a simple Java web application, Groovy basic archetype, etc.
To create a maven project, just run the following command: mvn archetype:generate This command will enable you to create a maven project using the interactive mode, that is you will be able to choose the archetype to use, the version of your project, and other necessary information for the project. To create a simple web application, run the following command:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
Maven Standard Directory Layout
Maven suggests a standard directory layout for all maven project. This will allow developers to easily navigate with another Maven project once they are comfortable with the standard. Shown below is the directory layout expected by maven.
After creating a Maven project, notice the file named pom.xml. POM stands for Project Object Model. This is an XML representation of a Maven project. It can contain all the necessary information you need for a Java project. A minimum pom.xml file is shown below:
4.0.0 org.ideyatech.group my-project 1.0
- groupId – a unique ID for an organization or project.
- artifactId – the name of the project
- version – the current version of the project
Maven also helps developers to manage the project’s dependencies. One advantage of using Maven is developers just need to specify the libraries they need. If those libraries have their own dependencies, called transitive dependencies maven will automatically download them. You can specify a dependency using the following:
junit junit 4.0 jar test
- groupId, artifactId, version – the same definition as creating a new Maven project
- type – the artifact’s packaging type. This defaults to jar.
- scope – refers to the classpath of the task at hand. Defaults to compile.
Compiling application sources can easily be done by executing the phase: mvn compile.
To execute unit tests we need to first add the following plugin:
org.apache.maven.plugins maven-surefire-plugin 2.11 **/*Test.java
Notice the include tag. This tells Maven to only include all test classes with name ending with Test. Then to run all tests just invoke mvn test
You can specify the packaging of your application in the packaging tag of your pom.xml file. Since we are working with web application, the packaging should be war. To package a maven project, we just need to execute the command mvn package.
To deploy an application in a running Tomcat instance, add the following plugin:
org.codehaus.mojo tomcat-maven-plugin http://localhost:9090/manager/html user1 password true
- url – the url of the tomcat manager
- username – username that will connect to the tomcat manager
- password – password of the username
Then just invoke the command: mvn tomcat:deploy