1. 程式人生 > >maven打jar包的那些事

maven打jar包的那些事

0.前言

  打包命令: mvn clean install,常用的 ide 一般都集成了 maven,因此可以更方便的執行 maven 命令.

1.方式一

pom.xml

 1 <!-- 包型別 -->
 2 <packaging>jar</packaging>
 3 
 4 <dependencies>
 5     <!-- ... -->
 6 </dependencies>
 7 
 8 <build>
 9     <plugins>
10         <plugin>
11 <artifactId>maven-assembly-plugin</artifactId> 12 <configuration> 13 <archive> 14 <manifest> 15 <addClasspath>true</addClasspath> 16 <!-- 指定主類,如: 自定義的一個 main 方法
--> 17 <mainClass>com.xxx.MyMain</mainClass> 18 </manifest> 19 </archive> 20 <descriptorRefs> 21 <descriptorRef>jar-with-dependencies</descriptorRef> 22 </
descriptorRefs> 23 </configuration> 24 <executions> 25 <execution> 26 <id>make-my-jar-with-dependencies</id> 27 <phase>package</phase> 28 <goals> 29 <goal>single</goal> 30 </goals> 31 </execution> 32 </executions> 33 </plugin> 34 <plugin> 35 <groupId>org.apache.maven.plugins</groupId> 36 <artifactId>maven-compiler-plugin</artifactId> 37 <configuration> 38 <source>1.8</source><!-- 原始碼使用的開發版本 --> 39 <target>1.8</target><!-- 需要生成的目標class檔案的編譯版本 --> 40 </configuration> 41 </plugin> 42 </plugins> 43 </build>

  生成2個jar包

  1. test-0.0.1-SNAPSHOT-jar-with-dependencies.jar: 該 jar 包包含此 project 所依賴的 jar(即 <dependencies> 裡面的依賴也打進去了)
  2. test-0.0.1-SNAPSHOT.jar

2.方式二

pom.xml

 1 <!-- 包型別 -->
 2 <packaging>jar</packaging>
 3 
 4 <dependencies>
 5     <!-- ... -->
 6 </dependencies> 
 7 
 8 <build>
 9     <plugins>
10         <plugin>
11             <groupId>org.apache.maven.plugins</groupId>
12             <artifactId>maven-jar-plugin</artifactId>
13             <configuration>
14                 <archive>
15                     <manifest>
16                         <addClasspath>true</addClasspath>
17                         <classpathPrefix>lib/</classpathPrefix><!--指定classpath的字首-->
18                         <!-- 指定主類 -->
19                         <mainClass>com.xxx.MyMain</mainClass>
20                     </manifest>
21                 </archive>
22             </configuration>
23         </plugin>
24 
25         <plugin>
26             <groupId>org.apache.maven.plugins</groupId>
27             <artifactId>maven-dependency-plugin</artifactId>
28             <executions>
29                 <execution>
30                     <id>copy-dependencies</id>
31                     <phase>prepare-package</phase>
32                     <goals>
33                         <goal>copy-dependencies</goal>
34                     </goals>
35                     <configuration>
36                         <!--指定outputDirectory-->
37                         <outputDirectory>${project.build.directory}/lib</outputDirectory>
38                         <!--
39                             <outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
40                             Alternatively use ${project.build.directory}/classes/lib as OutputDirectory to integrate all jar-files into the main jar, but then you will need to add custom classloading code to load the jars.
41                             -->
42                         <overWriteReleases>false</overWriteReleases>
43                         <overWriteSnapshots>false</overWriteSnapshots>
44                         <overWriteIfNewer>true</overWriteIfNewer>
45                     </configuration>
46                 </execution>
47             </executions>
48         </plugin>
49     </plugins>
50 </build>

生成 1 個 jar 包和一個 lib 目錄

  1. test-0.0.1-SNAPSHOT.jar 
  2. lib: 存放此 project 依賴的 jar(即: </dependencies> 中的依賴)

3.補充

  dubbo(provider)有 3 種啟動方式(servlet 容器,自建 main 方法,dubbo 官方提供的 main 類).線上環境推薦使用 dubbo 官方提供的 main 類,此時可以這樣打包: 

pom.xml

 1 <build>
 2     <finalName>provider-test</finalName>
 3     <resources>
 4         <resource>
 5             <targetPath>${project.build.directory}/classes</targetPath>
 6             <directory>src/main/resources</directory>
 7             <filtering>true</filtering>
 8             <includes>
 9                 <include>**/*.xml</include>
10                 <include>**/*.properties</include>
11             </includes>
12         </resource>
13         <!-- com.alibaba.dubbo.container.Main -->
14         <resource>
15             <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
16             <directory>src/main/resources/spring</directory>
17             <filtering>true</filtering>
18             <includes>
19                 <include>spring-context.xml</include>
20             </includes>
21         </resource>
22     </resources>
23     <pluginManagement>
24         <plugins>
25             <!-- 解決Maven外掛在Eclipse內執行了一系列的生命週期引起衝突 -->
26             <plugin>
27                 <groupId>org.eclipse.m2e</groupId>
28                 <artifactId>lifecycle-mapping</artifactId>
29                 <version>1.0.0</version>
30                 <configuration>
31                     <lifecycleMappingMetadata>
32                         <pluginExecutions>
33                             <pluginExecution>
34                                 <pluginExecutionFilter>
35                                     <groupId>org.apache.maven.plugins</groupId>
36                                     <artifactId>maven-dependency-plugin</artifactId>
37                                     <goals>
38                                         <goal>copy-dependencies</goal>
39                                     </goals>
40                                 </pluginExecutionFilter>
41                                 <action>
42                                     <ignore />
43                                 </action>
44                             </pluginExecution>
45                         </pluginExecutions>
46                     </lifecycleMappingMetadata>
47                 </configuration>
48             </plugin>
49         </plugins>
50     </pluginManagement>
51     <plugins>
52         <!-- 打包jar檔案時,配置manifest檔案,加入lib包的jar依賴 -->
53         <plugin>
54             <groupId>org.apache.maven.plugins</groupId>
55             <artifactId>maven-jar-plugin</artifactId>
56             <configuration>
57                 <classesDirectory>target/classes/</classesDirectory>
58                 <archive>
59                     <manifest>
60                         <mainClass>com.alibaba.dubbo.container.Main</mainClass>
61                         <!-- 打包時 MANIFEST.MF檔案不記錄的時間戳版本 -->
62                         <useUniqueVersions>false</useUniqueVersions>
63                         <addClasspath>true</addClasspath>
64                         <classpathPrefix>lib/</classpathPrefix>
65                     </manifest>
66                     <manifestEntries>
67                         <Class-Path>.</Class-Path>
68                     </manifestEntries>
69                 </archive>
70             </configuration>
71         </plugin>
72         <plugin>
73             <groupId>org.apache.maven.plugins</groupId>
74             <artifactId>maven-dependency-plugin</artifactId>
75             <executions>
76                 <execution>
77                     <id>copy-dependencies</id>
78                     <phase>package</phase>
79                     <goals>
80                         <goal>copy-dependencies</goal>
81                     </goals>
82                     <configuration>
83                         <type>jar</type>
84                         <includeTypes>jar</includeTypes>
85                         <outputDirectory>
86                             ${project.build.directory}/lib
87                         </outputDirectory>
88                     </configuration>
89                 </execution>
90             </executions>
91         </plugin>
92     </plugins>
93 </build>

  生成一個 jar 包和該 jar 包和一個目錄 lib 

4.執行

1 java -jar xxx.jar