maven 打包jar

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 effective 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 effective maven plugin called maven shade plugin which resolved the problem.

This plugin 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<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 ManifestResourceTransformer to set the main class of the executable jar .

Some jars contain additional resources (such as .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 using the AppendingTransformer. 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:
posted @ 2012-03-23 21:55  lhj588  阅读(4450)  评论(0编辑  收藏  举报