Create a Simple Maven Web Application using Command Line

In this tutorial, we will create a simple Maven web application using the command line. We will use the latest versions of Java and dependencies. This guide will take you through the process of setting up the project, adding necessary dependencies, configuring the web application, and running it.

Prerequisites

  • JDK 21 installed
  • Maven installed
  • A web browser

Step 1: Generate a Maven Project

First, open your command line terminal and run the following command to generate a Maven project:

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

This command creates a Maven project with the basic directory structure for a web application.

Step 2: Navigate to the Project Directory

Change to the newly created project directory:

cd my-webapp

Step 3: Update the pom.xml File

Open the pom.xml file and update it to include the latest versions of dependencies and plugins. Here is an example of an updated pom.xml:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>my-webapp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>My Web Application</name>
    <description>A simple Maven web application</description>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>6.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.jsp</groupId>
            <artifactId>jakarta.jsp-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jakarta.servlet.jsp.jstl</artifactId>
            <version>3.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <server>TomcatServer</server>
                    <path>/my-webapp</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Step 4: Create the Web Application Directory Structure

If not already created, ensure the directory structure is as follows:

my-webapp
│   pom.xml
└───src
    └───main
        └───java
        └───resources
        └───webapp
            │   WEB-INF
            │   index.jsp
  • src/main/java: Place your Java source files here.
  • src/main/resources: Place your resource files here.
  • src/main/webapp: Place your web content here (e.g., HTML, JSP files).
  • src/main/webapp/WEB-INF: Place your web.xml file and other configuration files here.

Step 5: Create a Simple JSP Page

Create a file named index.jsp in the src/main/webapp directory with the following content:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>My Web Application</title>
</head>
<body>
    <h2>Hello, World!</h2>
</body>
</html>

Step 6: Create web.xml File

Create a file named web.xml in the src/main/webapp/WEB-INF directory with the following content:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <display-name>Archetype Created Web Application</display-name>
</web-app>

Step 7: Build the Project

Run the following command to build the project:

mvn clean package

This command will generate a WAR file in the target directory.

Step 8: Run the Application

To run the web application, you can use an embedded Tomcat server. Add the following plugin configuration to your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <path>/my-webapp</path>
            </configuration>
        </plugin>
    </plugins>
</build>

Now, start the application using the following command:

mvn tomcat7:run

Step 9: Access the Application

Open your web browser and navigate to http://localhost:8080/my-webapp. You should see the "Hello, World!" message from your index.jsp page.

Conclusion

Congratulations! You have successfully created a simple Maven web application using the command line. This guide covered the essential steps, from generating the project to running it on an embedded Tomcat server. Maven simplifies the process of managing dependencies and building Java web applications, making it an invaluable tool for Java developers.

Comments

  1. Once the Jetty server is started just hit the below URL in a browser:
    http://localhost:8080/simple-webapp/simple

    ReplyDelete
  2. The maven archetype is missing, so you cannot even finish step 1.

    ReplyDelete

Post a Comment

Leave Comment