2010-07-26 78 views
15

我正在用Spring創建一個獨立的Sava應用程序來處理JDBC訪問。該應用程序在每個測試中都能正常工作,並且我決定需要一個jar來部署我們的客戶。另一個「無法找到Spring NamespaceHandler」錯誤

它們在類路徑中可能沒有彈簧,所以我使用maven-assembly-plugin來處理具有依賴關係的jar創建。

然而,當我嘗試運行應用程序:

java -jar target/myproject-0.0.1-SNAPSHOT-jar-with-dependencies.jar 

會拋出以下錯誤:

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/p] 
Offending resource: class path resource [applicationContext.xml] 

at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68) 
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85) 
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80) 
...and so on to the database access class of this project. 

applicationContext.xml文件是在projectbase/src目錄/主/資源。並將其放置在目標/包名稱基礎上。

applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 


    <bean id="dataSourceDesenv" class="org.apache.commons.dbcp.BasicDataSource"... /> 

    <bean id="simpleJdbcDaoSupport" class="org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport" 
     p:dataSource-ref="dataSourceDesenv" /> 

    <bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate"> 
     <constructor-arg ref="dataSourceDesenv" /> 
    </bean> 

</beans> 

這是所有我能想到這可能是有用的。如果需要,我會提供更多信息。

+0

該錯誤意味着applicationContext.xml中缺少頭文件。請張貼這個文件,或者至少是標題和根元素以及一些樣本。 – 2010-07-26 13:42:05

回答

8

我發現錯誤,錯誤在於unfixed bug in the maven-assembly plugin。 我使用了以下解決方法:

首先在我的pom中註釋了maven-assembly代碼。

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-dependency-plugin</artifactId> 
    <executions> 
     <execution> 
      <id>copy-dependencies</id> 
      <phase>package</phase> 
      <goals> 
       <goal>copy-dependencies</goal> 
      </goals> 
      <configuration> 
       <outputDirectory>${project.build.directory}/lib</outputDirectory> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

然後我用了Maven的罐子,插件設置我的可執行的JAR:

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jar-plugin</artifactId> 
      <version>2.3.1</version> 
      <configuration> 
       <archive> 
        <index>true</index> 
        <manifest> 
         <addClasspath>true</addClasspath> 
         <mainClass>org.foo.myproject.App</mainClass> 
        </manifest> 
        <manifestEntries> 
         <mode>development</mode> 
         <url>${pom.url}</url> 
         <key>value</key> 

        </manifestEntries> 
       </archive> 
      </configuration> 
     </plugin> 

最後然後我用maben依賴性-插件複製的依賴關係到lib文件夾的目標我創建了部署與應用程序中的bash腳本運行我的應用程序的庫和任何提供的參數:

java -cp lib/*:myproject-0.0.1-SNAPSHOT.jar org.foo.myproject.App [email protected] 

我應該已經建立了應用程序在python =/

4

看起來像Maven Assembly Plugin中的一個錯誤 - MASSEMBLY-360,正如本博客條目here中所討論的。

簡而言之,處理Spring命名空間的Spring JAR中的元數據文件正在被maven破壞。

29

我今天用maven-assembly-plugin遇到了這個問題。搜索引起了我的這個問題,並且看到一個錯誤報告提示我可能使用了錯誤的插件。所以我轉而使用maven-shade-plugin。據我所知,它完美運作。我有一個可執行的jar包含了許多Spring模塊以及Apache MQ。下面是從我的pom.xml的相關位:

<build> 
<finalName>sample-broker</finalName> 
<plugins> 
    ... 
    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-shade-plugin</artifactId> 
    <version>2.0</version> 
    <executions> 
     <execution> 
     <phase>package</phase> 
     <goals> 
      <goal>shade</goal> 
     </goals> 
     <configuration> 
      <transformers> 
      <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 
       <resource>META-INF/spring.handlers</resource> 
      </transformer> 
      <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 
       <resource>META-INF/spring.schemas</resource> 
      </transformer> 
      <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
       <mainClass>org.XYZ</mainClass> 
      </transformer> 
      </transformers> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 
</plugins> 
</build> 
+1

完美!!!!!!! – 2013-06-03 20:21:18

+0

謝謝你的直接回答。這解決了我使用maven-assembly-plugin時遇到的所有晦澀問題,而我不得不浪費時間去理解爲什麼! – 2014-03-09 23:23:20

+0

這對我來說非常合適!謝謝! – madx 2015-09-22 22:37:21

2

onejar - Maven的插件,您可以通過創建具有相關性的一個jar文件解決Maven和春天的矛盾。它通過將它放置在另一個jar(一個jar)中創建一個圍繞你的jar文件的包裝器。

<plugin> 
      <groupId>org.dstovall</groupId> 
      <artifactId>onejar-maven-plugin</artifactId> 
      <version>1.4.4</version> 
      <executions> 
       <execution> 
       <goals> 
        <goal>one-jar</goal> 
       </goals> 
       </execution> 
      </executions> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jar-plugin</artifactId> 
      <version>2.3.2</version> 
      <configuration> 
       <archive> 
        <manifest> 
         <mainClass>your.package.App</mainClass> 
        </manifest> 
       </archive> 
      </configuration> 
     </plugin> 

<!-- outside build tag in pom.xml --> 
<pluginRepositories> 
     <pluginRepository> 
      <id>onejar-maven-plugin.googlecode.com</id> 
      <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url> 
     </pluginRepository> 
</pluginRepositories> 
0

除了張貼@GrampaJohn的解決方案,我也不得不確保我包裝所需的庫到生成的JAR文件,而不是將其解壓到生成JAR的。

在Eclipse火星,在更改的pom.xml(如由@GrampaJohn建議加入行家蔭插件)後,按照下列步驟:

File -> Export -> Select Java folder ->Runnable JAR File -> enter the Launch configuration (main file), export destination, and in Library Handling, select "Package required Libraries into generated JAR -> Click finish

相關問題