Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1670234
  • 博文数量: 695
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4027
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-20 21:22
文章分类

全部博文(695)

文章存档

2018年(18)

2017年(74)

2016年(170)

2015年(102)

2014年(276)

2013年(55)

分类: Java

2017-09-27 16:16:30


点击(此处)折叠或打开

  1. <build>
  2.         <!-- mvn assembly:assembly -Dmaven.test.skip=true -->
  3.         <plugins>
  4.             <plugin>
  5.                 <artifactId>maven-assembly-plugin</artifactId>
  6.                 <version>2.2-beta-5</version>
  7.                 <configuration>
  8.                     <appendAssemblyId>false</appendAssemblyId>
  9.                     <descriptorRefs>
  10.                         <descriptorRef>jar-with-dependencies</descriptorRef>
  11.                     </descriptorRefs>
  12.                     <archive>
  13.                         <manifest>
  14.                             <mainClass>com.fxc.rpc.impl.member.MemberProvider</mainClass>
  15.                         </manifest>
  16.                     </archive>
  17.                 </configuration>
  18.                 <executions>
  19.                     <execution>
  20.                         <id>make-assembly</id>
  21.                         <phase>package</phase>
  22.                         <goals>
  23.                             <goal>assembly</goal>
  24.                         </goals>
  25.                     </execution>
  26.                 </executions>
  27.             </plugin>
  28.         </plugins>
  29.     </build>


  30.  结果出现spring命名空间无法找到的错误,

  31. org.xml.sax.SAXParseException: schema_reference.4: 无法读取方案文档 '', 原因为 1) 无法找到文档; 2) 无法读取文档; 3) 文档的根元素不是 <xsd:schema>

  32. 据查是由于spring-core,spring-aop每一个jar中都包含了一套spring.handlers,spring.schemas文件,以至于在打包过程中发生了覆盖,网上没有搜到使用maven-assembly-plugin插件如何解决此问题,大多数人建议使用maven-shade-plugin插件,修改后pom代码如下
  33. <plugin>
  34.     <groupId>org.apache.maven.plugins</groupId>
  35.     <artifactId>maven-shade-plugin</artifactId>
  36.     <version>1.4</version>
  37.     <executions>
  38.         <execution>
  39.             <phase>package</phase>
  40.             <goals>
  41.                 <goal>shade</goal>
  42.             </goals>
  43.             <configuration>
  44.                 <transformers>
  45.                     <transformer
  46.                         implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  47.                         <resource>META-INF/spring.handlers</resource>
  48.                     </transformer>
  49.                     <transformer
  50.                         implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  51.                         <mainClass>com.fxc.rpc.impl.member.MemberProvider</mainClass>
  52.                     </transformer>
  53.                     <transformer
  54.                         implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  55.                         <resource>META-INF/spring.schemas</resource>
  56.                     </transformer>
  57.                 </transformers>
  58.             </configuration>
  59.         </execution>
  60.     </executions>
  61. </plugin>

再次打包,出现文件签名不合法的问题

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

再查,原来是由于某些包的重复引用,以至于打包之后的META-INF的目录下多出了一些*.SF,*.DSA,*.RSA文件所致(据说解压jar包,然后删掉这些文件再次打包错误就会消失,未确认),再次修改pom.xml,最终使用如下配置文件,运行

mvn clean install -Dmaven.test.skip=true

点击(此处)折叠或打开

  1. <plugin>
  2.             <groupId>org.apache.maven.plugins</groupId>
  3.             <artifactId>maven-shade-plugin</artifactId>
  4.             <version>1.4</version>
  5.             <executions>
  6.                 <execution>
  7.                     <phase>package</phase>
  8.                     <goals>
  9.                         <goal>shade</goal>
  10.                     </goals>
  11.                     <configuration>
  12.                         <filters>
  13.                             <filter>
  14.                                 <artifact>*:*</artifact>
  15.                                 <excludes>
  16.                                     <exclude>META-INF/*.SF</exclude>
  17.                                     <exclude>META-INF/*.DSA</exclude>
  18.                                     <exclude>META-INF/*.RSA</exclude>
  19.                                 </excludes>
  20.                             </filter>
  21.                         </filters>

  22.                         <transformers>
  23.                             <transformer
  24.                                 implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  25.                                 <resource>META-INF/spring.handlers</resource>
  26.                             </transformer>
  27.                             <transformer
  28.                                 implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  29.                                 <mainClass>com.fxc.rpc.impl.member.MemberProvider</mainClass>
  30.                             </transformer>
  31.                             <transformer
  32.                                 implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  33.                                 <resource>META-INF/spring.schemas</resource>
  34.                             </transformer>
  35.                         </transformers>
  36.                     </configuration>
  37.                 </execution>
  38.             </executions>
  39.         </plugin>

此时查看target目录下会发现xxx.jar 和original-xxx.jar,后一个不包含引用的jar包,直接运行前一个即可

java -jar target/xxx.jar 

成功!

PS:项目中使用了几个公司自己的jar,在公有库里没有,在eclipse里运行的时候我都是修改scope为system,调用的本地jar,但是在打包的过程中scope=system的jar是不会自己打进去的,很是让我郁闷,我只好讲这些jar安装进入本地资源库

mvn install:install-file -Dfile=my-jar.jar -DgroupId=org.richard -DartifactId=my-jar -Dversion=1.0 -Dpackaging=jar

阅读(1002) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~