Centurion, South Africa info@code2bits.com On Request

How to Create a Simple Web Project using Maven

This post contains a step-by-step guide on how to create a simple web application making use of maven. The Archetype Plugin is used to create a simple web project from an existing template / archetype. The guide also illustrates how to build and then run the application using maven commands.

Getting Started

The following list defines the technologies and libraries I used to implement the sample code:

Multiple Posts

Apache Maven is a software project management and comprehension tool. It is one of the basic tools every Java Developer should know. This post forms part of a series of articles about maven and step-by-step guides on how to use maven to build your java applications.

Code Example

The code example can be downloaded from Github from the button below. After you have downloaded the code onto your machine, you can either work through the guide step-by-step to understand the example or jump to the “Compile & Run The Application” section to get the example up and running.

Download Code

Step 1: Use the Maven Archetype Plugin

The archetype plugin allows the user to create a maven project from an existing template or archetype. The generate goal generates a new project in a directory corresponding to the artifactId. A list of the different archetypes can be found here.

$ mvn archetype:generate -DgroupId=com.code2bits.maven.example -DartifactId=simplewebproject -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Example

Step 2: View The Project Object Model (POM)

The Project Object Model (POM) is an XML file that contains information about the project and configuration details. The pom.xml file is used by Maven to build the project.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.code2bits.maven.example</groupId>
    <artifactId>simplewebproject</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
  
    <name>simplewebproject Maven Webapp</name>
    <url>http://maven.apache.org</url>
  
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
  
    <build>
        <finalName>simplewebproject</finalName>
    </build>
</project>

Step 3: View the Project Layout

The project layout of the web application can be viewed below. This view is generated by the tree command.

├── pom.xml
├── src
│   └── main
│       ├── resources
│       └── webapp
│           ├── WEB-INF
│           │   └── web.xml
│           └── index.jsp
└── target
    ├── classes
    ├── maven-archiver
    │   └── pom.properties
    ├── simplewebproject
    │   ├── META-INF
    │   ├── WEB-INF
    │   │   ├── classes
    │   │   └── web.xml
    │   └── index.jsp
    └── simplewebproject.war

Step 4: Compile and Run the Application

Use the following command to compile the Web application making use of maven.

Build Application

$ mvn clean install

After you have successfully built the Web application, the compiled artifact called *simplewebproject.war* can be found in the target directory. There are several ways to run/deploy the Web Application.

Deploy Web Application in Tomcat

To deploy the Web Application in Tomcat, you need to install Tomcat on you machine, deploy the Web Application and then run the Tomcat server on you machine.

Once the we application is deployed in a Tomcat server, you can access the web application via your browser from at the following url:

http://localhost:8080/simplewebproject

Summary

Congratulations! You have successfully created a simple Web Application using maven. You have also used maven to compile the application and also to deploy the application. Follow me on any of the different social media platforms and feel free to leave comments.