0%

maven导出jar包

文章字数:1197,阅读全文大约需要4分钟

配置

常用插件有三种,对应三个配置

  1. maven-jar-plugin: maven 默认打包插件,用来创建 project jar
  2. maven-shade-plugin: 打可执行包,executable(fat) jar
  3. maven-assembly-plugin: 支持自定义打包方式

打包

  1. 打包前需要先clean一下,重新加载依赖

  2. maven-jar-plugin配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <!-- 对要打的jar包进行配置 -->
    <configuration>
    <!-- Configuration of the archiver -->
    <archive>
    <!--生成的jar中,不要包含pom.xml和pom.properties这两个文件-->
    <addMavenDescriptor>false</addMavenDescriptor>

    <!-- Manifest specific configuration -->
    <manifest>
    <!--是否要把第三方jar放到manifest的classpath中-->
    <addClasspath>true</addClasspath>

    <!--生成的manifest中classpath的前缀,
    因为要把第三方jar放到lib目录下,
    所以classpath的前缀是lib/-->
    <classpathPrefix>lib/</classpathPrefix>
    </manifest>
    </archive>
    <!--过滤掉不希望包含在jar中的文件-->
    <excludes>
    <!-- 排除不需要的文件夹(路径是jar包内部的路径) -->
    <exclude>**/assembly/</exclude>
    </excludes>
    </configuration>
    </plugin>
  3. maven-assembly-plugin

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <!-- 对项目的组装进行配置 -->
    <configuration>
    <!-- 指定assembly插件的配置文件所在位置 -->
    <descriptors>
    <descriptor>src/main/resources/assembly/package.xml</descriptor>
    </descriptors>
    </configuration>
    <executions>
    <execution>
    <id>make-assembly</id>
    <!-- 将组装绑定到maven生命周期的哪一阶段 -->
    <phase>package</phase>
    <goals>
    <!-- 指定assembly插件的打包方式-->
    <goal>single</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
  4. 双击idea maven菜单栏的package或者直接命令执行mvn:package生成两个包(可执行jar和项目压缩包)

pom配置

包含两个文件:
pom.xml整体的配置
package.xml包含在pom.xml中,用于指定assembly装配时的配置

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- ####################### 基础设置 ###################### -->
<!--groupId:项目或者组织的唯一标志,并且配置时生成路径也是由此生成,如org.myproject.mojo生成的相对路径为:/org/myproject/mojo-->
<groupId>com.dong</groupId>
<!--项目的通用名称-->
<artifactId>bigdata</artifactId>
<!--打包机制,如pom,jar,maven-plugin,ejb,war,ear,rar,par-->
<packaging>jar</packaging>
<!--项目的版本-->
<version>1.0-SNAPSHOT</version>

<!-- ####################### 项目信息 ###################### -->
<!--用户描述项目的名称,无关紧要的东西-->
<name>bigdata</name>
<!--写明开发团队的网站,无关紧要-->
<url>http://http://www.dong.com/.com</url>

<!-- ####################### 环境设置 ###################### -->
<properties>
<!-- 项目执行脚本目录 -->
<project.script.execute.directory>src/main/scripts/execute</project.script.execute.directory>
<!-- 项目说明文档目录 -->
<project.document.directory>document</project.document.directory>
<!-- 项目配置文件目录 -->
<project.config.directory>src/main/resources</project.config.directory>
<!-- 项目编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- 本地编译JDK版本 -->
<maven.compiler.source>1.8</maven.compiler.source>
<!-- 项目部署JDK版本 -->
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<!--
配置Maven的仓库, 在此处配置的仓库会优先于setting.xml里配置的仓库,
建议哪个仓库快,哪个配置在前面, 然后如果Maven在前面配置的仓库找不到的话会去后面的仓库找,
如果后面的仓库都找不到,会去setting.xml中央仓库里找
-->
<repositories>
<!-- 阿里云仓库,配置Maven仓库,速度快配置在最前面 -->
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
<!-- 国内备选仓库 -->
<repository>
<id>repo2</id>
<url>http://repo2.maven.org/maven2/</url>
</repository>

<!-- Cloudera仓库,如果在阿里云仓库里找不到去Cloudera的仓库里找,主要是CDH版本Hadoop依赖的jar -->
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>

<!-- Scala仓库,如果前面两个都找不到来仓库找,如果此仓库也找不到,去中央仓库找 -->
<repository>
<id>scala-tools.org</id>
<name>Scala-Tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url>
</repository>
</repositories>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>

<build>
<finalName>dong</finalName>
<plugins>
<!-- The configuration of maven-jar-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<!-- 对要打的jar包进行配置 -->
<configuration>
<!-- Configuration of the archiver -->
<archive>
<!--生成的jar中,不要包含pom.xml和pom.properties这两个文件-->
<addMavenDescriptor>false</addMavenDescriptor>

<!-- Manifest specific configuration -->
<manifest>
<!--是否要把第三方jar放到manifest的classpath中-->
<addClasspath>true</addClasspath>

<!--
生成的manifest中classpath的前缀,
因为要把第三方jar放到lib目录下,
所以classpath的前缀是lib/
-->
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
<!--过滤掉不希望包含在jar中的文件-->
<excludes>
<!-- 排除不需要的文件夹(路径是jar包内部的路径) -->
<exclude>**/assembly/</exclude>
</excludes>
</configuration>
</plugin>

<!-- The configuration of maven-assembly-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<!-- 对项目的组装进行配置 -->
<configuration>
<!-- 指定assembly插件的配置文件所在位置 -->
<descriptors>
<descriptor>src/main/resources/assembly/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- 将组装绑定到maven生命周期的哪一阶段 -->
<!--<phase>package</phase>-->
<goals>
<!-- 指定assembly插件的打包方式-->
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

package.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>full</id>
<!-- 最终打包成一个用于发布的zip文件 -->
<formats>
<format>zip</format>
</formats>

<!-- 把依赖jar包打包进Zip压缩文件的lib目录下 -->
<dependencySets>
<dependencySet>
<!--不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录-->
<useProjectArtifact>false</useProjectArtifact>

<!-- 第三方jar打包进Zip文件的lib目录下, -->
<!-- 注意此目录要与maven-jar-plugin中classpathPrefix指定的目录相同, -->
<!-- 不然这些依赖的jar包加载到ClassPath的时候会找不到-->
<outputDirectory>lib</outputDirectory>

<!-- 第三方jar不要解压-->
<!--<unpack>false</unpack>-->
</dependencySet>
</dependencySets>

<!-- 文件设置,你想把哪些文件包含进去,或者把某些文件排除掉,都是在这里配置-->
<fileSets>
<!-- 把项目自己编译出来的可执行jar,打包进zip文件的根目录 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>

<!--
把项目readme说明文档,打包进zip文件根目录下
(这里针对目录document/readme.txt文件)
${projet.document.directory}是pom.xml中自己配置的
-->
<fileSet>
<directoryl>${projet.document.directory}</directoryl>
<outputDirectory></outputDirectory>
<includes>
<include>readme.*</include>
</includes>
</fileSet>

<!--
把项目相关的说明文档(除了readme文档),
打包进zip文件根目录下的document目录
(这里针对document/exclode.txt文件)
${project.document.directory}是在pom.xml中自己配置的
-->
<fileSet>
<directory>${project.document.directory}</directory>
<outputDirectory>document</outputDirectory>
<excludes>
<exclude>readme.*</exclude>
</excludes>
</fileSet>

<!--
把项目的脚本文件目录(src/main/scripts )中的启动脚本文件,
打包进zip文件的根目录
(这里针对的是src/scripts/execute/include-file.sh文件)
${project.script.execute.directory}
-->
<fileSet>
<directory>${project.script.execute.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*</include>
</includes>
</fileSet>

</fileSets>
</assembly>