views:

74

answers:

2

I'm using Maven2 and Spring 3, when I run my project in Eclipse everything works fine, but when I use assembly:assembly, the resultant jar throws the following Exception:

Exception in thread "main" 
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 4 
in XML document from class path resource [beans.xml] is invalid; nested exception 
is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of 
element 'beans'.

my beans file looks something like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt; 
    <!-- Beans Here -->
</beans>

this file is stored in src/main/resources

my pom.xml has the following dependency for spring:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.0.3.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

any ideas why this is happening? how to fix?

UPDATE:

further probing Google turns out that Spring and Maven don't get on too well with the following in my pom.xml, although no solution is forthcoming:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-5</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>org.robert.xclades.Main</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>
+1  A: 

The problem, it seems, is that there is conflict between each of the different spring.handlers and spring.schemas in the META-INF folders of the various spring dependencies.

The trick I found is to override these by creating these files in src/main/resources/META-INF and copying and pasting the contents of those individual files into these. Secondly, use the maven-jar-plugin to set the manifest's mainClass; and the maven-shade-plugin, instead of the maven-assembly-plugin. So the updated pom.xml looks like:

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
              <mainClass>org.robert.xclades.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

So all that is required now is maven package.

Robert
+1  A: 

I had the same problem. Using the shade plugin helped, but I had to add an transformer as described in http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html to concat Spring's handler and schema files.

Fin Labusch
Thanks, that seems a much nicer way to merge the files!
Robert