Maven package jar

Source: Internet
Author: User
Tags maven shade plugin

Last week, while working on a requirement to create an executable jar in our project, I tried the following two Maven plug-ins:

*Maven Assembly plug-in

*Maven onejar plug-in

Using these plug-ins I was able to successfully create an executable jar but my executable was dependent on some spring framework jars for its own working and on executing the jar

I encountered some weird exceptions.(See a part of stack trace below)..

Exception in thread "Main" org. springframework. Beans. Factory. parsing. beandefinitionparsingexception:


Configuration problem: unable to locate spring namespacehandler
For XML Schema namespace [http://www.springframework.org/schema/context]
At org. springframework. Beans. Factory. parsing. failfastproblemreporter.
Error (failfastproblemreporter. Java: 68)
At org. springframework. Beans. Factory. parsing. readercontext. Error (readercontext. Java: 85)
At org. springframework. Beans. Factory. parsing. readercontext. Error (readercontext. Java: 80)
At org. springframework. Beans. Factory. xml. beandefinitionparserdelegate.
Error (beandefinitionparserdelegate. Java: 284)
At org. springframework. Beans. Factory. xml. beandefinitionparserdelegate.
Parsecustomelement (beandefinitionparserdelegate. Java: 1332)
At org. springframework. Beans. Factory. xml. beandefinitionparserdelegate.
Parsecustomelement (beandefinitionparserdelegate. Java: 1325)
At org. springframework. Beans. Factory. xml. defaultbeandefinitiondocumentreader.
Parsebeandefinitions (defaultbeandefinitiondocumentreader. Java: 135)
At org. springframework. Beans. Factory. xml. defaultbeandefinitiondocumentreader.
Registerbeandefinitions (defaultbeandefinitiondocumentreader. Java: 93 )....

I was getting these exception because the files in META-INF folder in spring jars were overwriting each other.In our case there are several spring jars all having META-INF/spring. handlers, META-INF/spring. schemas which are used by spring to handle XML Schema namespaces. in final executable jar these files were overwriting each other.

Finally I found an executive tive Maven plugin called Maven shade plugin which resolved the problem.

This plugin in provides the capability to package the artifact in an uber-jar, including its dependencies and to shade I. e. Rename the packages of some of the dependencies.

Following are the steps which I followed to resolve this issue:

    1. Create a resources folder in your src/main (ie src/main/resources)
    2. Create a META-INF folder in your src/main/Resources
    3. Make manifest. MF, spring. handlers and spring. schemas files under src/main/resources/META-INF.
    4. Paste the following in plugins section of your pom. xml
1234567891011121314151617181920212223242526272829303132 <Plugin>   <Groupid> org. Apache. Maven. plugins </groupid>   <Artifactid> Maven-shade-plugin </artifactid>   <Version> 1.4 </Version>   <Executions> <Execution> <Phase> Package </Phase> <Goals> <Goal> shade </goal> </Goals> <Configuration> <Transformers> <Transformer Implementation = "Org. Apache. Maven. plugins. shade. Resource. manifestresourcetransformer" > <Mainclass> com. Company. Project. Main </mainclass> </Transformer> <Transformer Implementation = "Org. Apache. Maven. plugins. shade. Resource. appendingtransformer" > <Resource> META-INF/spring. Handlers </resource> </Transformer> <Transformer Implementation = "Org. Apache. Maven. plugins. shade. Resource. appendingtransformer" > <Resource> META-INF/spring. schemas </resource> </Transformer> </Transformers> <Shadedartifactattached> True </Shadedartifactattached> <! -- Optional --> <Shadedclassifiername> executable </shadedclassifiername> </Configuration> </Execution> </Executions> </Plugin>

 

5. Build your project and you will get executable jar.

What actually happened is the problem of overwriting files like spring. handlers and spring. schemas.

Shades provides a provision to append contents of multiple files into one file.

While aggregating classes/resources from several artifacts into one uber jar with overlapping resources, things become complicated.

Shades provides you several "resource transformers", in this case, we used ManifestresourcetransformerTo set the main class of the executable jar.

Some jars contain additional resources (such. properties files) that have the same file name. to avoid overwriting, you can opt to merge them by appending their content into one file. here we merged the contents of all the files with that specific name usingAppendingtransformer. For XML files use xmlappendingtransformer.

<Plugin>
<Groupid> org. Apache. Maven. plugins </groupid>
<Artifactid> Maven-shade-plugin </artifactid>
<Version> 1.4 </version>
<Executions>
<Execution>
<Phase> package </phase>
<Goals>
<Goal> shade </goal>
</Goals>
<Configuration>
<Transformers>
<Transformer implementation = "org. Apache. Maven. plugins. shade. Resource. manifestresourcetransformer">
<Mainclass> com. Company. Project. Main </mainclass>
</Transformer>
<Transformer implementation = "org. Apache. Maven. plugins. shade. Resource. appendingtransformer">
<Resource> META-INF/spring. Handlers </resource>
</Transformer>
<Transformer implementation = "org. Apache. Maven. plugins. shade. Resource. appendingtransformer">
<Resource> META-INF/spring. schemas </resource>
</Transformer>
</Transformers>
<Shadedartifactattached> true </shadedartifactattached> <! -- Optional -->
<Shadedclassifiername> executable </shadedclassifiername>
</Configuration>
</Execution>
</Executions>
</Plugin> Share the bee buzz:

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.