1. tomcat8 插件
::: tip 提示
可能因為某些特殊原因,你需要使用 tomcat8-maven-plugin ,而不是 tomcat7 插件 。主要原因可能有 2 個:
tomcat7-maven-plugin 和 hibernate-validator 的高版本有沖突。 比如,你就只能用 hibernate-validator 5.4.3.Final 。
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 的路徑中不能有空格。
:::