• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Maven | Create a web module (WAR file)
  1. Create a Maven Project
  2. Update the pom.xml file

  1. Create a Maven Project
    Create a new Maven web application project using the webapp archetype:
    $ mvn archetype:generate \
      -DarchetypeGroupId=org.apache.maven.archetypes \
      -DarchetypeArtifactId=maven-archetype-webapp \
      -DarchetypeVersion=1.5 \
      -DgroupId=mtitek.maven.war.samples \
      -DartifactId=mtitek-maven-war-samples \
      -Dversion=1.0.0-SNAPSHOT \
      -DinteractiveMode=false
    The generated project structure will look like this:
    $ find ./mtitek-maven-war-samples/ -type f
    ./mtitek-maven-war-samples/pom.xml
    ./mtitek-maven-war-samples/src/main/webapp/index.jsp
    ./mtitek-maven-war-samples/src/main/webapp/WEB-INF/web.xml
    The archetype creates a minimal structure. Typically the project structure should look like:
    mtitek-maven-war-samples/
    ├── pom.xml
    ├── src/
    │   ├── main/
    │   │   ├── java/
    │   │   │   └── mtitek/maven/war/samples/
    │   │   │       └── servlet/
    │   │   │           └── HelloServlet.java
    │   │   ├── resources/
    │   │   │   ├── application.properties
    │   │   │   └── logback.xml
    │   │   └── webapp/
    │   │       ├── WEB-INF/
    │   │       │   └── web.xml
    │   │       ├── css/
    │   │       │   └── style.css
    │   │       ├── js/
    │   │       │   └── app.js
    │   │       └── index.jsp
    │   └── test/
    │       └── java/
    │           └── mtitek/maven/war/samples/
    │               └── servlet/
    │                   └── HelloServletTest.java
    └── target/
        └── mtitek-maven-war-samples.war
  2. Update the pom.xml file
    Once the project is generated, you should update the pom.xml file. Here's an example:
    <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>mtitek.maven.war.samples</groupId>
        <artifactId>mtitek-maven-war-samples</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <name>Maven WAR Sample Project</name>
        <description>Sample Maven web application demonstrating WAR packaging</description>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
            <!-- Spring Boot property -->
            <java.version>23</java.version>
    
            <!-- maven-compiler-plugin properties -->
            <maven.compiler.source>23</maven.compiler.source>
            <maven.compiler.target>23</maven.compiler.target>
            <maven.compiler.release>23</maven.compiler.release>
    
            <!-- Annotation-based configuration -->
            <failOnMissingWebXml>false</failOnMissingWebXml>
    
            <!-- Plugin versions -->
            <maven-compiler-plugin.version>3.14.0</maven-compiler-plugin.version>
            <maven-war-plugin.version>3.4.0</maven-war-plugin.version>
            <jetty-maven-plugin.version>11.0.25</jetty-maven-plugin.version>
            <jakarta.servlet-api.version>6.1.0</jakarta.servlet-api.version>
            <junit.version>5.13.1</junit.version>
        </properties>
    
        <dependencies>
            <!-- APP JAR dependencies -->
            <dependency>
                <groupId>mtitek.maven.jar.samples</groupId>
                <artifactId>mtitek-maven-jar-samples</artifactId>
                <version>1.0.0-SNAPSHOT</version>
                <type>jar</type>
            </dependency>
    
            <!-- Servlet API -->
            <dependency>
                <groupId>jakarta.servlet</groupId>
                <artifactId>jakarta.servlet-api</artifactId>
                <version>${jakarta.servlet-api.version}</version>
                <scope>provided</scope>
            </dependency>
    
            <!-- Test dependencies -->
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <!-- Removes version from WAR name -->
            <finalName>${project.artifactId}</finalName>
    
            <plugins>
                <!-- Compiler Plugin -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${maven-compiler-plugin.version}</version>
                </plugin>
    
                <!-- WAR Plugin -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>${maven-war-plugin.version}</version>
    
                    <configuration>
                        <!-- Example of files to include in the WAR -->
                        <packagingIncludes>
                            **/*.htm,
                            **/*.html,
                            **/*.css,
                            **/*.js,
                            **/*.jsp,
                            **/*.xml,
                            WEB-INF/lib/mtitek-*.jar
                        </packagingIncludes>
    
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                            </manifest>
    
                            <manifestEntries>
                                <Implementation-Build>${project.version}</Implementation-Build>
                                <Built-By>${user.name}</Built-By>
                                <Build-Time>${maven.build.timestamp}</Build-Time>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </plugin>
    
                <!-- Jetty Plugin -->
                <plugin>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-maven-plugin</artifactId>
                    <version>${jetty-maven-plugin.version}</version>
                    <configuration>
                        <httpConnector>
                            <port>8080</port>
                        </httpConnector>
                        <stopKey>STOP</stopKey>
                        <stopPort>9999</stopPort>
                        <webApp>
                            <contextPath>/</contextPath>
                        </webApp>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    Build the project:
    $ mvn clean install
    Run the web application using Jetty:
    $ mvn jetty:run
    You should see output similar to:
    [INFO] Configuring Jetty for project: mtitek-maven-war-samples
    [INFO] Context path = /
    [INFO] Started Jetty Server
    Test the web application:
    $ curl http://localhost:8080/
    Expected response:
    <html>
    <body>
    <h2>Hello World!</h2>
    </body>
    </html>
    Stop the server:
    Press Ctrl+C in the terminal.

    If the Jetty server was started with mvn jetty:start, then use the mvn jetty:stop command.

    If Tomcat was configured instead of Jetty,then you should use the following commands:
    $ mvn tomcat10:start
    $ mvn tomcat10:run
    $ mvn tomcat10:stop
© 2025  mtitek