如何在Maven项目中使用tomcat9-maven-plugin及IDEA配置示例
用tomcat9-maven-plugin启动嵌入式Tomcat测试Web应用
一、pom.xml配置示例
在你的Maven项目pom.xml的<build>节点下,找到<plugins>标签,添加以下tomcat9-maven-plugin配置:
<build> <plugins> <!-- 嵌入式Tomcat9插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat9-maven-plugin</artifactId> <!-- 使用稳定版本,可按需替换为最新兼容版 --> <version>2.2</version> <configuration> <!-- 自定义启动端口,默认8080 --> <port>8080</port> <!-- 应用访问路径,设为/则直接通过http://localhost:8080访问 --> <path>/</path> <!-- 解决中文乱码问题 --> <uriEncoding>UTF-8</uriEncoding> <!-- 可选:指定webapp目录,默认是src/main/webapp --> <!-- <warSourceDirectory>src/main/webapp</warSourceDirectory> --> <!-- 可选:设置上下文路径别名 --> <!-- <contextPath>/my-webapp</contextPath> --> </configuration> </plugin> </plugins> </build>
说明:
- 版本号建议选用官方稳定版,可根据项目需求调整为最新兼容版本
port:可修改为未被占用的端口,避免与其他服务冲突path:若设置为/my-webapp,则访问地址为http://localhost:8080/my-webapp
二、IntelliJ IDEA运行配置方法
- 点击IDEA顶部菜单栏的
Run->Edit Configurations... - 在弹出的配置窗口中,点击左上角的
+号,选择Maven - 配置页面填写内容:
Name:自定义配置名称,比如Tomcat9 RunWorking directory:选择你的Maven项目根目录(需指向包含pom.xml的文件夹)Command line:输入启动命令tomcat9:run
- 点击
Apply保存配置,再点击OK关闭窗口 - 返回IDEA主界面,在顶部运行配置下拉框中选择刚才创建的
Tomcat9 Run,点击运行按钮即可启动嵌入式Tomcat
如果需要添加JVM参数(比如调整内存),可在VM options中输入,例如-Xmx512m -Xms256m。
内容的提问来源于stack exchange,提问作者zhianABCD
相关产品推荐
相关产品推荐

