使用maven和嵌入式jetty:ClassCastException

发布于 2024-08-27 03:53:16 字数 7402 浏览 9 评论 0原文

我有一个用 java 编写的网络应用程序。

我想在嵌入式码头服务器上运行集成测试。

为此,我有一个 Maven 项目(仅用于运行集成测试)。

为了部署我使用cargo-maven2-plugin。但是,当码头启动时,我收到以下内容:

java.lang.ClassCastException: org.mortbay.jetty.webapp.WebInfConfiguration cannot be cast to org.mortbay.jetty.webapp.Configuration

完整日志:

[beddedLocalContainer] Jetty 6.x Embedded starting...
2010-03-29 16:20:46.615::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
2010-03-29 16:20:46.715::INFO:  jetty-6.1.1rc1
2010-03-29 16:20:46.980::INFO:  Extract jar:file:/C:/Documents%20and%20Settings/Alpha/Local%20Settings/Temp/cargo/conf/cargocpc.war!/ to C:\DOCUME~1\Alpha\LOCALS~1\Temp\Jetty_0_0_0_0_8080_cargocpc.war__cargocpc__xflgf3\webapp
log4j:WARN No appenders could be found for logger (org.apache.jasper.compiler.JspRuntimeContext).
log4j:WARN Please initialize the log4j system properly.
2010-03-29 16:20:47.897::INFO:  Started SelectChannelConnector @ 0.0.0.0:8080
[beddedLocalContainer] Jetty 6.x Embedded started on port [8080]
[cargo:deployer]
[mbeddedLocalDeployer] Deploying [c:\maven-repo\myapp\2.1-SNAPSHOT\myapp-2.1-SNAPSHOT.war]
2010-03-29 16:20:51.711::WARN:  Failed startup of context org.mortbay.jetty.webapp.WebAppContext@132e910{/myapp,c:\maven-repo\myapp\2.1-SNAPSHOT\myapp-2.1-SNAPSHOT.war}
java.lang.ClassCastException: org.mortbay.jetty.webapp.WebInfConfiguration cannot be cast to org.mortbay.jetty.webapp.Configuration
        at org.mortbay.jetty.webapp.WebAppContext.loadConfigurations(WebAppContext.java:801)
        at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:403)
        at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:40)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.cargo.container.jetty.Jetty6xEmbeddedLocalContainer.addHandler(Jetty6xEmbeddedLocalContainer.java:294)
        at org.codehaus.cargo.container.jetty.Jetty6xEmbeddedLocalDeployer.deployWebApp(Jetty6xEmbeddedLocalDeployer.java:77)
        at org.codehaus.cargo.container.jetty.internal.AbstractJettyEmbeddedLocalDeployer.deploy(AbstractJettyEmbeddedLocalDeployer.java:95)
        at org.codehaus.cargo.maven2.DeployerDeployMojo.performDeployerActionOnSingleDeployable(DeployerDeployMojo.java:79)
        at org.codehaus.cargo.maven2.AbstractDeployerMojo.performDeployerActionOnAllDeployables(AbstractDeployerMojo.java:104)
        at org.codehaus.cargo.maven2.AbstractDeployerMojo.doExecute(AbstractDeployerMojo.java:47)
        at org.codehaus.cargo.maven2.AbstractCargoMojo.execute(AbstractCargoMojo.java:255)
        at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
        at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)

Maven 货物设置:

<profile>
<id>container-cargo-jetty</id>
<properties>
    <skip.test.phase>true</skip.test.phase>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
            <version>1.0</version>
            <configuration>
                <wait>false</wait>
                <container>
                    <containerId>jetty6x</containerId>
                    <type>embedded</type>
                </container>
                <configuration>
                    <properties>
                        <cargo.servlet.port>8080</cargo.servlet.port>
                        <cargo.logging>medium</cargo.logging>
                    </properties>
                </configuration>
                <deployer>
                    <deployables>
                        <deployable>
                            <groupId>myapp</groupId>
                            <artifactId>myapp</artifactId>
                            <type>war</type>
                            <properties>
                                <context>/myapp</context>
                            </properties>
                        </deployable>
                    </deployables>
                </deployer>
            </configuration>
            <executions>
                <execution>
                    <id>start-container</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                        <goal>deployer-deploy</goal>
                    </goals>
                </execution>
                <execution>
                    <id>stop-container</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.mortbay.jetty</groupId>
                    <artifactId>jetty-embedded</artifactId>
                    <version>6.1.22</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

谢谢帮助

I have a web-application written in java.

I would like to run integration tests on a embedded jetty server.

For that purpose I have a maven project (just for running integration tests).

For deploying I use cargo-maven2-plugin. But while jetty startup I receive following:

java.lang.ClassCastException: org.mortbay.jetty.webapp.WebInfConfiguration cannot be cast to org.mortbay.jetty.webapp.Configuration

Full log:

[beddedLocalContainer] Jetty 6.x Embedded starting...
2010-03-29 16:20:46.615::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
2010-03-29 16:20:46.715::INFO:  jetty-6.1.1rc1
2010-03-29 16:20:46.980::INFO:  Extract jar:file:/C:/Documents%20and%20Settings/Alpha/Local%20Settings/Temp/cargo/conf/cargocpc.war!/ to C:\DOCUME~1\Alpha\LOCALS~1\Temp\Jetty_0_0_0_0_8080_cargocpc.war__cargocpc__xflgf3\webapp
log4j:WARN No appenders could be found for logger (org.apache.jasper.compiler.JspRuntimeContext).
log4j:WARN Please initialize the log4j system properly.
2010-03-29 16:20:47.897::INFO:  Started SelectChannelConnector @ 0.0.0.0:8080
[beddedLocalContainer] Jetty 6.x Embedded started on port [8080]
[cargo:deployer]
[mbeddedLocalDeployer] Deploying [c:\maven-repo\myapp\2.1-SNAPSHOT\myapp-2.1-SNAPSHOT.war]
2010-03-29 16:20:51.711::WARN:  Failed startup of context org.mortbay.jetty.webapp.WebAppContext@132e910{/myapp,c:\maven-repo\myapp\2.1-SNAPSHOT\myapp-2.1-SNAPSHOT.war}
java.lang.ClassCastException: org.mortbay.jetty.webapp.WebInfConfiguration cannot be cast to org.mortbay.jetty.webapp.Configuration
        at org.mortbay.jetty.webapp.WebAppContext.loadConfigurations(WebAppContext.java:801)
        at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:403)
        at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:40)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.cargo.container.jetty.Jetty6xEmbeddedLocalContainer.addHandler(Jetty6xEmbeddedLocalContainer.java:294)
        at org.codehaus.cargo.container.jetty.Jetty6xEmbeddedLocalDeployer.deployWebApp(Jetty6xEmbeddedLocalDeployer.java:77)
        at org.codehaus.cargo.container.jetty.internal.AbstractJettyEmbeddedLocalDeployer.deploy(AbstractJettyEmbeddedLocalDeployer.java:95)
        at org.codehaus.cargo.maven2.DeployerDeployMojo.performDeployerActionOnSingleDeployable(DeployerDeployMojo.java:79)
        at org.codehaus.cargo.maven2.AbstractDeployerMojo.performDeployerActionOnAllDeployables(AbstractDeployerMojo.java:104)
        at org.codehaus.cargo.maven2.AbstractDeployerMojo.doExecute(AbstractDeployerMojo.java:47)
        at org.codehaus.cargo.maven2.AbstractCargoMojo.execute(AbstractCargoMojo.java:255)
        at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
        at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)

Maven cargo settings:

<profile>
<id>container-cargo-jetty</id>
<properties>
    <skip.test.phase>true</skip.test.phase>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
            <version>1.0</version>
            <configuration>
                <wait>false</wait>
                <container>
                    <containerId>jetty6x</containerId>
                    <type>embedded</type>
                </container>
                <configuration>
                    <properties>
                        <cargo.servlet.port>8080</cargo.servlet.port>
                        <cargo.logging>medium</cargo.logging>
                    </properties>
                </configuration>
                <deployer>
                    <deployables>
                        <deployable>
                            <groupId>myapp</groupId>
                            <artifactId>myapp</artifactId>
                            <type>war</type>
                            <properties>
                                <context>/myapp</context>
                            </properties>
                        </deployable>
                    </deployables>
                </deployer>
            </configuration>
            <executions>
                <execution>
                    <id>start-container</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                        <goal>deployer-deploy</goal>
                    </goals>
                </execution>
                <execution>
                    <id>stop-container</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.mortbay.jetty</groupId>
                    <artifactId>jetty-embedded</artifactId>
                    <version>6.1.22</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

thx for help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

知足的幸福 2024-09-03 03:53:16

不支持指定 Cargo 使用的嵌入式 Jetty 版本(有一个关于此的旧的开放问题,请参阅

2010-03-29 16:20:46.715::INFO:  jetty-6.1.1rc1

因此,首先,删除插件配置中的 jetty 依赖项,这实际上是导致 ClassCastException 的原因。

但是一旦依赖关系被删除,我就遇到了另一个模糊的公共日志记录问题:

Caused by: org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy.  You have more than one version of 'org.apache.commons.logging.Log' visible, which is not allowed.

原因似乎是这部分:

            <execution>
                <id>start-container</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>start</goal>
                    <goal>deployer-deploy</goal>
                </goals>
            </execution>

说实话,我不知道为什么你在这里调用 deploy-deploy 目标。如果没有它,Cargo 就会部署声明的deployable。所以IMO没有必要指定这个目标。如果删除它,也不例外(第二个“修复”)。

总而言之,以下配置有效:

<profile>
  <id>container-cargo-jetty</id>
  <properties>
    <skip.test.phase>true</skip.test.phase>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.cargo</groupId>
        <artifactId>cargo-maven2-plugin</artifactId>
        <version>1.0</version>
        <configuration>
          <wait>false</wait>
          <container>
            <containerId>jetty6x</containerId>
            <type>embedded</type>
          </container>
          <configuration>
           <properties>
             <cargo.servlet.port>8080</cargo.servlet.port>
             <cargo.logging>medium</cargo.logging>
            </properties>
          </configuration>
          <deployer>
            <deployables>
              <deployable>
                <groupId>myapp</groupId>
                <artifactId>myapp</artifactId>
                <type>war</type>
                <properties>
                  <context>/myapp</context>
                </properties>
              </deployable>
            </deployables>
          </deployer>
        </configuration>
        <executions>
          <execution>
            <id>start-container</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>start</goal>
            </goals>
          </execution>
          <execution>
            <id>stop-container</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>stop</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

Specifying the version of the embedded Jetty used by Cargo is not supported (there is an old open issue about that, see CARGO-571). And indeed, if we look at the log, we see that it uses jetty-6.1.1rc1:

2010-03-29 16:20:46.715::INFO:  jetty-6.1.1rc1

So, first, remove the jetty dependency inside the plugin configuration which is actually the cause of the ClassCastException.

But once the dependency removed, I faced another obscure commons-logging issue:

Caused by: org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy.  You have more than one version of 'org.apache.commons.logging.Log' visible, which is not allowed.

And the cause seems to be this part:

            <execution>
                <id>start-container</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>start</goal>
                    <goal>deployer-deploy</goal>
                </goals>
            </execution>

To be honest, I don't know why you call the deploy-deploy goal here. Without it, Cargo does deploy the declared deployable. So there is IMO no need to specify this goal. And if you remove it, there is no exception (and that the second "fix").

To summarize, the following configuration works:

<profile>
  <id>container-cargo-jetty</id>
  <properties>
    <skip.test.phase>true</skip.test.phase>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.cargo</groupId>
        <artifactId>cargo-maven2-plugin</artifactId>
        <version>1.0</version>
        <configuration>
          <wait>false</wait>
          <container>
            <containerId>jetty6x</containerId>
            <type>embedded</type>
          </container>
          <configuration>
           <properties>
             <cargo.servlet.port>8080</cargo.servlet.port>
             <cargo.logging>medium</cargo.logging>
            </properties>
          </configuration>
          <deployer>
            <deployables>
              <deployable>
                <groupId>myapp</groupId>
                <artifactId>myapp</artifactId>
                <type>war</type>
                <properties>
                  <context>/myapp</context>
                </properties>
              </deployable>
            </deployables>
          </deployer>
        </configuration>
        <executions>
          <execution>
            <id>start-container</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>start</goal>
            </goals>
          </execution>
          <execution>
            <id>stop-container</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>stop</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
潦草背影 2024-09-03 03:53:16

您可能正在处理 JETTY 中的“类加载器地狱”,它使用 JETTY 实例的父类加载器,该父类加载器与您的 Web 应用程序使用的类加载器是分开的,因此即使它们应该是相同的类,但它们并不是。

JETTY 配置中有一些选项可以强制使用相同的类加载器,这可能有助于您的集成测试。

类加载 - JETTY

Jetty提供配置选项
控制所有这三个选项。
方法
org.mortbay.jetty.webapp.WebAppContext.setParentLoaderPriority(boolean)
允许正常的 java 2 行为
被使用并且所有类都将被加载
如果可能的话,从系统类路径。
如果图书馆的话这非常有用
Web 应用程序使用的具有
加载两者的类时出现问题
在 Web 应用程序和系统上
类路径。

我不确定如何访问 Cargo 中的 JETTY 配置,但在普通的 JETTY maven 插件中,您会执行以下操作:

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>7.0.0.pre5</version>
    <configuration>
        <jettyConfig>${jetty.configs}</jettyConfig>
        <reload>manual</reload>
        <contextPath>/</contextPath>
        <webAppConfig>
            <parentLoaderPriority>true</parentLoaderPriority>

        </webAppConfig>
    </configuration>
</plugin>

You may be dealing with "classloader hell" in JETTY, where it uses a parent classloader for the JETTY instance which is separate from the one used for your webapplication and so even though they should be identical classes they aren't.

There are some options in the JETTY configuration to force use of the same classloader, that may help in your integration tests.

Classloading - JETTY

Jetty provides configuration options
to control all three of these options.
The method
org.mortbay.jetty.webapp.WebAppContext.setParentLoaderPriority(boolean)
allows the normal java 2 behaviour to
be used and all classes will be loaded
from the system classpath if possible.
This is very useful if the libraries
that a web application uses are having
problems loading classes that are both
in a web application and on the system
classpath.

I'm not sure how you access JETTY configuration in Cargo, but in the normal JETTY maven plugin you would do something like:

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>7.0.0.pre5</version>
    <configuration>
        <jettyConfig>${jetty.configs}</jettyConfig>
        <reload>manual</reload>
        <contextPath>/</contextPath>
        <webAppConfig>
            <parentLoaderPriority>true</parentLoaderPriority>

        </webAppConfig>
    </configuration>
</plugin>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文