開始使用如下代碼進行打包
<build> <!-- mvn assembly:assembly -Dmaven.test.skip=true --> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.fxc.rpc.impl.member.MemberProvider</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
結果出現spring命名空間無法找到的錯誤,
org.xml.sax.SAXParseException: schema_reference.4: 無法讀取方案文檔 'http://www.springframework.org/schema/beans/spring-beans.xsd', 原因為 1) 無法找到文檔; 2) 無法讀取文檔; 3) 文檔的根元素不是 <xsd:schema>。
據查是由於spring-core,spring-aop每一個jar中都包含了一套spring.handlers,spring.schemas文件,以至於在打包過程中發生了覆蓋,網上沒有搜到使用maven-assembly-plugin插件如何解決此問題,大多數人建議使用maven-shade-plugin插件,修改后pom代碼如下
<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.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.fxc.rpc.impl.member.MemberProvider</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> </transformers> </configuration> </execution> </executions> </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
打包成功
<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> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.fxc.rpc.impl.member.MemberProvider</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> </transformers> </configuration> </execution> </executions> </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