3

我有一个小的 Spring 项目,我已经用 roo 1.2.2 启动了

我可以在 Eclipse Juno 中很好地运行主类。但是,当我尝试运行使用 构建的 JAR 文件时mvn package,出现以下错误:

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 插件来构建 uber 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?

4

1 回答 1

3

尝试将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>

希望这可以帮助。

于 2015-12-17T11:40:38.837 回答