Maven shade unable to locate Spring NamespaceHandl

2019-07-18 23:14发布

I've a small Spring project, that I've booted up with roo 1.2.2

I can run the main class just fine within Eclipse Juno. However when I try to run the JAR file built with mvn package, I get the following error:

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]

I am using Maven shade plugin to build the uber JAR, with the following configuration:

<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>
            ...

In the pom.xml properties I have <spring.version>3.1.2.RELEASE</spring.version> and one of the dependencies is:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
</dependency>

The application context header is as follows:

<?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">

In my main program I have:

String[] springConf = new String[] { "META-INF/spring/applicationContext.xml",
    "META-INF/spring/watcher.xml" };
BeanFactory appContext = new ClassPathXmlApplicationContext(springConf);

When I type mvn package I get

[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.

Anyone can suggest what I am missing and how to fix my build so that I can run my JAR?

1条回答
Melony?
2楼-- · 2019-07-18 23:57

Try adding an AppendingTransformer to your config. The example in maven documentation specifically mentions this as being useful for Spring handlers.

<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>

Hope this helps.

查看更多
登录 后发表回答