Maven打包的三種方式


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

要想jar包能直接通過java -jar xxx.jar運行,需要滿足:

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

2、要能加載到依賴包。

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

方法一:使用maven-jar-plugin和maven-dependency-plugin插件打包

在pom.xml中配置:

  1. <build>  
  2.     <plugins>  
  3.   
  4.         <plugin>  
  5.             <groupId>org.apache.maven.plugins</groupId>  
  6.             <artifactId>maven-jar-plugin</artifactId>  
  7.             <version>2.6</version>  
  8.             <configuration>  
  9.                 <archive>  
  10.                     <manifest>  
  11.                         <addClasspath>true</addClasspath>  
  12.                         <classpathPrefix>lib/</classpathPrefix>  
  13.                         <mainClass>com.xxg.Main</mainClass>  
  14.                     </manifest>  
  15.                 </archive>  
  16.             </configuration>  
  17.         </plugin>  
  18.         <plugin>  
  19.             <groupId>org.apache.maven.plugins</groupId>  
  20.             <artifactId>maven-dependency-plugin</artifactId>  
  21.             <version>2.10</version>  
  22.             <executions>  
  23.                 <execution>  
  24.                     <id>copy-dependencies</id>  
  25.                     <phase>package</phase>  
  26.                     <goals>  
  27.                         <goal>copy-dependencies</goal>  
  28.                     </goals>  
  29.                     <configuration>  
  30.                         <outputDirectory>${project.build.directory}/lib</outputDirectory>  
  31.                     </configuration>  
  32.                 </execution>  
  33.             </executions>  
  34.         </plugin>  
  35.   
  36.     </plugins>  
  37. </build>  

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文件片段:

  1. Class-Path: lib/commons-logging-1.2.jar lib/commons-io-2.4.jar  
  2. 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中配置:

  1. <build>  
  2.     <plugins>  
  3.   
  4.         <plugin>  
  5.             <groupId>org.apache.maven.plugins</groupId>  
  6.             <artifactId>maven-assembly-plugin</artifactId>  
  7.             <version>2.5.5</version>  
  8.             <configuration>  
  9.                 <archive>  
  10.                     <manifest>  
  11.                         <mainClass>com.xxg.Main</mainClass>  
  12.                     </manifest>  
  13.                 </archive>  
  14.                 <descriptorRefs>  
  15.                     <descriptorRef>jar-with-dependencies</descriptorRef>  
  16.                 </descriptorRefs>  
  17.             </configuration>  
  18.         </plugin>  
  19.   
  20.     </plugins>  
  21. </build>  

打包方式:

  1. mvn package assembly:single  

打包后會在target目錄下生成一個xxx-jar-with-dependencies.jar文件,這個文件不但包含了自己項目中的代碼和資源,還包含了所有依賴包的內容。所以可以直接通過java -jar來運行。

此外還可以直接通過mvn package來打包,無需assembly:single,不過需要加上一些配置:

  1. <build>  
  2.     <plugins>  
  3.   
  4.         <plugin>  
  5.             <groupId>org.apache.maven.plugins</groupId>  
  6.             <artifactId>maven-assembly-plugin</artifactId>  
  7.             <version>2.5.5</version>  
  8.             <configuration>  
  9.                 <archive>  
  10.                     <manifest>  
  11.                         <mainClass>com.xxg.Main</mainClass>  
  12.                     </manifest>  
  13.                 </archive>  
  14.                 <descriptorRefs>  
  15.                     <descriptorRef>jar-with-dependencies</descriptorRef>  
  16.                 </descriptorRefs>  
  17.             </configuration>  
  18.             <executions>  
  19.                 <execution>  
  20.                     <id>make-assembly</id>  
  21.                     <phase>package</phase>  
  22.                     <goals>  
  23.                         <goal>single</goal>  
  24.                     </goals>  
  25.                 </execution>  
  26.             </executions>  
  27.         </plugin>  
  28.   
  29.     </plugins>  
  30. </build>  

其中<phase>package</phase>、<goal>single</goal>即表示在執行package打包時,執行assembly:single,所以可以直接使用mvn package打包。

不過,如果項目中用到spring Framework,用這種方式打出來的包運行時會出錯,使用下面的方法三可以處理。

方法三:使用maven-shade-plugin插件打包

在pom.xml中配置:

  1. <build>  
  2.     <plugins>  
  3.   
  4.         <plugin>  
  5.             <groupId>org.apache.maven.plugins</groupId>  
  6.             <artifactId>maven-shade-plugin</artifactId>  
  7.             <version>2.4.1</version>  
  8.             <executions>  
  9.                 <execution>  
  10.                     <phase>package</phase>  
  11.                     <goals>  
  12.                         <goal>shade</goal>  
  13.                     </goals>  
  14.                     <configuration>  
  15.                         <transformers>  
  16.                             <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  17.                                 <mainClass>com.xxg.Main</mainClass>  
  18.                             </transformer>  
  19.                         </transformers>  
  20.                     </configuration>  
  21.                 </execution>  
  22.             </executions>  
  23.         </plugin>  
  24.   
  25.     </plugins>  
  26. </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來對文件內容追加合並:

    1. <build>  
    2.     <plugins>  
    3.   
    4.         <plugin>  
    5.             <groupId>org.apache.maven.plugins</groupId>  
    6.             <artifactId>maven-shade-plugin</artifactId>  
    7.             <version>2.4.1</version>  
    8.             <executions>  
    9.                 <execution>  
    10.                     <phase>package</phase>  
    11.                     <goals>  
    12.                         <goal>shade</goal>  
    13.                     </goals>  
    14.                     <configuration>  
    15.                         <transformers>  
    16.                             <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
    17.                                 <mainClass>com.xxg.Main</mainClass>  
    18.                             </transformer>  
    19.                             <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
    20.                                 <resource>META-INF/spring.handlers</resource>  
    21.                             </transformer>  
    22.                             <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
    23.                                 <resource>META-INF/spring.schemas</resource>  
    24.                             </transformer>  
    25.                         </transformers>  
    26.                     </configuration>  
    27.                 </execution>  
    28.             </executions>  
    29.         </plugin>  
    30.   
    31.     </plugins>  
    32. </build>  

 

自己代碼中的shade+assembly:

pom.xml:

 1         <plugin>
 2                 <groupId>org.apache.maven.plugins</groupId>
 3                 <artifactId>maven-shade-plugin</artifactId>
 4                 <dependencies>
 5                     <dependency>
 6                         <groupId>org.springframework.boot</groupId>
 7                         <artifactId>spring-boot-maven-plugin</artifactId>
 8                         <version>1.5.17.RELEASE</version>
 9                     </dependency>
10                 </dependencies>
11                 <configuration>
12                     <shadedArtifactAttached>true</shadedArtifactAttached>
13                     <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
14                     <createDependencyReducedPom>false</createDependencyReducedPom>
15                     <finalName>demo-web-server-shade</finalName>
16                     <filters>
17                         <filter>
18                             <artifact>*:*</artifact>
19                             <excludes>
20                                 <exclude>META-INF/*.SF</exclude>
21                                 <exclude>META-INF/*.DSA</exclude>
22                                 <exclude>META-INF/*.RSA</exclude>
23                             </excludes>
24                         </filter>
25                     </filters>
26                 </configuration>
27                 <executions>
28                     <execution>
29                         <phase>package</phase>
30                         <goals>
31                             <goal>shade</goal>
32                         </goals>
33                         <configuration>
34                             <transformers>
35                                 <transformer
36                                     implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
37                                     <resource>META-INF/spring.handlers</resource>
38                                 </transformer>
39                                 <transformer
40                                     implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
41                                     <resource>META-INF/spring.factories</resource>
42                                 </transformer>
43                                 <transformer
44                                     implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
45                                     <resource>META-INF/spring.schemas</resource>
46                                 </transformer>
47                                 <transformer
48                                     implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
49                                 <transformer
50                                     implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
51                                     <mainClass>com.my.demo.httpserver.WebServerApplication</mainClass>
52                                 </transformer>
53                             </transformers>
54                         </configuration>
55                     </execution>
56                 </executions>
57             </plugin>
58 
59             <plugin>
60                 <groupId>org.apache.maven.plugins</groupId>
61                 <artifactId>maven-assembly-plugin</artifactId>
62                 <executions>
63                     <execution>
64                         <id>assemble</id>
65                         <goals>
66                             <goal>single</goal>
67                         </goals>
68                         <phase>package</phase>
69                         <configuration>
70                             <descriptors>
71                                 <descriptor>assembly.xml</descriptor>
72                             </descriptors>
73                         </configuration>
74                     </execution>
75                 </executions>
76             </plugin>

assembly.xml:

 <?xml version="1.0" encoding="utf-8"?>

<assembly>
  <id>assembly</id>
  <includeBaseDirectory>false</includeBaseDirectory>
  <formats> 
    <format>zip</format> 
  </formats>  
  <fileSets> 
    <!-- <fileSet>
      <directory>src/main/resources/bin</directory>
      <includes> 
        <include>start.sh</include>
        <include>env.sh</include>
        <include>stop.sh</include>
      </includes>  
      <fileMode>0755</fileMode>  
      <outputDirectory>/</outputDirectory>
    </fileSet> -->
    <fileSet>
      <directory>src/main/resources/configsec</directory>
      <includes>
        <include>important.properties</include>
      </includes>
      <outputDirectory>configsec/</outputDirectory>
    </fileSet>
    <fileSet>
      <directory>src/main/resources/lib</directory>
      <includes>
        <include>quasar-core-0.7.9-jdk8.jar</include>
      </includes>
      <outputDirectory>/</outputDirectory>
    </fileSet>
  </fileSets>
  <files> 
    <file> 
      <source>${project.build.directory}/demo-web-server-shade.jar</source>
      <outputDirectory>/</outputDirectory>
    </file> 
  </files> 
</assembly>

 

轉自:https://blog.csdn.net/daiyutage/article/details/53739452

參考資料:

https://yq.aliyun.com/articles/308777

maven-shade-plugin:https://my.oschina.net/u/2377110/blog/1584205

maven-assembly-plugin:https://www.jianshu.com/p/14bcb17b99e0 https://blog.csdn.net/liupeifeng3514/article/details/79777976


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM