MAVEN 打包JAR 内嵌TOMCAT
时间: 2024-10-13 10:14:00 浏览: 73
要使用Maven打包包含内嵌Tomcat的JAR文件,可以按照以下步骤操作[^1]:
1. **配置pom.xml**:
在`pom.xml`文件中,添加`maven-assembly-plugin`插件来创建包含Tomcat和Web应用的可运行jar(fat-jar):
```xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.example.MainClass</mainClass> <!-- 自定义的主类 -->
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 其他与war相关的插件,如maven-war-plugin -->
</plugins>
</build>
```
2. **添加tomcat插件依赖**:
如果你想使用特定版本的Tomcat,添加对应的Tomcat-maven-plugin到pom.xml:
```xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.39</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.39</version>
</dependency>
</dependencies>
</dependencyManagement>
```
3. **自定义主类**:
创建一个名为`MainClass.java`的类,该类负责启动Tomcat并部署你的web应用。例如:
```java
public class MainClass {
阅读全文