Monday 1 August 2011

Issues Using Maven 2 Assembly Plugin & Spring

Friday evening I tried in vain to package up my deployable Spring JAR file using the standard maven assembly plugin using the given configuration as seen below with no luck. I kept getting hit by spring throwing exceptions when trying to run the JAR file as the spring.shemas and spring.handler files where incorrect. It seems that when maven builds the package, it keeps overwritting the shemas and handlers files and by the end of the build they are both missing elements.

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


	maven-assembly-plugin
	
		
			jar-with-dependencies
		
			
				
					com.some.package.Main
				
			
			
			
				true
					
						
							META-INF/spring.handlers
								META-INF/spring.schemas
							
						
					
				
			
			
				
					make-assembly
					package
					
						attached
					
				
			
		

It turns out this is a known bug and quite well documented. See: http://jira.codehaus.org/browse/MASSEMBLY-360.
So I turned my attention to maven shade plugin to build and package my JAR file, this only worked straight away.

My final maven shade plugin configuration is as follows:


            org.apache.maven.plugins
            maven-shade-plugin
            1.4
            
                
                    package
                    
                        shade
                    
                    
                        
                            
                                com.some.package.Main
                            
                            
                                META-INF/spring.handlers
                            
                            
                                META-INF/spring.schemas
                            
                        
			
				
				*:*
					
						META-INF/*.SF
						META-INF/*.DSA
						META-INF/*.RSA
					
				
			
                    
                
            
        


Using org.apache.maven.plugins.shade.resource.AppendingTransformer allows for the shade plugin to merge all spring.schemas and spring.handlers files into a single entry for each. I use the filters section to exclude particular files which would cause and Security exception to be thrown. The files are from javax.mail package.

It produces three jar files once built:
  • original-ProjectName.jar - The package application without dependencies.
  • ProjectName-shade.jar - The none shaded application with dependencies.
  • ProjectName.jar - The shaded application with dependencies. (i.e. a renamed package)

1 comment: