May 2010 17

I’m using the maven-shade-plugin to build a standalone spring-based java application.
Have you ever seen this complaint from spring:

Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/util]

Well, here’s the thing. Inside all those spring jars, in the META-INF directory you’ll see a file

META-INF/spring.handlers

and another file

META-INF/spring.schemas

Everyone of the spring jars, spring-core and spring-context and spring-aop and so on contains a directory META-INF which contains spring.handlers and spring.schemas files. Most or all of them also have the particular schema file contained as well — this lets you have your spring contexts validated even if you’re running offline.

Here’s what the spring.schemas file looks like if you open up spring-aop-3.0.2.RELEASE.jar and have a look:

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

Notice you do NOT see anything about spring-context or spring-util or spring-task … and so on.

Now the maven-shade-plugin basically expands the jars and rewrites them into the work directory before jarring everything back up again. This normally works fine when you don’t have naming and pathing COLLISIONS, like you do with these schema files. So whichever spring jar was processed last by maven-shade will have its META-INF/* contents written into the final jar you’re creating, overwriting any other content with the same name.

So if you’re using a spring context file that looks something like this:

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

  <context :annotation-config />

  <util :properties id="defaultProperties"
   location="classpath:/spring/defaultScraperExecution.properties"/>

</beans>

…you will get the complaint mentioned in the title of this blog entry. Spring can’t find the namespace handler for “util”, because it is not mentioned in the META-INF/spring.schemas that result in the final product.

Here’s the solution – you need to take advantage of the example in the maven-shade-plugin page and do this in your configuration section:

<configuration>
  <transformers>
  <!--
 must be SURE to do this with both spring.handlers and spring.schemas. otherwise
 you won't be able to use them in the spring config files.
  -->
    <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>

The maven-shade-plugin will append the contents of all META-INF/spring.schemas and META-INF/spring.handlers files it finds during the expansion.

FacebookShare

10 Responses to “Maven-shade-plugin and Spring: Unable to locate Spring NamespaceHandler for XML schema namespace”

  1. Scott Scott says:

    Howdy there,just found your web-site when i google something and wonder what webhosting do you use for your wordpress,the speed is more faster than my website, i really need to know it.will back to check it out,i appreciate it!

  2. baresytapas says:

    I have visited this blog by accident, but I found it quite interesting. You’re doing a good job (and most importantly it’s free). A greeting.

  3. Free Artikel says:

    Brilliant, it runs fairly wonderful. Thanks

  4. WP Themes says:

    Amiable fill someone in on and this fill someone in on helped me alot in my college assignement. Gratefulness you seeking your information.

  5. Genial fill someone in on and this fill someone in on helped me alot in my college assignement. Thanks you for your information.

  6. ogłoszenia says:

    Hey very nice site!! Man .. Beautiful .. Amazing .. I’ll bookmark your website and take the feeds also…I’m happy to find a lot of useful information here in the post, we need work out more techniques in this regard, thanks for sharing. . . . . .

  7. Andrew Swan says:

    This was a really useful tip; I’ve linked to this post from StackOverflow.

  8. Never seen such cool publish. I just read it all the way to the end. Have them coming. Now you appear on my personal weblog .

preload preload preload

Bad Behavior has blocked 572 access attempts in the last 7 days.