Introduction to the Build Lifecycle
https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
Maven基于构建生命周期的中心概念。 这意味着,构建和分发特定工件(项目)的过程是明确定义的。 对于构建项目的人,这意味着只需要学习一小组命令来构建任何Maven项目,并且POM将确保他们获得他们想要的结果。
There are three built-in build lifecycles: default, clean and site. The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project's site documentation.
Maven基于构建生命周期的中心概念。 这意味着,构建和分发特定工件(项目)的过程是明确定义的。 对于构建项目的人,这意味着只需要学习一小组命令来构建任何Maven项目,并且POM将确保他们获得他们想要的结果。
There are three built-in build lifecycles: default, clean and site. The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project's site documentation.
A Build Lifecycle is Made Up of Phases
Each of these build lifecycles is defined by a different list of build phases, wherein a build phase represents a stage in the lifecycle.
For example, the default lifecycle comprises of the following phases (for a complete list of the lifecycle phases, refer to the Lifecycle Reference):
验证validate - 验证项目是否正确,并且所有必要的信息都可用
编译 - 编译项目的源代码
测试 - 使用合适的单元测试框架测试编译的源代码。这些测试不应该要求打包或部署代码
package - 获取已编译的代码并以可分发格式(如JAR)打包。
验证verify - 对集成测试的结果进行任何检查,以确保满足质量标准
install - 将软件包安装到本地存储库中,以在本地其他项目中用作依赖关系
部署 - 在构建环境中完成,将最终包复制到远程存储库以与其他开发人员和项目共享。
这些生命周期阶段(以及此处未显示的其他生命周期阶段)按顺序执行,以完成缺省生命周期。给定上面的生命周期阶段,这意味着,当使用默认生命周期时,Maven将首先验证项目,然后尝试编译源代码,对测试运行这些源代码,打包二进制文件(例如jar),运行集成测试包,验证集成测试,将验证包安装到本地存储库,然后将安装的包部署到远程存储库。
通用命令行调用
在开发环境中,使用以下调用来构建并将工件安装到本地存储库中。
mvn install
此命令在执行安装之前按顺序执行每个默认生命周期阶段(验证,编译,打包等)。 您只需要调用要执行的最后一个构建阶段,在这种情况下,安装:
在构建环境中,使用以下调用来干净地构建和部署工件到共享存储库。
mvn clean deploy
相同的命令可以用在多模块场景(即具有一个或多个子项目的项目)中。 Maven遍历每个子项目并执行clean,然后执行deploy(包括所有之前的构建阶段步骤)。
A Build Phase is Made Up of Plugin Goals
However, even though a build phase is responsible for a specific step in the build lifecycle, the manner in which it carries out those responsibilities may vary. And this is done by declaring the plugin goals bound to those build phases.
A plugin goal represents a specific task (finer than a build phase) which contributes to the building and managing of a project. It may be bound to zero or more build phases. A goal not bound to any build phase could be executed outside of the build lifecycle by direct invocation(直接调用). The order of execution depends on the order in which the goal(s) and the build phase(s) are invoked. For example, consider the command below. The clean and package arguments are build phases, while the dependency:copy-dependencies is a goal (of a plugin).
mvn clean dependency:copy-dependencies package
If this were to be executed, the clean phase will be executed first (meaning it will run all preceding phases of the clean lifecycle, plus the clean phase itself), and then the dependency:copy-dependencies goal, before finally executing the package phase (and all its preceding build phases of the default lifecycle).
Moreover, if a goal is bound to one or more build phases, that goal will be called in all those phases.
Furthermore, a build phase can also have zero or more goals bound to it. If a build phase has no goals bound to it, that build phase will not execute. But if it has one or more goals bound to it, it will execute all those goals
Setting Up Your Project to Use the Build Lifecycle
The build lifecycle is simple enough to use, but when you are constructing a Maven build for a project, how do you go about assigning tasks to each of those build phases?
Packaging
The first, and most common way, is to set the packaging for your project via the equally named POM element <packaging>. Some of the valid packaging values are jar, war, ear and pom. If no packaging value has been specified, it will default to jar.
Each packaging contains a list of goals to bind to a particular phase. For example, the jar packaging will bind the following goals to build phases of the default lifecycle.
ocess-resources | resources:resources |
compile | compiler:compile |
process-test-resources | resources:testResources |
test-compile | compiler:testCompile |
test | surefire:test |
package | jar:jar |
install | install:install |
deploy | deploy:deploy |
This is an almost standard set of bindings; however, some packagings handle them differently. For example, a project that is purely metadata (packaging value is pom) only binds goals to the install and deployphases (for a complete list of goal-to-build-phase bindings of some of the packaging types, refer to the Lifecycle Reference).
Note that for some packaging types to be available, you may also need to include a particular plugin in the <build> section of your POM and specify <extensions>true</extensions> for that plugin. One example of a plugin that requires this is the Plexus plugin, which provides a plexus-application and plexus-service packaging.
Plugins
The second way to add goals to phases is to configure plugins in your project. Plugins are artifacts that provide goals to Maven. Furthermore, a plugin may have one or more goals wherein each goal represents a capability of that plugin. For example, the Compiler plugin has two goals: compile and testCompile. The former compiles the source code of your main code, while the latter compiles the source code of your test code.
As you will see in the later sections, plugins can contain information that indicates which lifecycle phase to bind a goal to. Note that adding the plugin on its own is not enough information - you must also specify the goals you want to run as part of your build.
The goals that are configured will be added to the goals already bound to the lifecycle from the packaging selected. If more than one goal is bound to a particular phase, the order used is that those from the packaging are executed first, followed by those configured in the POM. Note that you can use the <executions>element to gain more control over the order of particular goals.
For example, the Modello plugin binds by default its goal modello:java to the generate-sources phase (Note: The modello:java goal generates Java source codes). So to use the Modello plugin and have it generate sources from a model and incorporate that into the build, you would add the following to your POM in the <plugins> section of <build>:
<goals> <goal>java</goal> </goals>
Lifecycle Reference
The following lists all build phases of the default, clean and site lifecycles, which are executed in the order given up to the point of the one specified.
Default Lifecycle
validate | validate the project is correct and all necessary information is available. |
initialize | initialize build state, e.g. set properties or create directories. |
generate-sources | generate any source code for inclusion in compilation. |
process-sources | process the source code, for example to filter any values. |
generate-resources | generate resources for inclusion in the package. |
process-resources | copy and process the resources into the destination directory, ready for packaging. |
compile | compile the source code of the project. |
process-classes | post-process the generated files from compilation, for example to do bytecode enhancement on Java classes. |
generate-test-sources | generate any test source code for inclusion in compilation. |
process-test-sources | process the test source code, for example to filter any values. |
generate-test-resources | create resources for testing. |
process-test-resources | copy and process the resources into the test destination directory. |
test-compile | compile the test source code into the test destination directory |
process-test-classes | post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above. |
test | run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed. |
prepare-package | perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above) |
package | take the compiled code and package it in its distributable format, such as a JAR. |
pre-integration-test | perform actions required before integration tests are executed. This may involve things such as setting up the required environment. |
integration-test | process and deploy the package if necessary into an environment where integration tests can be run. |
post-integration-test | perform actions required after integration tests have been executed. This may including cleaning up the environment. |
verify | run any checks to verify the package is valid and meets quality criteria. |
install | install the package into the local repository, for use as a dependency in other projects locally. |
deploy | done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects. |
Appendix: Maven POM structure
<dependencies>
<groupId>org.opendaylight.openflowplugin.model</groupId>
<build>-
<plugins>
<executions>
| <configuration>
| <codeGenerators>
| <generator>
<dependencies>
<groupId>org.opendaylight.mdsal</groupId>
<groupId>org.opendaylight.controller</groupId>
<dependencies>
<groupId>org.opendaylight.openflowplugin.model</groupId>
<build>-
<plugins>
<executions>
| <configuration>
| <codeGenerators>
| <generator>
<dependencies>
<groupId>org.opendaylight.mdsal</groupId>
<groupId>org.opendaylight.controller</groupId>
评论
发表评论