坑爹的maven插件-assembly

今天项目需要使用maven的assembly插件打包成一个可执行的jar包,main方法类如下:

public class AppClientServiceStart {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				"classpath:spring/spring-context.xml");
		com.alibaba.dubbo.container.Main.main(args);
	}
}

其实就是启动dubbo服务,由于里面有依赖spring,使用assembly插件代码如下:

			
				org.apache.maven.plugins
				maven-assembly-plugin
				
					false
					
						jar-with-dependencies
					
					
						
							com.XXX.AppClientServiceStart
							true
						
					
				
				
					
						make-assembly
						package
						
							assembly
						
					
				
			
在执行其打包出的jar时,一直报错,错误信息如下:

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:spring/spring-datasource.xml]
Offending resource: URL [jar:file:/Users/macbook/workspace/mishi-cmc/cmc-service/appclient/biz/target/mishi-cmc-appclient-biz.jar!/spring/spring-context.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]
Offending resource: class path resource [spring/spring-datasource.xml]
开始一直以为是spring的问题,在网上查阅了很多资料,有些说是没有把classpath添加进去。但是测试结果依然是没效果。最后找到这两篇文章

http://blog.csdn.net/bluishglc/article/details/6085209 http://ju.outofmemory.cn/entry/48544。原来是assembly在打包时只会把第一次遇到的文件打入jar包,后面遇到的都会skip掉。按照这种方法试验了一下:

			
				org.apache.maven.plugins
				maven-shade-plugin
				
					
						package
						
							shade
						
						
							cmc
							true
							jar-with-dependencies
							
								
									com.mishi.cmc.appclient.AppClientServiceStart
								
								
									META-INF/spring.handlers
								
								
									META-INF/spring.schemas
								
								
									META-INF/spring.tooling
								
							
						
					
				
			

上面的这个错误确实是没有了,但是新的问题出现了,在运行jar时出现下面的错误:

macbookdeMacBook-Air:target macbook$ java -jar cmc.jar
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
	at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:286)
	at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:239)
	at java.util.jar.JarVerifier.processEntry(JarVerifier.java:317)
	at java.util.jar.JarVerifier.update(JarVerifier.java:228)
	at java.util.jar.JarFile.initializeVerifier(JarFile.java:348)
	at java.util.jar.JarFile.getInputStream(JarFile.java:415)
	at sun.misc.URLClassPath$JarLoader$2.getInputStream(URLClassPath.java:775)
	at sun.misc.Resource.cachedInputStream(Resource.java:77)
	at sun.misc.Resource.getByteBuffer(Resource.java:160)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:436)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
	at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
继续Google后发现,原来还少一段代码,最终代码如下:

			
				org.apache.maven.plugins
				maven-shade-plugin
				
					
						package
						
							shade
						
						
							cmc
							true
							jar-with-dependencies
							
								
									com.mishi.cmc.appclient.AppClientServiceStart
								
								
									META-INF/spring.handlers
								
								
									META-INF/spring.schemas
								
								
									META-INF/spring.tooling
								
							
							
								
									*:*
									
										META-INF/*.SF
										META-INF/*.DSA
										META-INF/*.RSA
									
								
							
						
					
				
			
OK,问题算是真正解决了,先留备如此















 
  

你可能感兴趣的:(maven,spring)