Spring exports a jar package that can run

Source: Internet
Author: User

There is a recent need to resolve the issue of the MAVEN project import executable jar package, if the project does not contain spring, then use MVN assembly:assembly, details can refer to: http://www.cnblogs.com/liqiu/p/3816068.html

However, if you include spring, then this method is not feasible, error:

 for XML schema namespace

I have been on the internet for two days, this is a bug assembly. See also: http://jira.codehaus.org/browse/MASSEMBLY-360

It is said that the reason is that more than one jar package in spring contains Spring.handlers and Spring.schemas files, and assembly only the first encountered files into the jar package, the later encountered will skip.

The solution is to give up assembly, Use the shade plug-in to package. Specifies that spring.handlers and spring.schemas files are added append in the shade packaging configuration to ensure that the information of the two files in other spring jars is not omitted.

Here is a very simple example of a MAVEN project with only four files, in the code re-attachment:

1, Pom.xml
<Projectxmlns= "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.javaee</groupId>    <Artifactid>Main-spring</Artifactid>    <version>0.0.1-snapshot</version>    <Dependencies>        <Dependency>            <groupId>Org.springframework</groupId>            <Artifactid>Spring-context</Artifactid>            <version>3.0.6.RELEASE</version>        </Dependency>    </Dependencies>    <Build>        <Plugins>            <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>                        <Configuration>                            <Finalname>My-spring-app</Finalname>                            <shadedartifactattached>True</shadedartifactattached>                            <Shadedclassifiername>Jar-with-dependencies</Shadedclassifiername>                            <Transformers>                                <TransformerImplementation= "Org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                                    <MainClass>Com.test.Main</MainClass>                                </Transformer>                                <TransformerImplementation= "Org.apache.maven.plugins.shade.resource.AppendingTransformer">                                    <Resource>Meta-inf/spring.handlers</Resource>                                </Transformer>                                <TransformerImplementation= "Org.apache.maven.plugins.shade.resource.AppendingTransformer">                                    <Resource>Meta-inf/spring.schemas</Resource>                                </Transformer>                                <TransformerImplementation= "Org.apache.maven.plugins.shade.resource.AppendingTransformer">                                    <Resource>Meta-inf/spring.tooling</Resource>                                </Transformer>                            </Transformers>                        </Configuration>                    </Execution>                </executions>            </plugin>        </Plugins>    </Build></Project>
2. applicationcontext.xml (Spring config file)
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"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 Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.0.xsd ">    <Context:annotation-config/>    <Context:component-scanBase-package= "com.*" /></Beans>
3. Code examples
Package Com.service;import Org.springframework.stereotype.Service, @Servicepublic class UserService {public    String Finduser () {        return "find Liqiu";    }}
 Packagecom.exec;ImportOrg.springframework.context.support.GenericXmlApplicationContext;ImportCom.service.UserService; Public classMain { Public Static voidMain (string[] args)throwsinterruptedexception {genericxmlapplicationcontext context=NewGenericxmlapplicationcontext (); Context.setvalidating (false); Context.load ("Classpath:applicationContext.xml");        Context.refresh (); UserService UserService= Context.getbean (userservice.class);  while(true) {System.out.println (Userservice.finduser ()); Thread.Sleep (10000); }    }}

Run at command line: MVN package

The target directory will then produce a jar package: My-spring-app.jar

Run: Java-jar Target/my-spring-app.jar

May 16, 2015 10:46:52pm Org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadbeandefinitionsinfo:loading XML Bean Definitions fromclasspath resource [applicationcontext.xml] May16, 2015 10:46:53Afternoon Org.springframework.context.support.AbstractApplicationContext preparerefreshinfo:refreshing O[email Protected]4a5e88f7:startup Date [Sat May2015 22:46:53 CST]; Root of context hierarchy May16, 2015 10:46:53Afternoon Org.springframework.beans.factory.support.DefaultListableBeanFactory Preinstantiatesingletonsinfo:pre -instantiating singletons in org.s[email protected]18fd54ec:defining beans [ Org.springframework.context.annotation.internalConfigurationAnnotationProcessor, Org.springframework.context.annotation.internalAutowiredAnnotationProcessor, Org.springframework.context.annotation.internalRequiredAnnotationProcessor, Org.springframework.context.annotation.internalcommonannotationprocessor,userservice]; Root of Factory Hierarchyfind Liqiufind Liqiu

Of course, you can also run this: Java-classpath Target/my-spring-app.jar com.exec.Main

Source code: HTTP://FILES.CNBLOGS.COM/FILES/LIQIU/MAIN-SPRING.TAR.GZ

Spring exports a jar package that can run

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.