Maven2可用于构建Java应用工程和Java Web应用工程。
一.构建Java Web工程
在控制台输入:
D:/workme/maven_demo>mvn archetype:create -DgroupId=com.sam.app
-DartifactId=sam-webapp
-DarchetypeArtifactId=maven-archetype-webapp
回车,稍等几分钟后,生成一个标准的Web工程项目。
其中的项目文件pom.xml文件,如下:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sam.app</groupId>
<artifactId>sam-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>sam-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>sam-webapp</finalName>
</build>
</project>
二.构建Java应用工程
在控制台输入:D:/workme/maven_demo>mvn archetype:create
-DgroupId=com.sam.app -DartifactId=my-app
运行完毕,生成my-app工程,pom.xml文件内容如下:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sam.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
三.Maven2工程目录介绍
Maven2推荐大家使用如下标准的目录结构:
src/main/java | 源代码 |
src/main/resources | 资源文件 |
src/main/filters | filters文件 |
src/main/assembly | 项目装配描述 |
src/main/config | 项目配置文件 |
src/main/webapps | Web目录 |
src/test/java | 测试代码 |
src/test/resources | 测试资源文件 |
src/test/filters | 测试filters文件 |
src/site | 项目站点文件 |
license.txt | 项目license |
readme.txt | 项目readme |
pom.xml | 工程的描述文件 |
target | 所有工程编译构建的输出目录 |
四.构建项目的命令主要有,如下几种:(不翻译这些命令的具体功能了,看原 文更简单,^_^!)
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. |
clean | cleans up artifacts created by prior builds |
site | generates site documentation for this project |
详细命令的学习可参见 这里,呵呵。
五. pom.xml干嘛用的?
蛮拉风的xml格式,大家认真看看就知道大概意思了。
pom.xml包含了一个项目的项目对象模型。项目对象模型( POM)是 Maven工作的基本单元。 POM包含了工程的非常 重要的信息块,并且基本上包含了和项目相关的任何要素。
主要的要素说明如下:
poject | 这是pom.xml的顶级元素 |
modelVersion | 这是元素指出了这个POM使用的是那个版本的对象模型。这个模型的版本自身么是经常改变的,但这种改变是为了使模型更加的稳定。 |
groupId | 这个元素指出创建这个工程的组织或团队的唯一标识,并且这个也是一个项目的关键标识,推荐使用这个组织或团队的完整域名。例如:org.apache.maven.plugins是为Maven plug-ins定义的groupId。 |
artifactId | 这个元素指出这个工程的主要制品的基本名称。一个工程的主要制品如果是jar文件,次要制品如果是源码包,则次要制品的名称的一部分也使用artifactId。典型的制品名称使用这样的格式:<artifactId>-<version>.<extension>(例如,myapp-1.0.jar)。 |
packaging | 这个元素指出制品的类型(例如:JAR,WAR,EAR等等)。 |
version | 这个元素指出这个项目产生的制品的版本号,Maven在帮助开发人员管理版本号时走了很长的路,以后你将经常看到SNAPSHOT在一个版本 |
name | 这个元素指出这个工程显示的名称。这个常用于Maven产生的文档中。 |
url | 这个元素指出在哪里能发现工程的站点。这个常用于Maven产生的 |
desription | 这个元素提供了这个工程基本的描述。这个也常用于Maven产生的文档中。 |
详细的介绍可以看 官方介绍。下回见^_^!