在前几讲中,我都是使用tomcat7插件来运行maven项目的,这只需要我们在pom.xml文件中添加tomcat7插件的依赖即可。
我靠!这个tomcat7插件是不是有点太老了,因为我本地tomcat服务器使用的都是apache-tomcat-8.5.38
这个版本了。我不禁就想了,何不在maven中安装tomcat8-maven-plugin插件,并使用它来运行maven项目呢?你不禁想问了,如何找到这样一个tomcat8-maven-plugin插件的坐标呢?首先,打开浏览器,访问https://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat8-maven-plugin这样一个url地址,这时你就可以看到所有版本的tomcat8-maven-plugin插件了。
这里,我们不妨选择3.0-r1655215
这个版本的tomcat8-maven-plugin插件。点击3.0-r1655215
,就能看到这个版本的tomcat8-maven-plugin插件的坐标了。
如果把上面的依赖加入到pom.xml文件中,那么要么就是提示找不到该依赖,要么就是下载不下来。我找了半天终于找到了一个解决方法,那就是使用maven中的<pluginRepositories>
标签(该标签是用来配置插件地址的)。因为maven中的所有功能都是使用插件来实现的,所以需要从特定的地址下载插件包。
于是,首先要在pom.xml文件中加入如下内容:
<!-- 配置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>
<pluginRepository>
<id>beardedgeeks-releases</id>
<url>http://beardedgeeks.googlecode.com/svn/repository/releases</url>
</pluginRepository>
</pluginRepositories>
然后,再在pom.xml文件中加入tomcat8-maven-plugin插件的依赖。
<!-- 配置tomcat8插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat8-maven-plugin</artifactId>
<version>3.0-r1655215</version>
<configuration>
<server>tomcat8</server>
<!-- 配置访问端口 -->
<port>8080</port>
<!-- 访问的项目路径 -->
<path>/hellomaven</path>
<!--
编码,为什么加这个编码呢?大家都知道,我们使用网络传输的时候,默认情况下使用的是iso8859-1编码
(提交请求的时候,特别是使用get请求的时候),所以要进行一个编码转换!
-->
<uriEncoding>utf-8</uriEncoding>
</configuration>
</plugin>
接下来,就要使用tomcat8-maven-plugin插件来运行maven项目了,大家可以按照下图所示的步骤进行操作。
然后,在弹出的窗口中输入tomcat8:run
命令,点击Run即可运行maven项目了。
这时,eclipse控制台打印出了如下内容。
从中可以发现使用maven里面带有的tomcat8插件来运行maven项目已经运行成功了。紧接着,打开Google Chrome浏览器,并访问http://localhost:8080/hellomaven/HelloServlet
这样一个url地址,发现报错了,错误提示信息如下:
而且,eclipse控制台也打印出了如下错误提示信息:
严重: Servlet.service() for servlet jsp threw exception
java.lang.LinkageError: loader constraint violation: when resolving method "org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(Ljavax/servlet/ServletConfig;)Lorg/apache/tomcat/InstanceManager;" the class loader (instance of org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/index_jsp, and the class loader (instance of org/codehaus/plexus/classworlds/realm/ClassRealm) for the method's defining class, org/apache/jasper/runtime/InstanceManagerFactory, have different Class objects for the type org/apache/tomcat/InstanceManager used in the signature
at org.apache.jsp.index_jsp._jspInit(index_jsp.java:32)
at org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:49)
at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:180)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:721)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:466)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:391)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
at com.meimeixia.web.HelloServlet.doGet(HelloServlet.java:13)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
十二月 21, 2019 9:13:01 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [HelloServlet] in context with path [/hellomaven] threw exception [java.lang.LinkageError: loader constraint violation: when resolving method "org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(Ljavax/servlet/ServletConfig;)Lorg/apache/tomcat/InstanceManager;" the class loader (instance of org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/index_jsp, and the class loader (instance of org/codehaus/plexus/classworlds/realm/ClassRealm) for the method's defining class, org/apache/jasper/runtime/InstanceManagerFactory, have different Class objects for the type org/apache/tomcat/InstanceManager used in the signature] with root cause
java.lang.LinkageError: loader constraint violation: when resolving method "org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(Ljavax/servlet/ServletConfig;)Lorg/apache/tomcat/InstanceManager;" the class loader (instance of org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/index_jsp, and the class loader (instance of org/codehaus/plexus/classworlds/realm/ClassRealm) for the method's defining class, org/apache/jasper/runtime/InstanceManagerFactory, have different Class objects for the type org/apache/tomcat/InstanceManager used in the signature
at org.apache.jsp.index_jsp._jspInit(index_jsp.java:32)
at org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:49)
at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:180)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:721)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:466)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:391)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
at com.meimeixia.web.HelloServlet.doGet(HelloServlet.java:13)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
尼玛的,使用tomcat8插件来运行maven项目,有这么难吗?我也在网上翻阅了好多篇文章,但也没找到一个解决办法,我的心真的好痛啊!但不管怎么样,还是把这个问题记录在这儿吧!如果大家知道一个好的解决办法,那么请告知我一下,万分感谢!!!