Unable to locate Spring NamespaceHandler for XML schema namespace http://www.springframework.org/schema/context

maven-assembly-pluginを利用してjarを作成した時にエラーが発生した。

■実行したコマンド
>mvn assembly:assembly

■エラー内容

Unable to locate Spring NamespaceHandler for XML schema namespace http://www.springframework.org/schema/context

■エラー原因
jar作成時に、xsdの情報が記述されている下記2つののファイルが上書きされているため。
META-INF/spring.handlers
META-INF/spring.schemas

参照サイト
http://forum.springsource.org/showthread.php?48787-Unable-to-locate-NamespaceHandler-when-using-context-annotation-config
http://d.hatena.ne.jp/garbagetown/20091105/1257433226

■解決方法
META-INF/spring.handlers、META-INF/spring.schemasを自分で作成し上書きされないようにする。

(1) maven-assembly-pluginの設定ファイルを編集する。
META-INF/spring.handlers、META-INF/spring.schemasのパスを設定する。

<?xml version="1.0" encoding="UTF-8"?>

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>final</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <unpack>true</unpack>
            <unpackOptions>
                <excludes>
                    <exclude>META-INF/spring.handlers</exclude>
                    <exclude>META-INF/spring.schemas</exclude>
                </excludes>
            </unpackOptions>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
    <files>
    <file>
        <source>${project.basedir}/config/META-INF/spring.handlers</source>
        <outputDirectory>META-INF</outputDirectory>
    </file>
    <file>
        <source>${project.basedir}/config/META-INF/spring.schemas</source>
        <outputDirectory>META-INF</outputDirectory>
    </file>
    </files>
</assembly>

(2) spring.handlersを作成する

http://www.springframework.org/schema/tx=org.springframework.transaction.config.TxNamespaceHandler
http://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler

ちなみに、自動作成されていたspring.handlersは以下のような内容になっていた。トランザクション関係のSpringのxsdのみが書かれている。

http://www.springframework.org/schema/tx=org.springframework.transaction.config.TxNamespaceHandler

(3) spring.schemasを作成する

http://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd=org/springframework/transaction/config/spring-tx-3.0.xsd
http://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-3.0.xsd
http://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd
http://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd

自動作成されていたspring.schemasは以下のような内容になっていた。

http://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd=org/springframework/transaction/config/spring-tx-3.0.xsd
http://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-3.0.xsd

Springのバージョンはjarを展開して確認する。



■補足:maven-assembly-pluginの導入方法
pom.xmlプラグインを追加する。に設定ファイルのパスを記述する。

  <build>
    <plugins>

      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <descriptors>
            <descriptor>src/main/assembly/distribution.xml</descriptor>
          </descriptors>
        </configuration>
      </plugin>
    
    </plugins>
  </build>

設定ファイル(distribution.xml)を作成する。

<?xml version="1.0" encoding="UTF-8"?>

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>final</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <unpack>true</unpack>
            <unpackOptions>
                <excludes>
                    <exclude>META-INF/spring.handlers</exclude>
                    <exclude>META-INF/spring.schemas</exclude>
                </excludes>
            </unpackOptions>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
    <files>
    <file>
        <source>${project.basedir}/config/META-INF/spring.handlers</source>
        <outputDirectory>META-INF</outputDirectory>
    </file>
    <file>
        <source>${project.basedir}/config/META-INF/spring.schemas</source>
        <outputDirectory>META-INF</outputDirectory>
    </file>
    </files>
</assembly>