五分钟快速搞定maven(maven in 5 minutes)(三)

2014-11-24 11:52:27 · 作者: · 浏览: 65
一个工程的主要信息,正如你所想的那种创建方式。POM信息量巨大,它的复杂性也许会让你望而生畏,但是没有必要去理解它的错综复杂,有效地使用它就好了。该工程的POM如下:
[ html]
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.mycompany.app
my-app
1.0-SNAPSHOT
jar
Maven Quick Start Archetype
http://maven.apache.org
junit
junit
4.8.2
test
我刚才做了什么?
你执行了maven命令archetype:generate,并在使用了一些参数予以通过。archetype 是maven命令包含的一个插件。如果你的Ant熟悉的话,你可以把此次任务想象成是一样的。这个命令创建了一个简单的基于原型的项目。我只想说,一个插件就是一系列的用于一般常见目的的命令。例如jboss-maven-plugin,它的目的是用于“处理各种jboss项目”。
构建这个工程
[sql]
mvn package
这条命令将会打印出一系列的动作,并且以如下结尾:
[sql]
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Thu Jul 07 21:34:52 CEST 2011
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------
不像第一个执行过的命令(archetype:generate),你会注意到这条命令仅仅需要一个单词-package。这是一个阶段而不简单是一个命令。一个阶段是一个构建生命周期中的一个步骤,这是一个有序序列的阶段。当一个阶段给定后,Maven将执行序列中的每一个阶段,包括一个定义。例如,如果我们执行compile阶段,该阶段实际执行中包含:
validate
generate-sources
process-sources
generate-resources
process-resources
compile
你可以用如下的命令来测试新编译、打包过的jar:
[plain]
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
它将打印出经典的:
[plain]
Hello World!
Running Maven Tools
Maven Phases
Although hardly a comprehensive list, these are the most common default lifecycle phases executed.
validate: validate the project is correct and all necessary information is available
compile: compile the source code of the project
test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
package: take the compiled code and package it in its distributable format, such as a JAR.
integration-test: process and deploy the package if necessary into an environment where integration tests can be run
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.
There are two other Maven lifecycles of note beyond the default list above. They are
clean: cleans up artifacts created by prior builds
site: generates site documentation for this project
Phases are actually mapped to underlying goals. The specific goals executed per phase is dependant upon the packaging type of the project. For example, package executes jar:jar if the project type is a JAR, and war:war is the project type is - you guessed it - a WAR.
An interesting thing to note is that phases and goals may be executed in sequence.
mvn clean dependency:copy-dependencies package
This command will clean the project, copy dependencies, and package the project (executing all phases up to package, of course).
Generating the Site
mvn site
This phase