maven tomcat8 插件

1. tomcat8 插件

::: tip 提示

可能因为某些特殊原因,你需要使用 tomcat8-maven-plugin ,而不是 tomcat7 插件 。主要原因可能有 2 个:

  1. tomcat7-maven-plugin 和 hibernate-validator 的高版本有冲突。 比如,你就只能用 hibernate-validator 5.4.3.Final 。

  2. tomcat7-maven-plugin 支持 Servlet 3.0,但是 Servlet 的内置的文件上传功能官方宣称从 Servlet 3.1 开始支持的<small>( 实测 tomcat7 / Servlet 3.0 也能支持,但是有个文件名中文乱码的 bug )</small>。

:::

你能在 mvnrepository 上查到 tomcat8-maven-plugin 插件的信息<small>(网址)</small>。但是,人家有也明确说了,这个包并不在中央仓库中,而是在另一个<small>( 非中央 )</small>仓库里。

Note: this artifact is located at ICM repository (http://maven.icm.edu.pl/artifactory/repo/)

因此,maven 无法从中央仓库下载到 tomcat 8 的 maven 插件,因为中央仓库根本就没有。<small>阿里的镜像服务器中自然也没有。</small>

如果,你要使用 tomcat8-maven-plugin 插件,你需要额外地、明确地告诉它对于中央仓库中没有的包,要去哪里下载。这就需要在 pom.xml 配置文件中进行配置。

依赖声明整体结构<small>( 其它无关元素略。另外,pluginRepositories 习惯性在 build 的后面 )</small>:

project 
├── dependencies
├── build
└── pluginRepositories
    ├── pluginRepository
    ├── pluginRepository
    ├── ...
    └── pluginRepository

主要包含如下元素:

<pluginRepositories>
    <pluginRepository>
        <id>alfresco-public</id>
        <url>https://artifacts.alfresco.com/nexus/content/groups/public</url>
        <snapshots>
           <enabled>false</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
        </releases>
    </pluginRepository>

    <pluginRepository> ... </pluginRepository>
    <pluginRepository> ... </pluginRepository>
    <pluginRepository> ... </pluginRepository>
</pluginRepositories>

考虑到境外仓库的网络问题,最好还是直接从老师的本地仓库中将他们下好的 tomcat8 插件拷贝过来。

pom.xml 配置

<project>

  ...

  <build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
      <!-- plugin 在 plugins 里面 -->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat8-maven-plugin</artifactId>
<!--    <version>3.0-r1655215</version>-->
        <version>3.0-r1756463</version>
        <configuration>
          <port>8081</port>
          <path>/${project.artifactId}</path>
          <uriEncoding>UTF-8</uriEncoding>
        </configuration>
      </plugin>

      <plugin> ... </plugin>
      <plugin> ... </plugin>
      <plugin> ... </plugin>
      <plugin> ... </plugin>
    </plugins>
  </build>

  <!--tomcat8 插件仓库 -->
  <pluginRepositories>
    <pluginRepository>
      <id>alfresco-public</id>
      <url>https://artifacts.alfresco.com/nexus/content/groups/public</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <enabled>true</enabled>
      </releases>
    </pluginRepository>
  </pluginRepositories>

</project>

几个注意事项

在通过 tomcat8-maven-plugin 运行项目时,可能会遇到一些问题,以下几个问题都是有人遇到过,但是有人却不会出现这个问题。

问题一:找不到 rt.jar

使用 tomcat8:run 时,有可能会出现如下错误:

Caused by: java.io.FileNotFoundException: C:\Program%20Files\Java\jdk1.8.0_201\jre\lib\rt.jar (系统找不到指定的路径。)

这是因为 tomcat8-maven-plugin 运行时需要查找、使用 rt.jar ,但是它所在的路径名中有空格(上述的 %20)对 tomcat8-maven-plugin 造成了干扰。

::: tip 解决方案
使用的是 tomcat8:run-war 命令运行项目。
:::

问题二:访问后台遇到 500 错误

使用 tomcat8:run 时,有可能会出现莫名其妙的 500 错误。

::: tip 解决方案
使用的是 tomcat8:run-war 命令运行项目。
:::

问题三:找不到 javax.inject-1.jar

使用 tomcat8:run-war 时,有可能会遇到如下问题<small>( 猜测是和 IDEA 的版本有关,我在使用 2021.2.2 等高版本之后就没有遇到这个问题了 )</small>。

Caused by: java.io.FileNotFoundException: C:\Program%20Files\JetBrains\IntelliJ%20IDEA%202020.2.3\plugins\maven\lib\maven3\lib\javax.inject-1.jar (系统找不到指定的路径。)

这是因为 IDEA 所使用的内置的 maven 的路径名中有空格(上述的 %20)对 tomcat8-maven-plugin 造成了干扰<small>(对 tomcat7-maven-plugin 没影响)</small>。

::: tip 解决方案
万一遇到了这个问题,你需要将 IDEA 所使用的 maven 从内置的 maven 配置为外部的 maven ,而且这个 maven 的路径中不能有空格。
:::

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容