如何在Web应用中使用多包Java应用?Tomcat WAR编译打包疑问
Hey there! Let's walk through exactly how to compile your single-module Maven application and package it into a WAR file ready for Tomcat. I'll break this down into simple, actionable steps tailored to your setup:
First off, your pom.xml needs two key adjustments to output a WAR instead of the default JAR:
- Set the
<packaging>element towar - Mark the Servlet API dependency as
provided(since Tomcat already includes this, we don't want to bundle it into our WAR and cause conflicts)
Here's what the updated relevant sections of your POM should look like:
<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> <!-- Keep your existing groupId, artifactId, version here --> <!-- Critical: Switch packaging to WAR --> <packaging>war</packaging> <dependencies> <!-- Servlet API: Tomcat provides this at runtime, so scope=provided --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <!-- Use this for Tomcat 9; for Tomcat 10+, use jakarta.servlet:jakarta.servlet-api:5.0.0 --> <scope>provided</scope> </dependency> <!-- Leave all your other application dependencies as-is --> </dependencies> <!-- Optional: Configure the Maven WAR plugin (for custom behavior) --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.2</version> <configuration> <!-- If your web resources are in a non-default path, specify it here --> <!-- <warSourceDirectory>src/main/webapp</warSourceDirectory> --> <!-- Disable web.xml requirement if you're using Servlet 3.0+ annotations (like @WebServlet) --> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
Maven expects a standard layout for web apps—double-check yours matches this:
src/main/java: All your Java classes and packages (this should already be set up for your app)src/main/webapp: Static assets (HTML, CSS, JS) and theWEB-INFdirectory (if you use aweb.xml)src/main/resources: Any configuration files (optional but recommended)
If you're using Servlet 3.0+ annotations (like @WebServlet to define servlets without web.xml), you don't need a web.xml file—just make sure the failOnMissingWebXml flag in the WAR plugin is set to false as shown above.
Open a terminal in your project's root directory (where the pom.xml lives) and run this Maven command:
mvn clean package
clean: Wipes out previous build files to avoid conflictspackage: Compiles your code, runs tests (if you have them), and generates the WAR file
Your finished WAR will be in the target/ directory, named something like [your-artifactId]-[your-version].war.
You've got two easy options for deployment:
Manual Deployment
- Copy the WAR file from
target/to your Tomcat installation'swebapps/directory - Start or restart Tomcat—Tomcat will automatically unpack and deploy the WAR
- Access your app at
http://localhost:8080/[war-filename-without-version](e.g., if your WAR ismyapp-1.0.war, the URL ishttp://localhost:8080/myapp)- To make it the root app, rename the WAR to
ROOT.war
- To make it the root app, rename the WAR to
Maven Plugin Deployment (Automated)
Use the tomcat7-maven-plugin (works for Tomcat 7, 8, 9) to deploy directly from Maven:
- Add this plugin to your POM's
<build><plugins>section:<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>/myapp</path> <!-- The URL path for your app --> </configuration> </plugin> - Add Tomcat credentials to your Maven
settings.xml(usually in~/.m2/settings.xml):
Note: You'll need to create a user with<servers> <server> <id>TomcatServer</id> <username>your-tomcat-username</username> <password>your-tomcat-password</password> </server> </servers>manager-scriptrole in Tomcat'sconf/tomcat-users.xmlfirst. - Run the deploy command:
Usemvn tomcat7:deploymvn tomcat7:redeployif you're updating an existing deployment.
- "Missing web.xml" error: Either add a
web.xmltosrc/main/webapp/WEB-INFor setfailOnMissingWebXmltofalsein the WAR plugin. - ClassNotFoundExceptions after deployment: Check that dependencies like Servlet API have
scope=provided(they shouldn't be in the WAR). For other missing classes, verify your dependencies are correctly declared in the POM. - Incorrect app URL: Double-check the WAR filename or the
pathparameter in the Tomcat plugin configuration.
内容的提问来源于stack exchange,提问作者Abdul Gaffar

