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 的內(nèi)置的文件上傳功能官方宣稱從 Servlet 3.1 開始支持的<small>( 實測 tomcat7 / Servlet 3.0 也能支持,但是有個文件名中文亂碼的 bug )</small>。

:::

你能在 mvnrepository 上查到 tomcat8-maven-plugin 插件的信息<small>(網(wǎng)址)</small>。但是,人家有也明確說了,這個包并不在中央倉庫中,而是在另一個<small>( 非中央 )</small>倉庫里。

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

因此,maven 無法從中央倉庫下載到 tomcat 8 的 maven 插件,因為中央倉庫根本就沒有。<small>阿里的鏡像服務(wù)器中自然也沒有。</small>

如果,你要使用 tomcat8-maven-plugin 插件,你需要額外地、明確地告訴它對于中央倉庫中沒有的包,要去哪里下載。這就需要在 pom.xml 配置文件中進行配置。

依賴聲明整體結(jié)構(gòu)<small>( 其它無關(guān)元素略。另外,pluginRepositories 習(xí)慣性在 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>

考慮到境外倉庫的網(wǎng)絡(luò)問題,最好還是直接從老師的本地倉庫中將他們下好的 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 運行項目時,可能會遇到一些問題,以下幾個問題都是有人遇到過,但是有人卻不會出現(xiàn)這個問題。

問題一:找不到 rt.jar

使用 tomcat8:run 時,有可能會出現(xiàn)如下錯誤:

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

這是因為 tomcat8-maven-plugin 運行時需要查找、使用 rt.jar ,但是它所在的路徑名中有空格(上述的 %20)對 tomcat8-maven-plugin 造成了干擾。

::: tip 解決方案
使用的是 tomcat8:run-war 命令運行項目。
:::

問題二:訪問后臺遇到 500 錯誤

使用 tomcat8:run 時,有可能會出現(xiàn)莫名其妙的 500 錯誤。

::: tip 解決方案
使用的是 tomcat8:run-war 命令運行項目。
:::

問題三:找不到 javax.inject-1.jar

使用 tomcat8:run-war 時,有可能會遇到如下問題<small>( 猜測是和 IDEA 的版本有關(guān),我在使用 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 (系統(tǒng)找不到指定的路徑。)

這是因為 IDEA 所使用的內(nèi)置的 maven 的路徑名中有空格(上述的 %20)對 tomcat8-maven-plugin 造成了干擾<small>(對 tomcat7-maven-plugin 沒影響)</small>。

::: tip 解決方案
萬一遇到了這個問題,你需要將 IDEA 所使用的 maven 從內(nèi)置的 maven 配置為外部的 maven ,而且這個 maven 的路徑中不能有空格。
:::

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容