14

I am using spring and hibernate in my java project which is managed by maven. I created an assembly (jar with dependencies) using following command mvn install assembly:assembly

Now, when I am trying to run my main class with the command: java -cp xyz-1.0-SNAPSHOT-jar-with-dependencies.jar com.xyz.class then I am getting following error:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/data/jpa]**
Offending resource: class path resource [xyz-component-scans-config.xml]

    at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:76)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:271)
.
.

I am not understanding that why it is not able to find the NamespaceHandler? as I already have following dependencies in pom.

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>3.1.0.RELEASE</version
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-jpa</artifactId>
      <version>1.0.2.RELEASE</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency> 
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>

I did try the suggestion in the following thread, but it didn't work for me. Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/data/jpa]

Source code for org.springframework.beans.factory.parsing.BeanDefinitionParsingException

Arry
  • 1,630
  • 9
  • 27
  • 46
  • 4
    The schema is most likely stripped out by the maven assembly plugin, see http://stackoverflow.com/questions/5586515/idea-to-avoid-that-spring-handlers-spring-schemas-get-overwritten-when-merging-m –  Oct 27 '13 at 07:35
  • @RC.:But my project runs fine when I am running this through eclipse. The problem is when I run it via a packaged jar (jar with dependencies). Any comment on this? – Arry Oct 27 '13 at 07:38
  • "maven assembly plugin" = what makes the jar with dependency, so.. –  Oct 27 '13 at 07:41
  • Any work around for this? – Arry Oct 27 '13 at 07:42
  • see the linked question answer! –  Oct 27 '13 at 07:43
  • @RC.: No luck! I tried generating jar with maven-shade-plugin, but still same issue while running the new jar. – Arry Oct 27 '13 at 07:52
  • If you have configured the pom as stated in the linked answer and `mvn clean package` does not provide a working jar, please edit you question and add pom config and details on how you run etc –  Oct 27 '13 at 09:24
  • With that assembly, first (or latest, depending of overrides) spring.schemas will end up to be the only one in your JAR. You need to package it differently, having each library packaged inside its own JAR, like with the https://code.google.com/p/onejar-maven-plugin/ – Tome Nov 27 '13 at 10:45

4 Answers4

14

You may be better off using the maven-shade-plugin to create your jar with dependencies. Here is an example of how to use the plugin:

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-shade-plugin</artifactId>
          <version>2.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.stackexchange.stackoverflow.ExecutableJar</mainClass>
                  </transformer>
                  <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>

In my experience the maven-shade-plugin is the best way to create an uber jar. See my other SO answer for a more complete example. Note that this example doesn't require any 3rd party jars, but the maven-shade-plugin does handle them nicely. Give it a shot! :-)

Community
  • 1
  • 1
axiopisty
  • 4,972
  • 8
  • 44
  • 73
1

I used the [one-jar] plugin (https://code.google.com/p/onejar-maven-plugin/) for this.

I was having the same issue; namely maven assembly was messing up my spring.schema files. (Maven's plugin has been known to do this (spring forum link from another person experiencing the same issue)).

If you really want to know what's going on here, expand your .jar file and look at the spring.schema and spring.handlers files. Look at the product of maven's assembly plugin, read this (Need understanding of spring.handlers and spring.schemas) stack overflow post that explains how those files are used.

Community
  • 1
  • 1
zmf
  • 9,095
  • 2
  • 26
  • 28
  • 1
    Exactly, spring.schemas files on every Spring module are located at the same place, with the same name. So unjaring all the Spring jars to make a single JAR cannot work. You have to package each JAR separately, otherwise you will only end up with one spring.schemas file. One-jar plugin can definitely help. That is for instance what the Spring Boot Maven plugin does (http://projects.spring.io/spring-boot/docs/spring-boot-tools/spring-boot-maven-plugin/README.html) – Tome Nov 27 '13 at 10:42
0

Can you make sure that you have Spring jar inside your WEB-INF/lib folder?

And also make sure that only 1 version exists.

If it still doesn't work, it will be helpful if you can attach the header of the configuration, in addition of the part of the body that you already pasted.

-2

I'm not really familiar with the maven assembly goal but the fact you've scoped these dependencies to compile time only could mean that these dependencies aren't included in the assembly. Could you try removing the:

<scope>compile</scope>

Parts from your dependency declarations?

NickDK
  • 5,159
  • 2
  • 18
  • 11
  • This is unrelated to the problem, and removing the `compile` from the dependency will not have any affect. In maven, the default scope for dependencies is compile. – axiopisty Nov 26 '13 at 23:26