首页 > 编程语言 > 详细

maven 打包 spring 项目

时间:2016-01-15 20:27:25      阅读:120      评论:0      收藏:0      [点我收藏+]

在程序中使用到了springframework控件(主要是为了使用Mybatis-spring操作数据库,省事)。

使用maven管理项目的构建,现在需要生成一个jar包,包含所有依赖的jar包,并可以在命令行使用java -jar [filename] 来运行。

1. maven-assembly-plugin

最开始的时候尝试使用maven-assembly-plugin插件,配置如下:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

但是,在命令行使用mvn package命令打包之后,运行java -jar 之后,报错如下:

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 [JavaProjectApplicationContext.xml]

google了一阵子之后,网上有人说这是assembly-plugin插件的一个bug,因为它在对第三方打包是,对于 META-INF 下的 spring.handlers,spring.schemas 等多个同名文件进行了覆盖。

推荐使用maven-shade-plugin插件。

2.maven-shade-plugin插件

shade插件可以把所有依赖的包都导入到最终的一个可执行jar包当中。根据网上内容,在pom中设置如下:

<build>  
    <plugins>  
        <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.defonds.RsaEncryptor</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>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>  
    </plugins>  
</build> 

但是执行之后依然报错,如下:

Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

查找之后,原因是一些包重复引用,打包后多出来了一些*.SF等文件,导致报错。

解决方案,添加如下代码到pom.xml文件:

<configuration>  
  <filters>  
    <filter>  
      <artifact>*:*</artifact>  
      <excludes>  
        <exclude>META-INF/*.SF</exclude>  
        <exclude>META-INF/*.DSA</exclude>  
        <exclude>META-INF/*.RSA</exclude>  
      </excludes>  
    </filter>  
  </filters>  
</configuration>  

mvn clean;mvn package; java -jar ...  之后,一切运行正常。

折腾了一天多,总算前进了异步,mark。

 

maven 打包 spring 项目

原文:http://www.cnblogs.com/starRebel/p/5133999.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!