views:

107

answers:

3

I am able to compile and start my Spring project using Maven:

mvn -e  clean  compile  exec:java -Dexec.mainClass=de.fraunhofer.fkie.tet.vmware.manager.Test

However, when i assemble all jars in a single file using the maven-assembly-plugin (including applicationContext.xml), i always get an Exception during the java call:

java -cp target/test-jar-with-dependencies.jar:.  de.fraunhofer.fkie.tet.vmware.manager.Test

  INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
  Sep 6, 2010 10:37:21 AM org.springframework.util.xml.SimpleSaxErrorHandler warning
  WARNING: Ignored XML validation warning
  org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/context/spring-context.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
  ...
  Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: 
  Line 10 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: 
  The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'.
  ...
  Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: 
  The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'.

I also tried to attach the schema definitions, i.e. spring-context.xsd etc., directly to the classpath, but without any success.

less src/main/resources/applicationContext.xml

  <?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"
         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"&gt;

      <context:annotation-config />   <!-- wegen '@PostConstruct' -->
  <!--<context:component-scan base-package="de.fraunhofer" />     -->

  ...
A: 

I suspect your spring config file is missing the context XML namespace. It should be added to the root element of your spring config file 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" 
    xmlns:context="http://www.springframework.org/schema/context"
    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"&gt;

    <context:annotation-config />
    <context:component-scan base-package="com.abc.xyz" />

</beans>
abhin4v
Thanks for the rapid answer! I didn't post my applicationContext.xml file, because 'mvn exec:java -Dexec.mainClass=de.fraunhofer.fkie.tet.vmware.manager.Test' works fine. So i think there is nothing wrong with the configuration file in principal, it actually contains all elements from your example above. The problem occurs, when i try to start the assembled version directly from Java.
rmv
A: 

What spring dependencies do you have in your pom? You may get xml parsing errors due to some spring jar files not being on the class path. In spring 3 the library was split up into many jar files. Check out this post to see what you need, specifically:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
krock
Thanks for the hint, but 'spring-context.jar' is already assembled within 'target/test-jar-with-dependencies.jar', i.e. the final jar contains 'org/springframework/context/config/spring-context-3.0.xsd'. By the way: we are connected with the internet via a proxy server. Is it possible, that Maven uses some system-wide proxy settings, while Java is unable to connect and check '.xsd' documents from the web?
rmv
Update: it has nothing to do with proxy issues as i verified by a test run at home directly connected to the internet.
rmv
+2  A: 

Spring namespace handlers are resolved using files /META-INF/spring.schemas and /META-INF/spring.handlers. Because files with these names exist in different Spring jars, probably only one of them remains in target jar after maven-assembly-plugin.

Perhaps you may merge these files manually and somehow configure maven-assembly-plugin to overwrite file in target jar with this merged file.

axtavt
Yes, you are right, that's it! The independent '/META-INF/spring.handlers' files from different jars get lost in the final jar. Facing this, i decide not to use the 'maven-assembly-plugin' for now, but include all jars manually from the 'target/lib/' directory (after 'mvn install'), e.g. 'java -cp target/lib/spring-aop-3.0.4.RELEASE.jar:target/lib/spring-asm-3.0.4.RELEASE.jar:[...]:target/classes de.fraunhofer.fkie.tet.vmware.manager.Test'. This works as expected. Thank you very much! ;-)
rmv