xheritier-maven2
Image by  Xavier Heritier

About Apache Maven

Apache Maven  an open source software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

Step 1 – Prerequisites  and Requirements

For this tutorial, here’s the tools and resources we’ll be needing

  • Java Development Kit (JDK)
  • Apache Maven 3.x+
  • Apache Tomcat
  • A web browser

Step 2 – Installing Java Development Kit (JDK)

To build a maven project, we’ll have to have Java installed on your pc. So to verify if Java is installed, fire up a Terminal and run java -version. The output should be similar to this

openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111-8u111-b14-2ubuntu0.16.04.2-b14)
OpenJDK 64-Bit Server VM (build 25.111-b14, mixed mode)

Now, if you don’t see something similar to this, then Java is not installed. In your Terminal run sudo apt-get install default-jdk for linux users.

Alternatively, Go to the Java download page, and download the version suitable for your computer. After downloading and setting up Java, the next thing to do is install maven

Step 3 – Installing Apache Maven

Installing Apache Maven is similar to installing JDK, but maven runs independent of your operating system. So to check if we have maven up and running, in a Terminal, run

mvn -version the output should look similar to this below
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_111, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-53-generic", arch: "amd64", family: "unix"

If you see an error message, then maven is not installed yet. In your terminal type sudo apt-get install maven

Alternatively head over to the Apache Maven download page and under the ‘Files’ sub section in the page, select an appropriate binary or you can download the source code and build (which I am not going to cover here). Maven at the time of writing, requires JDK 1.7+ to execute.

Step 4 – Setting Up Apache Tomcat 8

Apache Tomcat is an open source web server and servlet container that is used to serve Java applications. In this tutorial, we will be needing Apache Tomcat to deploy out app to a local server. Run sudo apt-get install tomcat8 to install Tomcat. You can also, download Apache Tomcat from here and install.

Note: You are required to configure tomcat properly to be able to deploy this project successively. To configure Tomcat, see How To Install Tomcat by Digital Ocean

Step 5 – Creating a New Maven Project

OK, by now you should have configured all tools above to be running. Open a terminal window and run the following command to create a maven web project in the current working directory
mvn archetype:generate \
-DgroupId=com.hello.world \
-DartifactId=helloworld \
-DarchetypeArtifactId=maven-archetype-webapp \
-DinteractiveMode=false

This would create a new project and get called helloworldand maven will automatically download the dependencies for the project. (The values above in blue can be replaced by any other name. ) Now using your favorite text editor, open the project and you’ll see something like this

helloworldpro
Project directory structure

There are some important files we should note here:

  • pom.xmlIt is an XML file that contains information about the project and configuration details used by Maven to build the project.
  • index.jspIt is a sample JavaServer Page generated by maven
  • web.xmlIt is the deployment descriptor for a Servlet-based Java web application

Step 6 – Building and Deploying the App to Tomcat

Now that we have created our project, it’s time to finally test it and see how it works. So run the command mvn clean package. This will build our project to a .warfile which will be located in a new folder created called target

helloworldtag
.war file location under directory tree

So we will copy the helloworld.warfile into our Tomcat webapps folder. The tomcat webapps folder is located here:
%CATALINA_HOME\webappsfor Windows users, and
/usr/share/tomcat{version_number}/webapps for Linux users

Depending on how you configured Tomcat, those should be the default locations.

Now, in your browser, open http://localhost:8080/helloworldyou should see

hello
Preview of how our app running

Conclusion

And we are done with our first maven web application. Now you can add more stuff to it and make it even more powerful and dynamic.

Advertisements