我有一个小型的Spring项目,我已经使用roo 1.2.2启动了
我可以在Eclipse Juno中运行主类。但是,当我尝试运行使用mvn package
构建的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/tx]
Offending resource: class path resource [META-INF/spring/applicationContext.xml]
我正在使用Maven shade插件来构建超级JAR,具有以下配置:
<build>
<pluginManagement>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.7.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.xyz.watcher.WatcherMain</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
...
在pom.xml属性中,我有<spring.version>3.1.2.RELEASE</spring.version>
,其中一个依赖项是:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
应用程序上下文标题如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
在我的主程序中,我有:
String[] springConf = new String[] { "META-INF/spring/applicationContext.xml",
"META-INF/spring/watcher.xml" };
BeanFactory appContext = new ClassPathXmlApplicationContext(springConf);
当我输入mvn package
时,我得到了
[INFO] Building jar: /home/stivlo/workspace/monitor/target/monitor-0.1.0.BUILD-SNAPSHOT.jar
[INFO] --- maven-shade-plugin:1.7.1:shade (default) @ monitor ---
...
[INFO] Including org.springframework:spring-tx:jar:3.1.2.RELEASE in the shaded JAR.
任何人都可以建议我缺少什么以及如何修复我的构建以便我可以运行我的JAR?
答案 0 :(得分:2)
尝试在配置中添加 AppendingTransformer 。 maven文档中的示例特别提到这对Spring处理程序很有用。
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</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>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
希望这有帮助。