mvn:jetty 无法加载 WebAppContext

mar*_*sin 5 jndi maven jetty-9

当尝试从 Eclipse 运行 jetty 时,我遇到了一个奇怪的情况。我将旧项目从jetty 7更新到jetty 9.3.7.v20160115。但是,现在当使用 m2eclipse 启动 jetty:run 时,出现以下异常:

java.lang.IllegalArgumentException: Object of class 'org.eclipse.jetty.maven.plugin.JettyWebAppContext' is not of type 'org.eclipse.jetty.webapp.WebAppContext'. Object Class and type Class are from different loaders. in file:/xx/WebApp/src/main/webapp/WEB-INF/jetty-env.xml
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:295)
at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:245)
at org.eclipse.jetty.plus.webapp.EnvConfiguration.configure(EnvConfiguration.java:116)
Run Code Online (Sandbox Code Playgroud)

从消息中我可以看到这两个类不一样。但从文档中我需要使用类 org.eclipse.jetty.webapp.WebAppContext 和 org.eclipse.jetty.maven.plugin.JettyWebAppContext 是 WebAppContext 的子类。所以我不清楚第 292 行中的条件是否

oClass.isInstance(obj)
Run Code Online (Sandbox Code Playgroud)

在那里正确使用。

我在 jetty-env.xml 文件中定义了以下条目:

<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
 <New id="resInspector" class="org.eclipse.jetty.plus.jndi.Resource">
  <Arg><Ref refid="wac"/></Arg>
  <Arg>jdbc/xxx</Arg>
  <Arg>
   <New class="org.apache.commons.dbcp.BasicDataSource">
     <Set name="driverClassName">org.postgresql.Driver</Set>
     <Set name="url">jdbc:postgresql://xx:5432/xx</Set>
     <Set name="username">xx</Set>
     <Set name="password">xx</Set>
     <Set name="maxActive">100</Set>
     <Set name="maxIdle">30</Set>
     <Set name="maxWait">-1</Set>
     <Set name="defaultAutoCommit">false</Set>
   </New>
  </Arg>
 </New>
</Configure>
Run Code Online (Sandbox Code Playgroud)

接下来我在 web.xml 中添加了这些条目

<resource-ref>
  <description> JNDI resource</description>
  <res-ref-name>jdbc/xxx</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助。

马库斯

Ste*_*com 2

错误消息显示:

Object Class and type Class are from different loaders
Run Code Online (Sandbox Code Playgroud)

所以对象类 obj.getClass() 和类型 Class oClass 可能相同或相同,但从不同的类加载器加载。

可能的原因可能是您的类路径中有两次此类。一旦通过了 jetty 插件的依赖关系并进入了你的 pom.xml 文件。使用 mvn dependentecy:tree 检查并排除 pom 的依赖关系。

您也可以尝试更改班级

<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
Run Code Online (Sandbox Code Playgroud)

<Configure id='wac' class="org.eclipse.jetty.maven.plugin.JettyWebAppContext">
Run Code Online (Sandbox Code Playgroud)