2010-03-29 85 views
2

我有一個用java編寫的web應用程序。使用maven和嵌入式碼頭:ClassCastException

我想在嵌入式碼頭服務器上運行集成測試。

爲此我有一個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 [email protected]{/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> 

THX的幫助

回答

2

指定由貨物所使用的嵌入碼頭的版本不支持(有一箇舊的公開問題,請參閱CARGO-571)。事實上,如果我們看一下日誌,我們可以看到它使用碼頭-6.1.1rc1:

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

因此,首先,除去這實際上是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> 
+0

如果沒有目標「部署部署」的應用程序將未部署。 Apache公用日誌記錄存在類加載器問題。最後我切換到jetty-maven-plugin。 – amra 2010-04-02 16:41:06

+0

@amra奇怪的是,它沒有'部署 - 部署'工作對我來說(我在發佈前測試過)。 – 2010-04-02 16:55:32

2

您可能處理「classl oader hell「,在JETTY中,它使用JETTY實例的父類加載器,它與用於Web應用程序的JETTY實例分開,因此即使它們應該是相同的類,它們也不是。

JETTY配置中有一些選項可以強制使用相同的類加載器,這可能有助於集成測試。

Classloading - JETTY

碼頭提供配置選項 來控制所有這三個選項。 方法 org.mortbay.jetty.webapp.WebAppContext.setParentLoaderPriority(布爾值) 允許正常的Java 2行爲 可以使用,如果可能,所有的類將被裝入 從系統類路徑。 如果網絡應用程序使用的庫 在加載類應用程序中的 類和 類路徑中存在 問題,這非常有用。

我不知道你如何接貨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>