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

2014-11-24 11:52:27 · 作者: · 浏览: 63
e of a project's configuration in Maven. It is a single configuration file that contains the majority of information required to build a project in just the way you want. The POM is huge and can be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it effectively. This project's POM is:
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
What did I just do
You executed the Maven goal archetype:generate, and passed in various parameters to that goal. The prefix archetype is the plugin that contains the goal. If you are familiar with Ant, you may conceive of this as similar to a task. This goal created a simple project based upon an archetype. Suffice it to say for now that a plugin is a collection of goals with a general common purpose. For example the jboss-maven-plugin, whose purpose is "deal with various jboss items".
Build the Project
mvn package
The command line will print out various actions, and end with the following:
...
[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] ------------------------------------------------------------------------
Unlike the first command executed (archetype:generate) you may notice the second is simply a single word - package. Rather than a goal, this is a phase. A phase is a step in the build lifecycle, which is an ordered sequence of phases. When a phase is given, Maven will execute every phase in the sequence up to and including the one defined. For example, if we execute the compile phase, the phases that actually get executed are:
validate
generate-sources
process-sources
generate-resources
process-resources
compile
You may test the newly compiled and packaged JAR with the following command:
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Which will print the quintessential:
Hello World!
创建一个工程
你需要找一个地方存放你的工程。创建一个文件夹目录在某个地方并在此目录下用终端开启一个shell窗口(windows使用cmd命令行)
。在此命令行下,执行如下Maven命令:
[plain]
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
如果你刚刚安装好maven,第一次运行的时候可能需要一段时间。这是由于Maven需要下载最新的一些依赖组件(jar包以及其他文件)
到本地存储库。你可能还需要多执行几次这个命令。这是因为maven远程服务器可能在你下载完成之前出现超时。别慌,有办法去解决这个问题。
你将注意到目标文件夹下会创建一个与artifactId同名的目录文件。切换到这个目录下:
[plain]
cd my-app
在这个文件夹下你会注意到如下标准工程结构:
[plain]
my-app
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- mycompany
| `-- app
| `-- App.java
`-- test
`-- java
`-- com
`-- mycompany
`-- app
`-- AppTest.java
src/main/java目录下包含工程的源文件,src/test/java 目录下包含测试源代码文件,而pom.xml这个文件是该工程的工程对象模型,或称之为POM。
POM
pom.xml文件是maven工程的核心配置文件。它是一个独立的文件,包含了创建