org.apache.tomcat.maven插件使用问题总结

本文详细介绍了使用 Maven 插件部署 Tomcat 的全过程,包括部署命令、首次与重新部署的区别、解决找不到 WAR 包的方法、配置 Tomcat 用户权限、设置 pom.xml 和 settings.xml 文件的步骤。同时提供了样例代码帮助开发者快速上手。

问题一:用什么命令部署

mvn tomcat6/7:deploy # 做第一次部署

mvn tomcat6/7:redeploy # 做重新部署

 

问题二:在使用插件部署前,tomcat需要事先启动么

是的。

 

问题三:在插件部署过程中,报错找不到war包

先要使用 mvn war:war 打包

 

问题四:在插件部署过程中,无法完成war包的上传

1. 在tomcat的tomcat-user.xml中配置好manager-script用户角色,并且manager-gui角色的用户和manager-script角色的用户名不要相同。

2. 在pom.xml文件中,插件<configuration>节点下,需要有一个<server>节点,其中文本内容是服务器名(可任意命名)。

3. 在maven主目录的conf目录下,修改settings.xml文件,在<server>节点下分别配置<id><username><password>节点。

 

pom.xml样例代码

<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.1</version>
  <configuration>
    <server>myserver</server>
    <path>/</path>
    <port>8080</port>
    <uriEncoding>UTF-8</uriEncoding>
  </configuration>
</plugin>

 

tomcat-users.xml样例代码

  <role rolename="manager-script"/>
  <user username="ns" password="ns" roles="manager-script"/>
  <role rolename="manager-gui"/>
  <user username="tomcat" password="tomcat" roles="manager-gui"/>

 

settings.xml样例代码

    <server>
      <id>myserver</id>
      <username>ns</username>
      <password>ns</password>
    </server>

 

出现错误: ``` Could not find artifact org.apache.tomcat.maven:tomcat-maven-plugin:jar:2.2 in central (https://repo.maven.apache.org/maven2) ``` 是因为你尝试使用Maven 插件 `tomcat-maven-plugin` 的 `2.2` 版本在 Maven Central 中确实不存在。这个插件的正确版本是 `2.2`,但它在 Maven Central 上的路径是 **`org.apache.tomcat.maven/tomcat7-maven-plugin`** 而不是 `tomcat-maven-plugin`。 --- ## ✅ 正确使用方式 ### 1. **使用正确的插件名称 `tomcat7-maven-plugin`** ```xml <build> <plugins> <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>tomcat</server> <path>/your-app</path> </configuration> </plugin> </plugins> </build> ``` ### 2. **确认版本是否存在于 Maven Central** 你可以访问官方 Maven Central 地址验证插件是否存在: - [https://repo1.maven.org/maven2/org/apache/tomcat/maven/tomcat7-maven-plugin/2.2/](https://repo1.maven.org/maven2/org/apache/tomcat/maven/tomcat7-maven-plugin/2.2/) 该地址下确实存在 `tomcat7-maven-plugin-2.2.jar`,说明版本是有效的。 --- ## 🔍 问题原因分析 1. **插件名称错误**:你使用了 `tomcat-maven-plugin`,而正确的插件名是 `tomcat7-maven-plugin`。 2. **版本与插件名不匹配**:Tomcat Maven 插件按版本命名,例如 `tomcat7-maven-plugin` 支持 Tomcat 7,`tomcat9-maven-plugin` 支持 Tomcat 9。 3. **Maven Central 中不存在该组合**:Maven Central 中没有 `tomcat-maven-plugin:2.2` 这个组合,因此报错。 --- ## ✅ 替代方案(推荐升级) 如果你不需要特别依赖 Tomcat 7,可以考虑使用更新的插件版本,例如: ### 使用 `tomcat9-maven-plugin`(兼容 Tomcat 7+) ```xml <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat9-maven-plugin</artifactId> <version>3.0.0</version> <configuration> <url>http://localhost:8080/manager/text</url> <server>tomcat</server> <path>/your-app</path> </configuration> </plugin> ``` --- ##
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值