2

I have looked around and SO and I have not found a solution that has solved this issue for me. I have a Maven project using Spring and I call assembly-single and build a runnable jar. This project works fine from the IDE but when I run it as the runnable jar I get the following exception:

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 8 in
 XML document from class path resource [properties.xml] is invalid; nested excep
tion is org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 62; cvc-elt.
1.a: Cannot find the declaration of element 'beans'.
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadB
eanDefinitions(XmlBeanDefinitionReader.java:396)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:334)
[...]

My properties.xml file looks like the following. Notice I have schemaLocation correct and line 8 is http://www.springframework.org/schema/context

<?xml version="1.0" encoding="UTF-8"?>
<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:aop="http://www.springframework.org/schema/aop"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <context:property-placeholder
        location="classpath:test.properties"  system-properties-mode="OVERRIDE"/>

<!-- -->

</beans>
Smeiff
  • 259
  • 1
  • 6
  • 17

2 Answers2

2

Looking around to other solutions I saw that some people suggested putting the classpath of the xsd directly into the beans tag. So I went ahead and tried 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 
classpath:/org/springframework/beans/factory/xml/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
classpath:/org/springframework/context/config/spring-context-3.2.xsd
http://www.springframework.org/schema/util 
classpath:/org/springframework/beans/factory/xml/spring-util-3.2.xsd">
</beans>

This solution seemed to work for the beans but not for context. My next solution I looked into an article I found a while ago stating that it might be related to Maven overwriting the spring.schemas file and not appending to the file (this solution). I realized that my spring.schemas only included MVC schemas so I looked into the suggestion of using Maven Shade to build my jar (using this as an example). Shade will allow for a transformer that will tell maven to append to the file rather than overwrite allowing for multiple dependencies to use the same file.

Final pom:

<build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.mainClass</mainClass>
                        </manifest>
                    </archive>
                    <shadedArtifactAttached>true</shadedArtifactAttached>
                    <shadedClassifierName>jar-with-dependencies</shadedClassifierName>
                    <finalName>Filename</finalName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <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>

            </plugin>
        </plugins>

    </build>
Community
  • 1
  • 1
Smeiff
  • 259
  • 1
  • 6
  • 17
0

As you are in an offline mode, I assume Spring validates your beans using schemas from its own jar. Thus you should use an aligned versions for the xml schemas as of the spring dependency version.

In your properties.xml descriptor I can see you are using the 3.0 schema version so you should be using the 3.0.x dependency version, otherwise update your schema version to match the Spring version.

Edit:

Since you are using the 3.2 version, update your xml bean descriptor to fit in:

<?xml version="1.0" encoding="UTF-8"?>
<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:aop="http://www.springframework.org/schema/aop"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- -->
</beans>
tmarwen
  • 15,750
  • 5
  • 43
  • 62
  • Offline mode isn't the issue I validated I have the issue on my own machine as well. When I look at the spring-beans.jar package I see the .xsd for 3.0 format versus 3.0.x format. Do you mean to change the .xsd references? – Smeiff Aug 19 '14 at 13:13
  • I did end up using 3.2 on my own but there was more to the issue than this. I think 3.0 would have worked fine as well since the issue didn't seem to be with the schema location. See my answer I posted. It is my solution. – Smeiff Aug 19 '14 at 16:28