Maven打包
[TOC]
# 自定义打zip包
- pom.xml
<!-- 自定义打zip包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- assembly.xml
<assembly>
<!-- 从目标目录拷贝文件去压缩 -->
<id>pro</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<!-- 从目标目录拷贝文件去压缩 -->
<fileSet>
<directory>target</directory>
<includes>
<include>*.jar</include>
</includes>
<outputDirectory>/</outputDirectory>
</fileSet>
<!-- <fileSet>
<directory>target/lib</directory>
<outputDirectory>/lib</outputDirectory>
</fileSet> -->
<!-- <fileSet>
<directory>target/config</directory>
<outputDirectory>/config</outputDirectory>
</fileSet>-->
<!-- 从源目录拷贝文件去压缩 -->
<fileSet>
<directory>bin</directory>
<includes>
<include>*.sh</include>
<include>*.bat</include>
</includes>
<outputDirectory>/</outputDirectory>
</fileSet>
<!-- <fileSet>
<directory>doc</directory>
<includes>
<include>*.*</include>
</includes>
<outputDirectory>/</outputDirectory>
</fileSet> -->
</fileSets>
</assembly>
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
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
# Maven install 导出 jar包
# 1.将所有jar包导出到一个jar包(scope设置不生效)
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!--这里要替换成jar包main方法所在类 -->
<mainClass>com.sf.pps.client.IntfClientCall</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- 指定在打包节点执行jar包合并操作 -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
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
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
# 2.将引用jar包导出到lib目录中
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass></mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
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
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
# 3.scala无法 maven install
<build>
<plugins>
//主要是这一段
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
//
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass></mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
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
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
# 4.Maven导成一个Jar包(scope生效)
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.hbase.coprocessor.UserRelatedInformationObserver</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Maven设置没有版本号的名称
在build中添加<finalName>tool</finalName>
<build>
<finalName>tool</finalName>
...
</build>
1
2
3
4
2
3
4
生成jar包名称则为tool.jar
# Maven 项目中pom文件设置 阿里云
<repositories><!-- 代码库 -->
<repository>
<id>maven-ali</id>
<url>http://maven.aliyun.com/nexus/content/groups/public//</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 指令介绍 validate,compile,Test,package,install等
- 验证 validate 验证项目 验证项目是否正确且所有必须信息是可用的
- 编译 compile 执行编译 源代码编译在此阶段完成
- 测试 Test 测试 使用适当的单元测试框架(例如JUnit)运行测试。
- 包装 package 打包 创建JAR/WAR包如在 pom.xml 中定义提及的包
- 检查 verify 检查 对集成测试的结果进行检查,以保证质量达标
- 安装 install 安装 安装打包的项目到本地仓库,以供其他项目使用
- 部署 deploy 部署 拷贝最终的工程包到远程仓库中,以共享给其他开发人员和工程
package
把项目打包并尝试着运行一次,打包后的资源文件在项目的target 文件夹下面
install
把项目打包并尝试着运行一次,打包后的资源文件在项目的target 文件夹下面
然后把打好的包安装到maven 本地仓库,可以提供给其它项目进行依赖
# 问题处理
# 编译器提示 Missing artifact jdk.tools:jdk.tools:jar:1.7
解决方法:
在pom中加入
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
1
2
3
4
5
6
7
2
3
4
5
6
7
# 引入lib目录(外部)jar包,打包失败
在pom中加入下面配置
<!--引用系统jar包防止打包失败-->
<dependency>
<groupId>msg.izhonghong</groupId>
<artifactId>msg-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>/Users/mac/IdeaProjects/crawl-data-process/lib/msg-client-0.0.1-SNAPSHOT.jar</systemPath>
</dependency>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
上次更新: 2023/04/24, 13:36:49