Maven打包三種方法

經常使用MAVEN打包方式總結html

Maven可使用mvn package指令對項目進行打包,若是使用Java -jar xxx.jar執行運行jar文件,會出現"no main manifest attribute, in xxx.jar"(沒有設置Main-Class)、ClassNotFoundException(找不到依賴包)等錯誤。java

要想jar包能直接經過java -jar xxx.jar運行,須要知足:spring

一、在jar包中的META-INF/MANIFEST.MF中指定Main-Class,這樣才能肯定程序的入口在哪裏;apache

二、要能加載到依賴包。maven

使用Maven有如下幾種方法能夠生成能直接運行的jar包,能夠根據須要選擇一種合適的方法。ui

方法一:使用maven-jar-plugin和maven-dependency-plugin插件打包
在pom.xml中配置:spa

[html] view plain copy
<build>  
    <plugins>  
  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-jar-plugin</artifactId>  
            <version>2.6</version>  
            <configuration>  
                <archive>  
                    <manifest>  
                        <addClasspath>true</addClasspath>  
                        <classpathPrefix>lib/</classpathPrefix>  
                        <mainClass>com.xxg.Main</mainClass>  
                    </manifest>  
                </archive>  
            </configuration>  
        </plugin>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-dependency-plugin</artifactId>  
            <version>2.10</version>  
            <executions>  
                <execution>  
                    <id>copy-dependencies</id>  
                    <phase>package</phase>  
                    <goals>  
                        <goal>copy-dependencies</goal>  
                    </goals>  
                    <configuration>  
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>  
  
    </plugins>  
</build>  .net


maven-jar-plugin用於生成META-INF/MANIFEST.MF文件的部份內容,<mainClass>com.xxg.Main</mainClass>指定MANIFEST.MF中的Main-Class,<addClasspath>true</addClasspath>會在MANIFEST.MF加上Class-Path項並配置依賴包,<classpathPrefix>lib/</classpathPrefix>指定依賴包所在目錄。插件

例以下面是一個經過maven-jar-plugin插件生成的MANIFEST.MF文件片斷:orm

[plain] view plain copy
Class-Path: lib/commons-logging-1.2.jar lib/commons-io-2.4.jar  
Main-Class: com.xxg.Main  

只是生成MANIFEST.MF文件還不夠,maven-dependency-plugin插件用於將依賴包拷貝到<outputDirectory>${project.build.directory}/lib</outputDirectory>指定的位置,即lib目錄下。

配置完成後,經過mvn package指令打包,會在target目錄下生成jar包,並將依賴包拷貝到target/lib目錄下,目錄結構以下:

指定了Main-Class,有了依賴包,那麼就能夠直接經過java -jar xxx.jar運行jar包。

這種方式生成jar包有個缺點,就是生成的jar包太多不便於管理,下面兩種方式只生成一個jar文件,包含項目自己的代碼、資源以及全部的依賴包。

方法二:使用maven-assembly-plugin插件打包
在pom.xml中配置:

[html] view plain copy
<build>  
    <plugins>  
  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-assembly-plugin</artifactId>  
            <version>2.5.5</version>  
            <configuration>  
                <archive>  
                    <manifest>  
                        <mainClass>com.xxg.Main</mainClass>  
                    </manifest>  
                </archive>  
                <descriptorRefs>  
                    <descriptorRef>jar-with-dependencies</descriptorRef>  
                </descriptorRefs>  
            </configuration>  
        </plugin>  
  
    </plugins>  
</build>  
打包方式:

[plain] view plain copy
mvn package assembly:single  
打包後會在target目錄下生成一個xxx-jar-with-dependencies.jar文件,這個文件不但包含了本身項目中的代碼和資源,還包含了全部依賴包的內容。因此能夠直接經過java -jar來運行。

此外還能夠直接經過mvn package來打包,無需assembly:single,不過須要加上一些配置:

[html] view plain copy
<build>  
    <plugins>  
  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-assembly-plugin</artifactId>  
            <version>2.5.5</version>  
            <configuration>  
                <archive>  
                    <manifest>  
                        <mainClass>com.xxg.Main</mainClass>  
                    </manifest>  
                </archive>  
                <descriptorRefs>  
                    <descriptorRef>jar-with-dependencies</descriptorRef>  
                </descriptorRefs>  
            </configuration>  
            <executions>  
                <execution>  
                    <id>make-assembly</id>  
                    <phase>package</phase>  
                    <goals>  
                        <goal>single</goal>  
                    </goals>  
                </execution>  
            </executions>  
        </plugin>  
  
    </plugins>  
</build>  

其中<phase>package</phase>、<goal>single</goal>即表示在執行package打包時,執行assembly:single,因此能夠直接使用mvn package打包。
不過,若是項目中用到spring Framework,用這種方式打出來的包運行時會出錯,使用下面的方法三能夠處理。

方法三:使用maven-shade-plugin插件打包
在pom.xml中配置:

[html] view plain copy
<build>  
    <plugins>  
  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-shade-plugin</artifactId>  
            <version>2.4.1</version>  
            <executions>  
                <execution>  
                    <phase>package</phase>  
                    <goals>  
                        <goal>shade</goal>  
                    </goals>  
                    <configuration>  
                        <transformers>  
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
                                <mainClass>com.xxg.Main</mainClass>  
                            </transformer>  
                        </transformers>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>  
  
    </plugins>  
</build>  

配置完成後,執行mvn package便可打包。在target目錄下會生成兩個jar包,注意不是original-xxx.jar文件,而是另一個。和maven-assembly-plugin同樣,生成的jar文件包含了全部依賴,因此能夠直接運行。
若是項目中用到了Spring Framework,將依賴打到一個jar包中,運行時會出現讀取XML schema文件出錯。緣由是Spring Framework的多個jar包中包含相同的文件spring.handlers和spring.schemas,若是生成一個jar包會互相覆蓋。爲了不互相影響,可使用AppendingTransformer來對文件內容追加合併:

[html] view plain copy <build>       <plugins>              <plugin>               <groupId>org.apache.maven.plugins</groupId>               <artifactId>maven-shade-plugin</artifactId>               <version>2.4.1</version>               <executions>                   <execution>                       <phase>package</phase>                       <goals>                           <goal>shade</goal>                       </goals>                       <configuration>                           <transformers>                               <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                                   <mainClass>com.xxg.Main</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>    

相關文章
相關標籤/搜索