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.
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.
Change to the newly created project directory:
cd my-webapp
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>
If not already created, ensure the directory structure is as follows:
my-webapp │ pom.xml └───src └───main └───java └───resources └───webapp │ WEB-INF │ index.jsp
Create a file named index.jsp in the src/main/webapp directory with the following content:
<pre><%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>My Web Application</title> </head> <body> <h2>Hello, World!</h2> </body> </html></pre>
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>
Run the following command to build the project:
mvn clean package
This command will generate a WAR file in the target directory.
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
Open your web browser and navigate to http://localhost:8080/my-webapp. You should see the "Hello, World!" message from your index.jsp page.
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.