下面是使用Intellij 打包jar文件的步驟,之后會有運行jar文件時遇到的錯誤。





打包完成。
==========================================================================
運行jar出現問題:
1、找不到主類。打開jar文件包,在MANIFEST.MF文件中添加Main-Class: 包名.類名,
注意:包名前面有空格,類名沒有.java或者.class后綴,最后一定要回車到下一行。讓光標定位在空白行。
打開


2、java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
打開META-INF目錄,將*.SF,*.DSA,*.RSA文件刪除,即可。應為有些包有簽名,導致錯誤。
此問題,可以參考下面的連接,這位大神比較詳細,http://www.cnblogs.com/fuxinci/p/3356087.html,(如有侵權請告知,會刪除,謝謝!)。

使用meaven打包過程中遇到的一些問題
開始使用如下代碼進行打包
<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

