码迷,mamicode.com
首页 > 编程语言 > 详细

maven打jar包的那些事

时间:2018-11-22 14:26:21      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:命令   开发   pom   VID   assembly   style   eve   write   r文件   

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

 

maven打jar包的那些事

标签:命令   开发   pom   VID   assembly   style   eve   write   r文件   

原文地址:https://www.cnblogs.com/wangliangwu/p/10000429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!