0%

maven生命周期

maven生命周期

maven的命令无论是执行哪个阶段,都会从最初的位置开始执行

我们执行的maven命令就对应了生命周期,比如mvn package。生命周期是一种抽象的描述,实际行为是由插件来完成的。maven为大多数构建步骤都绑定了默认插件

maven有三套生命周期

一、clean生命周期

在构建之前进行一些清理工作,将以前编译得到的旧文件class字节码文件删除

目的是清理项目

  • pre-clean clean之前完成的工作
  • clean 清理上一次构建生成的文件
  • post-clean 执行一些需要在clean之后立刻完成的工作

二、Default 生命周期

构建的核心,编译、测试、打包、安装、部署 每个都是一个目标goal 在插件中指定目标

目的是构建项目

  • validate: 验证工程配置是否正确

  • generate-sources 生成编译阶段需要的任何源码文件

  • process-sources 处理源代码
  • generate-resources 生成工程包中需要包含的资源文件
  • process-resources 复制并处理资源文件,至目标目录,准备打包
  • complie 编译,将java源程序编译成class字节码文件
  • process-classes 处理编译生成的文件,可以对class字节码进行加强和优化
  • generate-test-sources 生成编译阶段需要包含的任何测试源代码
  • process-test-sources 处理测试源代码
  • generate-test-resources 生成编译阶段需要包含的测试资源
  • process-test-resources 复制并处理资源文件,至目标测试目录
  • test-complie 编译测试代码
  • process-test-classes 处理测试代码文件编译后生成的文件
  • test 测试,执行单测
  • prepare-package 在真正打包前执行任何必要的操作
  • package 打包
  • pre-integration-test 在集成测试执行之前,执行所需的操作
  • integration-test 可以将包处理发布到一个能够进行集成测试的环境
  • post-integration-test 集成测试被执行后执行的操作,如清理环境
  • verify 验证包是否有效
  • install 安装至本地仓库
  • deploy 发布到远程仓库

三、Site生命周期

生成项目报告、站点、发布站点

目的是建立项目站点

  • pre-site:执行一些需要在生成站点文档之前的工作

  • site:生成项目的站点文档

  • post-site:执行一些需要在生成站点文档之后完成的工作,并且为部署做准备
  • site-deploy:将生成的站点文档部署到服务器上

插件目标

插件目标表示一个特定的、对构建和管理工程有帮助的任务,可能绑定了0个或多个构建阶段,没有绑定任何构建阶段的目标可以在构建生命周期之外被直接调用执行

maven-dependency-plugin插件有十多个目标,每个目标对应了一个功能,dependency:analyzedependency:treedependency:list(这种方式是通用写法,冒号前边是插件前缀,冒号后边是插件的目标)

绑定

生命周期的阶段与插件的目标进行相互绑定来完成某个具体的构建任务。

内置绑定

maven在内部为一些主要的生命周期阶段绑定了很多插件的目标,当用户通过命令执行生命周期阶段的时候,对应的插件目标就会执行相应的任务。

几个常见的生命周期绑定情况:

  • clean生命周期就是和maven-clean-plugin:clean绑定的
  • process-resources生命周期是和maven-resources-plugin:resources 绑定的
  • compile生命周期是和maven-compiler-plugin:compile 绑定的
  • process-test-resources生命周期是和maven-resources-plugin:testResources 绑定的
  • test生命周期是和maven-surefire-plugin:test 绑定的
  • package生命周期是和maven-jar-plugin:jar 绑定的
  • install生命周期是和maven-install-plugin:install 绑定的
  • deploy生命周期是和maven-deploy-plugin:deploy 绑定的

执行的顺序依赖于目标和构建阶段被调用的顺序

1
2
# clean阶段先执行,然后dependency:copy-dependencies 目标进行执行,最后package阶段执行
mvn clean dependency:copy-dependencies package
自定义绑定

除了内置绑定之外,用户还可以自己选择将某个插件目标绑定到生命周期的某个阶段

比如maven-thrift-plugin是在generate-sources阶段执行compile目标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<plugin>
<groupId>org.apache.thrift.tools</groupId>
<artifactId>maven-thrift-plugin</artifactId>
<version>0.1.10</version>
<configuration>
</configuration>
<executions>
<execution>
<id>thrift-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>

在输入mvn generate-sources时就会发现有如下输出

1
[INFO] --- maven-thrift-plugin:0.1.10:compile (thrift-sources) 

有时候插件是有默认绑定阶段的,这样就不用写phase了。可以通过下述命令查看插件的详细信息

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
mvn help:describe -Dplugin=org.apache.maven.plugins:maven-surefire-plugin:2.12.4 -Ddetail


[INFO] org.apache.maven.plugins:maven-surefire-plugin:2.12.4

Name: Maven Surefire Plugin
Description: Surefire is a test framework project.
Group Id: org.apache.maven.plugins
Artifact Id: maven-surefire-plugin
Version: 2.12.4
Goal Prefix: surefire

This plugin has 2 goals:

surefire:help
Description: Display help information on maven-surefire-plugin.
Call mvn surefire:help -Ddetail=true -Dgoal=<goal-name> to display
parameter details.
Implementation: org.apache.maven.plugin.surefire.HelpMojo
Language: java

Available parameters:

detail (Default: false)
User property: detail
If true, display all settable properties for each goal.

goal
User property: goal
The name of the goal for which to show help. If unspecified, all goals
will be displayed.

indentSize (Default: 2)
User property: indentSize
The number of spaces per indentation level, should be positive.

lineLength (Default: 80)
User property: lineLength
The maximum length of a display line, should be positive.

surefire:test
Description: Run tests using Surefire.
Implementation: org.apache.maven.plugin.surefire.SurefirePlugin
Language: java
Bound to phase: test #默认绑定到test生命周期

Available parameters:

additionalClasspathElements
Additional elements to be appended to the classpath.

argLine
User property: argLine
Arbitrary JVM options to set on the command line.

basedir (Default: ${basedir})
The base directory of the project being tested. This can be obtained in
your integration test via System.getProperty('basedir').

childDelegation (Default: false)
User property: childDelegation
When false it makes tests run using the standard classloader delegation
instead of the default Maven isolated classloader. Only used when forking
(forkMode is not 'none').
Setting it to false helps with some problems caused by conflicts between
xml parsers in the classpath and the Java 5 provider parser.

classesDirectory (Default: ${project.build.outputDirectory})
The directory containing generated classes of the project being tested.
This will be included after the test classes in the test classpath.

classpathDependencyExcludes
List of dependencies to exclude from the test classpath. Each dependency
string must follow the format groupId:artifactId. For example:
org.acme:project-a

classpathDependencyScopeExclude
A dependency scope to exclude from the test classpath. The scope should
be one of the scopes defined by org.apache.maven.artifact.Artifact. This
includes the following:
- compile - system, provided, compile
- runtime - compile, runtime
- compile+runtime - system, provided, compile, runtime
- runtime+system - system, compile, runtime
- test - system, provided, compile, runtime, test

debugForkedProcess
User property: maven.surefire.debug
Attach a debugger to the forked JVM. If set to 'true', the process will
suspend and wait for a debugger to attach on port 5005. If set to some
other string, that string will be appended to the argLine, allowing you
to configure arbitrary debuggability options (without overwriting the
other options specified through the argLine parameter).

disableXmlReport (Default: false)
User property: disableXmlReport
Flag to disable the generation of report files in xml format.

enableAssertions (Default: true)
User property: enableAssertions
By default, Surefire enables JVM assertions for the execution of your
test cases. To disable the assertions, set this flag to 'false'.

environmentVariables
Additional environment variables to set on the command line.

excludedGroups
User property: excludedGroups
(TestNG/JUnit47 provider with JUnit4.8+ only) Excluded groups. Any
methods/classes/etc with one of the groups specified in this list will
specifically not be run.
For JUnit, this parameter forces the use of the 4.7 provider
This parameter is ignored if the suiteXmlFiles parameter is specified.

excludes
A list of <exclude> elements specifying the tests (by pattern) that
should be excluded in testing. When not specified and when the test
parameter is not specified, the default excludes will be
<excludes>
<exclude>**/*$*</exclude>
</excludes>
(which excludes all inner classes).
This parameter is ignored if the TestNG suiteXmlFiles parameter is
specified. Each exclude item may also contain a comma-separated sublist
of items, which will be treated as multiple <exclude> entries.

failIfNoSpecifiedTests
User property: surefire.failIfNoSpecifiedTests
Set this to 'true' to cause a failure if the none of the tests specified
in -Dtest=... are run. Defaults to 'true'.

failIfNoTests
User property: failIfNoTests
Set this to 'true' to cause a failure if there are no tests to run.
Defaults to 'false'.

forkedProcessTimeoutInSeconds
User property: surefire.timeout
Kill the forked test process after a certain number of seconds. If set to
0, wait forever for the process, never timing out.

forkMode (Default: once)
User property: forkMode
Option to specify the forking mode. Can be 'never', 'once', 'always' or
'perthread'. 'none' and 'pertest' are also accepted for backwards
compatibility. 'always' forks for each test-class. 'perthread' will
create 'threadCount' parallel forks.

groups
User property: groups
(TestNG/JUnit47 provider with JUnit4.8+ only) Groups for this test. Only
classes/methods/etc decorated with one of the groups specified here will
be included in test run, if specified.
For JUnit, this parameter forces the use of the 4.7 provider
This parameter is ignored if the suiteXmlFiles parameter is specified.

includes
A list of <include> elements specifying the tests (by pattern) that
should be included in testing. When not specified and when the test
parameter is not specified, the default includes will be
<includes>
<include>**/IT*.java</include>
<include>**/*IT.java</include>
<include>**/*ITCase.java</include>
</includes>
Each include item may also contain a comma-separated sublist of items,
which will be treated as multiple <include> entries.
This parameter is ignored if the TestNG suiteXmlFiles parameter is
specified.

junitArtifactName (Default: junit:junit)
User property: junitArtifactName
Allows you to specify the name of the JUnit artifact. If not set,
junit:junit will be used.

jvm
User property: jvm
Option to specify the jvm (or path to the java executable) to use with
the forking options. For the default, the jvm will be a new instance of
the same VM as the one used to run Maven. JVM settings are not inherited
from MAVEN_OPTS.

objectFactory
User property: objectFactory
(TestNG only) Define the factory class used to create all test instances.

parallel
User property: parallel
(TestNG only) When you use the parallel attribute, TestNG will try to run
all your test methods in separate threads, except for methods that depend
on each other, which will be run in the same thread in order to respect
their order of execution. (JUnit 4.7 provider) Supports values
'classes'/'methods'/'both' to run in separate threads, as controlled by
threadCount.

perCoreThreadCount (Default: true)
User property: perCoreThreadCount
(JUnit 4.7 provider) Indicates that threadCount is per cpu core.

printSummary (Default: true)
User property: surefire.printSummary
Option to print summary of test suites or just print the test cases that
have errors.

properties
List of properties for configuring all TestNG related configurations.
This is the new preferred method of configuring TestNG.

redirectTestOutputToFile (Default: false)
User property: maven.test.redirectTestOutputToFile
Set this to 'true' to redirect the unit test standard output to a file
(found in reportsDirectory/testName-output.txt).

remoteRepositories (Default:
${project.pluginArtifactRepositories})
The remote plugin repositories declared in the POM.

reportFormat (Default: brief)
User property: surefire.reportFormat
Selects the formatting for the test report to be generated. Can be set as
'brief' or 'plain'.

reportNameSuffix
User property: surefire.reportNameSuffix
Add custom text into report filename:
TEST-testClassName-reportNameSuffix.xml,
testClassName-reportNameSuffix.txt and
testClassName-reportNameSuffix-output.txt. File
TEST-testClassName-reportNameSuffix.xml has changed attributes
'testsuite'--'name' and 'testcase'--'classname' - reportNameSuffix is
added to the attribute value.

reportsDirectory (Default:
${project.build.directory}/surefire-reports)
Base directory where all reports are written to.

runOrder (Default: filesystem)
Defines the order the tests will be run in. Supported values are
'alphabetical', 'reversealphabetical', 'random', 'hourly' (alphabetical
on even hours, reverse alphabetical on odd hours), 'failedfirst',
'balanced' and 'filesystem'. Odd/Even for hourly is determined at the
time the of scanning the classpath, meaning it could change during a
multi-module build. Failed first will run tests that failed on previous
run first, as well as new tests for this run. Balanced is only relevant
with parallel=classes, and will try to optimize the run-order of the
tests to make all tests complete at the same time, reducing the overall
execution time. Note that the statistics are stored in a file named
.surefire-XXXXXXXXX beside pom.xml, and should not be checked into
version control. The 'XXXXX' is the SHA1 checksum of the entire surefire
configuration, so different configurations will have different statistics
files, meaning if you change any config settings you will re-run once
before new statistics data can be established.

skip (Default: false)
User property: maven.test.skip
Set this to 'true' to bypass unit tests entirely. Its use is NOT
RECOMMENDED, especially if you enable it using the 'maven.test.skip'
property, because maven.test.skip disables both running the tests and
compiling the tests. Consider using the skipTests parameter instead.

skipExec
User property: maven.test.skip.exec
This old parameter is just like skipTests, but bound to the old property
'maven.test.skip.exec'.
Deprecated. Use skipTests instead.

skipTests (Default: false)
User property: skipTests
Set this to 'true' to skip running tests, but still compile them. Its use
is NOT RECOMMENDED, but quite convenient on occasion.

suiteXmlFiles
(TestNG) List of <suiteXmlFile> elements specifying TestNG suite xml file
locations. Note that suiteXmlFiles is incompatible with several other
parameters of this plugin, like includes/excludes.
This parameter is ignored if the test parameter is specified (allowing
you to run a single test instead of an entire suite).

systemProperties
List of System properties to pass to the JUnit tests.
Deprecated. Use systemPropertyVariables instead.

systemPropertiesFile
List of System properties, loaded from a file, to pass to the JUnit
tests.

systemPropertyVariables
List of System properties to pass to the JUnit tests.

test
User property: test
Specify this parameter to run individual tests by file name, overriding
the includes/excludes parameters. Each pattern you specify here will be
used to create an include pattern formatted like **/${test}.java, so you
can just type '-Dtest=MyTest' to run a single test called
'foo/MyTest.java'.
This parameter overrides the includes/excludes parameters, and the TestNG
suiteXmlFiles parameter. Since 2.7.3, you can execute a limited number of
methods in the test by adding #myMethod or #my*ethod. For example,
'-Dtest=MyTest#myMethod'. This is supported for junit 4.x and testNg.

testClassesDirectory (Default:
${project.build.testOutputDirectory})
The directory containing generated test classes of the project being
tested. This will be included at the beginning of the test classpath. *

testFailureIgnore (Default: false)
User property: maven.test.failure.ignore
Set this to 'true' to ignore a failure during testing. Its use is NOT
RECOMMENDED, but quite convenient on occasion.

testNGArtifactName (Default: org.testng:testng)
User property: testNGArtifactName
Allows you to specify the name of the TestNG artifact. If not set,
org.testng:testng will be used.

testSourceDirectory (Default:
${project.build.testSourceDirectory})
Required: true
The test source directory containing test class sources.

threadCount
User property: threadCount
(forkMode=perthread or TestNG/JUnit 4.7 provider) The attribute
thread-count allows you to specify how many threads should be allocated
for this execution. Only makes sense to use in conjunction with the
parallel parameter. (forkMode=perthread does not support/require the
parallel parameter)

trimStackTrace (Default: true)
User property: trimStackTrace
Whether to trim the stack trace in the reports to just the lines within
the test, or show the full trace.

useFile (Default: true)
User property: surefire.useFile
Option to generate a file test report or just output the test report to
the console.

useManifestOnlyJar (Default: true)
User property: surefire.useManifestOnlyJar
By default, Surefire forks your tests using a manifest-only JAR; set this
parameter to 'false' to force it to launch your tests with a plain old
Java classpath. (See
http://maven.apache.org/plugins/maven-surefire-plugin/examples/class-loading.html
for a more detailed explanation of manifest-only JARs and their
benefits.) Beware, setting this to 'false' may cause your tests to fail
on Windows if your classpath is too long.

useSystemClassLoader (Default: true)
User property: surefire.useSystemClassLoader
Option to pass dependencies to the system's classloader instead of using
an isolated class loader when forking. Prevents problems with JDKs which
implement the service provider lookup mechanism by using the system's
classloader.

useUnlimitedThreads (Default: false)
User property: useUnlimitedThreads
(JUnit 4.7 provider) Indicates that the thread pool will be unlimited.
The parallel parameter and the actual number of classes/methods will
decide. Setting this to 'true' effectively disables perCoreThreadCount
and threadCount. Defaults to 'false'.

workingDirectory
User property: basedir
Command line working directory.

欢迎关注我的其它发布渠道

表情 | 预览
快来做第一个评论的人吧~
Powered By Valine
v1.3.10