已搭建可运行Vert.x项目,如何用Maven构建WAR包?
Hey there! Let's break this down step by step—first I'll quickly clarify what the maven-shade-plugin does (since you mentioned you copied the config without understanding it), then walk you through exactly how to build a WAR for your Vert.x project.
First: What does the maven-shade-plugin do?
The shade plugin’s job is to create a fat (or uber) JAR: it takes your compiled application code, pulls in all the dependencies your project relies on (like Vert.x core, web, etc.), and merges everything into a single executable JAR file. That’s why you can run it directly without juggling classpath settings—every bit of code your app needs is bundled inside one file. When you added that plugin config, you were telling Maven to package all those pieces together and set your main Verticle class as the entry point to launch the app.
Now: Building a WAR for your Vert.x project
To build a WAR instead of (or alongside) a fat JAR, you’ll need to adjust your pom.xml and add the maven-war-plugin. Here’s how to do it properly:
1. Update your project packaging type
First, change the packaging from jar to war at the top of your pom.xml:
<project ...> <!-- Switch from JAR to WAR packaging --> <packaging>war</packaging>
2. Add the Vert.x Servlet adapter (if needed)
If you plan to deploy your WAR to a standard Servlet container (like Tomcat, Jetty, etc.), you’ll need the vertx-web-servlet dependency to wrap your Vert.x app as a Servlet. Add this to your <dependencies> section:
<dependencies> <!-- Keep your existing Vert.x dependencies (vertx-core, vertx-web, etc.) here --> <!-- Add Servlet adapter for container deployment --> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-web-servlet</artifactId> <version>${vertx.version}</version> <!-- Use <scope>provided</scope> only if your container already supplies the Servlet API --> </dependency> </dependencies>
3. Configure the maven-war-plugin
Add this plugin to your <build><plugins> section to handle assembling the WAR file. You can keep your shade plugin config if you still want to build fat jars too:
<build> <plugins> <!-- Configure WAR plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.2</version> <!-- Use the latest stable version --> <configuration> <!-- Optional: Exclude dependencies marked as "provided" to avoid duplication --> <packagingExcludes>WEB-INF/lib/*-provided.jar</packagingExcludes> <!-- Include web.xml if you're using it to register the Vert.x Servlet --> <webResources> <resource> <directory>src/main/resources</directory> <targetPath>WEB-INF</targetPath> <includes> <include>web.xml</include> </includes> </resource> </webResources> </configuration> </plugin> <!-- Keep your existing shade plugin config here if you still need fat jars --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>your.package.name.YourMainVerticle</mainClass> <!-- Replace with your actual main class --> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>
4. Add a web.xml (if deploying to a Servlet container)
If you’re using a Servlet container, create a web.xml file in src/main/resources to register the Vert.x Servlet. Replace the verticle class path with your own:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>VertxServlet</servlet-name> <servlet-class>io.vertx.ext.web.servlet.VertxServlet</servlet-class> <init-param> <param-name>verticle-class</param-name> <param-value>your.package.name.YourMainVerticle</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>VertxServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
5. Build the WAR
Run this command in your terminal:
mvn clean package
Your WAR file will be generated in the target directory (e.g., your-project-name.war). If you kept the shade plugin, you’ll also get your fat JAR there too.
Quick notes
- If you don’t need to deploy to a Servlet container, you can skip the
vertx-web-servletdependency andweb.xml—some Vert.x runtimes can handle WARs directly without Servlet adaptation. - Double-check that your main Verticle class path is correct in both the shade plugin’s manifest and
web.xml(if used) to avoid startup errors.
内容的提问来源于stack exchange,提问作者Ramanujan R

