使用meaven打包過程當中遇到的一些問題

開始使用以下代碼進行打包java

<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命名空間沒法找到的錯誤,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代碼以下apache

<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> 

再次打包,出現文件簽名不合法的問題app

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

再查,原來是因爲某些包的重複引用,以致於打包以後的META-INF的目錄下多出了一些*.SF,*.DSA,*.RSA文件所致(聽說解壓jar包,而後刪掉這些文件再次打包錯誤就會消失,未確認),再次修改pom.xml,最終使用以下配置文件,運行eclipse

mvn clean install -Dmaven.test.skip=true

打包成功maven

<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包,直接運行前一個便可ide

java -jar target/xxx.jar 

成功!ui

PS:項目中使用了幾個公司本身的jar,在公有庫裏沒有,在eclipse裏運行的時候我都是修改scope爲system,調用的本地jar,可是在打包的過程當中scope=system的jar是不會本身打進去的,非常讓我鬱悶,我只好講這些jar安裝進入本地資源庫spa

mvn install:install-file -Dfile=my-jar.jar -DgroupId=org.richard -DartifactId=my-jar -Dversion=1.0 -Dpackaging=jar
相關文章
相關標籤/搜索