IDEA中使用tomcat8-maven-plugin插件
第一种方式
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>edu.finance</groupId><artifactId>test-tomcat8</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><dependencies><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency></dependencies><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><build><finalName>ssm</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat8-maven-plugin</artifactId><version>3.0-r1655215</version>
<!-- <configuration>-->
<!-- <port>8080</port>-->
<!-- <path>/</path>-->
<!-- <uriEncoding>${project.build.sourceEncoding}</uriEncoding>-->
<!-- </configuration>--></plugin></plugins></pluginManagement></build><!--配置Tomcat8仓库--><pluginRepositories><pluginRepository><id>alfresco-public</id><url>https://artifacts.alfresco.com/nexus/content/groups/public</url></pluginRepository><pluginRepository><id>alfresco-public-snapshots</id><url>https://artifacts.alfresco.com/nexus/content/groups/public-snapshots</url><snapshots><enabled>true</enabled><updatePolicy>daily</updatePolicy></snapshots></pluginRepository><!--这个google的用不了--><!--<pluginRepository><id>beardedgeeks-releases</id><url>http://beardedgeeks.googlecode.com/svn/repository/releases</url></pluginRepository>--></pluginRepositories>
</project>
运行
输入命令
mvn tomcat8:run-war
运行成功
报错信息
参考
https://www.jianshu.com/p/4944d7d503a1
问题一:找不到 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
时,有可能会遇到如下问题( 猜测是和 IDEA 的版本有关,我在使用 2021.2.2 等高版本之后就没有遇到这个问题了 )。
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 造成了干扰(对 tomcat7-maven-plugin 没影响)。
tip 解决方案
万一遇到了这个问题,你需要将 IDEA 所使用的 maven 从内置的 maven 配置为外部的 maven ,而且这个 maven 的路径中不能有空格。
第二种
下面仓库有Tomcat8
https://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat8-maven-plugin
https://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat8-maven-plugin/3.0-r1655215
或者
https://artifacts.alfresco.com/nexus/
pom
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>edu.finance</groupId><artifactId>test-tomcat81</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><!-- https://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat8-maven-plugin --><dependencies><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.tomcat.maven</groupId>-->
<!-- <artifactId>tomcat8-maven-plugin</artifactId>-->
<!-- <version>3.0-r1655215</version>-->
<!-- </dependency>--></dependencies><build><plugins><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat8-maven-plugin</artifactId><version>3.0-r1655215</version><configuration><port>80</port><path>/</path></configuration></plugin></plugins></build><!--配置Tomcat8仓库--><pluginRepositories><pluginRepository><id>alfresco</id><url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url></pluginRepository></pluginRepositories>
</project>
“tomcat8:run” 爱报各种错误,建议使用“tomcat8:run war”
如果不想启动的时候太麻烦,可以在右上角工具点击“Edit Configurations”
点击“+”,Maven ,可以命名为tomcat8,这样可以直接点击,不用再找到“tomcat8:run”