再介绍几个其它命令:
- mvn site : 为你的project创建一个站点
- mvn clean: 清除target目录下的所有文件
- mvn eclipse:eclipse :为project生成eclipse的工程文件和classpath文件
build lifecycle & build phase & goal
maven有一套build的生命周期,是按照一套顺序走下来的,这一套顺序就叫一个生命周期(lifecycle)。maven内置三种生命周期:default, clean 和 site。一个生命周期分为多个build phase,下面是default生命周期全部的build phase:
- 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.
这些build phase是按照顺序执行的,如果执行后面的build phase,前面的build phase 也会被执行。例如如果执行:
mvn deploy
前面的install、verify一直到validate这些build phase都会执行。
每一个build phase是由goal组成的,一个goal其实就是一个任务,一个goal可以关联到一个build phase也可以不关联到任何build phase 。 不关联到任何phase的goal是可以独立执行的,例如:
mvn clean dependency:copy-dependencies package
上面的命令会导致先执行clean这个phase,然后拷贝依赖项,最后打包。注意,这里clean这个goal是clean这个lifecycle里面的一个goal,所以可以看到不同lifecycle的build phase和goal是可以混合在一起执行的。 如果一个goal被绑定到多个phase上,那么goal就会被执行多次。
phase的顺序是已经固定的,如果一个phase没有绑定到任何goal,那么phase就不会被执行。 一个goal可以通过两种方式绑定到一个phase,一个是指定packaging,另一个就是plugin。