在这之前,我自己找了点maven的东西添加进来
1
2
3
4
5
6
7
|
Maven内置变量说明:
${basedir} 项目根目录
${project.build.directory} 构建目录,缺省为target
${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes
${project.build.finalName} 产出物名称,缺省为${project.artifactId}-${project.version}
${project.packaging} 打包类型,缺省为jar
${project.xxx} 当前pom文件的任意节点的内容
|
方式1
修改pom.xml增加如下内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<
plugin
>
<
groupId
>org.apache.maven.plugins
<
artifactId
>maven-jar-plugin
<
version
>2.4
<
configuration
>
<
archive
>
<
manifest
>
<
addClasspath
>true
<
classpathPrefix
>lib/
<
mainClass
>com.sysware.HelloWorld
|
运行mvn clean package即可
批注:
经过我本人自测之后发现,这种方式虽然可以打包,但是不会将依赖包也添加进去。所以我认为这种方式的打包适合写一个工具,然后打成JAR包,导入其他工程使用,如果是一个单独运行的JAR包,则不适用。
方式2
在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
|
<
plugin
>
<
groupId
>org.apache.maven.plugins
<
artifactId
>maven-assembly-plugin
<
version
>2.3
<
configuration
>
<
appendAssemblyId
>false
<
descriptorRefs
>
<
descriptorRef
>jar-with-dependencies
<
archive
>
<
manifest
>
<
mainClass
>com.juvenxu.mvnbook.helloworld.HelloWorld
<
executions
>
<
execution
>
<
id
>make-assembly
<
phase
>package
<
goals
>
<
goal
>assembly
|
运行mvn assembly:assembly (我自己使用mvn clean package也是可以的)
批注:
这个方式和方式1不同,这种打包方式,将项目及所依赖的所有jar包打包成一个jar,让打出来的JAR包成为一个可独立运行的JAR包
方式3
批注:
我自己在博主的基础上,添加了一些配置和注释
方式3和方式4都是比较复杂的打包方式,但是可以自己定义细节,比如需要哪些文件被打包,哪些文件不打包进去,哪些文件放到哪里等等。
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
|
<
build
>
<
finalName
>test-${project.version}
<
sourceDirectory
>src/main/java
<
resources
>
<
resource
>
<
directory
>src/main/resources
<
targetPath
>${project.build.directory}
<
excludes
>
<
exclude
>*.txt
<
includes
>
<
include
>*.properties
<
plugins
>
<
plugin
>
<
groupId
>org.apache.maven.plugins
<
artifactId
>maven-compiler-plugin
<
configuration
>
<
defaultLibBundleDir
>lib
<
source
>1.7
<
target
>1.7
<
encoding
>UTF-8
<
plugin
>
<
groupId
>org.apache.maven.plugins
<
artifactId
>maven-jar-plugin
<
configuration
>
<
archive
>
<
manifest
>
<
addClasspath
>true
<
classpathPrefix
>lib/
<
mainClass
>com.eya.main.Hello
<
plugin
>
<
groupId
>org.apache.maven.plugins
<
artifactId
>maven-dependency-plugin
<
executions
>
<
execution
>
<
id
>copy
<
phase
>package
<
goals
>
<
goal
>copy-dependencies
<
configuration
>
<
outputDirectory
>
${project.build.directory}/lib
<
plugin
>
<
groupId
>org.apache.maven.plugins
<
artifactId
>maven-resources-plugin
<
version
>2.4
<
configuration
>
<
encoding
>UTF-8
<
plugin
>
<
artifactId
>maven-source-plugin
<
version
>2.1
<
configuration
>
<
attach
>true
<
encoding
>UTF-8
<
executions
>
<
execution
>
<
phase
>compile
<
goals
>
<
goal
>jar
|
方式4
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
|
<
build
>
<
resources
>
<
resource
>
<
targetPath
>${project.build.directory}/classes
<
directory
>src/main/resources
<
filtering
>true
<
includes
>
<
include
>**/*.xml
<
plugins
>
<
plugin
>
<
groupId
>org.apache.maven.plugins
<
artifactId
>maven-compiler-plugin
<
version
>3.0
<
configuration
>
<
source
>1.6
<
target
>1.6
<
encoding
>UTF-8
<
plugin
>
<
groupId
>org.apache.maven.plugins
<
artifactId
>maven-shade-plugin
<
version
>2.0
<
executions
>
<
execution
>
<
phase
>package
<
goals
>
<
goal
>shade
<
configuration
>
<
transformers
>
<
transformer
implementation
=
"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"
>
<
mainClass
>com.test.testguava.app.App
<
transformer
implementation
=
"org.apache.maven.plugins.shade.resource.AppendingTransformer"
>
<
resource
>applicationContext.xml
<
shadedArtifactAttached
>true
<
shadedClassifierName
>executable
|