You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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:

1. Update Your POM for WAR Packaging

First off, your pom.xml needs two key adjustments to output a WAR instead of the default JAR:

  • Set the <packaging> element to war
  • 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>
2. Verify Your Project Structure

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 the WEB-INF directory (if you use a web.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.

3. Compile and Build the WAR

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 conflicts
  • package: 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.

4. Deploy to Tomcat

You've got two easy options for deployment:

Manual Deployment

  1. Copy the WAR file from target/ to your Tomcat installation's webapps/ directory
  2. Start or restart Tomcat—Tomcat will automatically unpack and deploy the WAR
  3. Access your app at http://localhost:8080/[war-filename-without-version] (e.g., if your WAR is myapp-1.0.war, the URL is http://localhost:8080/myapp)
    • To make it the root app, rename the WAR to ROOT.war

Maven Plugin Deployment (Automated)

Use the tomcat7-maven-plugin (works for Tomcat 7, 8, 9) to deploy directly from Maven:

  1. 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>
    
  2. Add Tomcat credentials to your Maven settings.xml (usually in ~/.m2/settings.xml):
    <servers>
        <server>
            <id>TomcatServer</id>
            <username>your-tomcat-username</username>
            <password>your-tomcat-password</password>
        </server>
    </servers>
    
    Note: You'll need to create a user with manager-script role in Tomcat's conf/tomcat-users.xml first.
  3. Run the deploy command:
    mvn tomcat7:deploy
    
    Use mvn tomcat7:redeploy if you're updating an existing deployment.
5. Quick Troubleshooting Tips
  • "Missing web.xml" error: Either add a web.xml to src/main/webapp/WEB-INF or set failOnMissingWebXml to false in 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 path parameter in the Tomcat plugin configuration.

内容的提问来源于stack exchange,提问作者Abdul Gaffar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:05:48