我有一个支持Maven的webapp。当我使用mvn install
运行构建时,一切看起来都很好,类在WEB-INF/classes
下,maven依赖项在WEB-INF/lib
下,web.xml文件在WEB-INF/
下。 / p>
但是,当我使用Tomcat 7.x从Eclipse内部署时,它找不到监听器类。也不会调用com.test.backend.BackendApplication servlet,即使它在web.xml中配置为在启动时加载。
我注意到这些类没有出现在Project Explorer上Eclipse的“Deployed Resources”节点下,但是我不知道如何让它出现在那里。
以下是我得到的例外情况:
SEVERE: Error configuring application listener of class com.test.backend.BackendServletContextListener
java.lang.ClassNotFoundException: com.test.backend.BackendServletContextListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:532)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:514)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:142)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4876)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5455)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
这是我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>com.test.backend</display-name>
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.test.backend.BackendApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>com.test.backend.BackendServletContextListener</listener-class>
</listener>
</web-app>
我的pom.xml看起来像这样:
<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>com.test</groupId>
<artifactId>backend</artifactId>
<packaging>war</packaging>
<version>1.0.0.0</version>
<properties>
<java.version>1.8</java.version>
<maven.version>3.1.0</maven.version>
<jersey.version>2.14</jersey.version>
<jersey.test.version>2.15</jersey.test.version>
<jackson.version>2.5.2</jackson.version>
<guice.version>4.0</guice.version>
</properties>
<dependencies>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>prepare-war</id>
<phase>prepare-package</phase>
<configuration>
<archiveClasses>true</archiveClasses>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix />
</manifest>
<manifestEntries>
<Implementation-Build>${build.number}</Implementation-Build>
<Implementation-Title>${project.name}</Implementation-Title>
<Implementation-Vendor>RestApi.com</Implementation-Vendor>
<Built-By>${user.name}</Built-By>
<Built-OS>${os.name}</Built-OS>
<Build-Date>${build.date}</Build-Date>
</manifestEntries>
</archive>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>./target/${project.artifactId}</directory>
</resource>
</webResources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
非常感谢帮助!我搜索了几个小时,但到目前为止还没有运气......
答案 0 :(得分:0)
右键单击maven Web项目,然后在部署程序集下添加&#34;文件夹,其中包含目标文件夹中的侦听器类&#34;。
例如,如果ServletContextListener
中存在package com.company.listeners
实现,则部署程序集应具有以下文件夹映射:
目标/&lt; applicationname&gt; / WEB-INF / classes / com / company / listeners
应该映射到
<强> WEB-INF /类/ COM /公司/听众强>
我认为这与tomcat中的类加载器层次结构和用于加载侦听器的类加载器(以及可用于该类加载器的类)有关。
答案 1 :(得分:0)