Problems starting from command line Spring 3 and 4 applications packaged as one fat jar using maven-assembly-plugin due to ‘Failed to read schema document ‘http://www.springframework.org/schema/beans/spring-beans.xsd’

I have my simple Spring (release 3 or 4) Java application that I have packaged into single fat jar with all the dependencies using maven-assembly-plugin. Not then trying to jar if as a standalone Java application via:

java -jar target/my-spring-4-xml-0.0.1-SNAPSHOT-jar-with-dependencies.jar

I get the error:

org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 128; schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-beans.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not
...
Caused by: java.net.UnknownHostException: http://www.springframework.org

./pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.bawi.spring</groupId>
	<artifactId>my-spring-4-xml</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>

		<!-- Generic properties -->
		<java.version>1.8</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

		<!-- Spring -->
		<spring-framework.version>4.1.6.RELEASE</spring-framework.version>

	</properties>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.5.5</version>
				<configuration>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<archive>
						<manifest>
							<mainClass>com.bawi.spring.MyApplication</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

	<dependencies>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>

	</dependencies>
</project>

./src/main/resources/application-context.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="myClass" class="com.bawi.spring.MyClass" />

</beans>

./src/main/java/com/bawi/MyClass.java:

package com.bawi.spring;

public class MyClass {

	public void doSth() {
		System.out.println("MyClass started");
	}
}

./src/main/java/com/bawi/MyApplication.java:

package com.bawi.spring;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyApplication {

	public static void main(String[] args) {
		ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("/application-context.xml");
		MyClass myClass = context.getBean(MyClass.class);
		myClass.doSth();
		context.close();
	}
}

When unzipped the my-spring-4-xml-0.0.1-SNAPSHOT-jar-with-dependencies.jar we get no spring-beans in
target/META-INF/spring.schemas

Solution:
Modify your pom.xml dependencies to add spring-beans at the top:

	<dependencies>

		<!-- spring-beans needs to be explicitly at the top (before spring-context) when assembling one fat jar with maven-assembly-plugin so that META-INF/spring.schemas (with spring-beans.xsd) from spring-beans with be packaged instead one from spring-context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>

	</dependencies>

After applying the fix the unzipped my-spring-4-xml-0.0.1-SNAPSHOT-jar-with-dependencies.jar has
target/META-INF/spring.schemas with:
$ cat META-INF/spring.schemas | grep spring-beans

http\://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd
http\://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.2.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd
http\://www.springframework.org/schema/beans/spring-beans-4.0.xsd=org/springframework/beans/factory/xml/spring-beans-4.0.xsd
http\://www.springframework.org/schema/beans/spring-beans-4.1.xsd=org/springframework/beans/factory/xml/spring-beans-4.1.xsd
http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-4.1.xsd
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s