用到了maven,读的《maven权威指南》,总结于下,以备后查
关于maven:
Maven是一个项目管理工具,它包含了:
一个项目对象模型 (Project Object Model),
一组标准集合,
一个项目生命周期(Project Lifecycle),
一个依赖管理系统(Dependency Management System),
和用来运行定义在生命周期阶段(phase)中插件(plugin)目标(goal)的逻辑。
maven结构
maven是插件式结构,所以maven的命令 通常如此 :
mvn pluginname:goal -args..
mvn为入口,插件实现功能,goal为插件中的子命令,后面跟上参数
pom
project object model,maven构建地图
包含四部分信息:
项目总体信息(项目坐标,网址,许可证,开发者等)
构建配置(自定义maven构建行为,配置编译路径等)
构建环境(可以针对不同环境配置不同的profile)
POM关系(父项目,子项目等关系)
关于maven坐标:
由groupId,artifactId,package,version组成,项目的唯一标示
常用maven命令(插件:goal)
mvn archetype:create -DgroupId=xxx -DatifactId=xxx -DpackageName=xxx -Dversion=xxx 创建新项目 (如果是web app 需要加上 -DarchetypeArtifactId=maven-archetype-webapp)
mvn install(包含了一系列动作)如下:
mvn resources:resources \
compiler:compile \
resources:testResources \
compiler:testCompile \
surefire:test \
jar:jar
mvn help:describe -Dplugin=xxx -Dfull 帮助
mvn dependency:resole 查看项目依赖
mvn dependency:tree 树形显示依赖
mvn test 执行单元测试,执行到test为止的过程
mvn test -Dmaven.test.failure.ignore=true 忽略测试失败
mvn install -Dmaven.test.skip=true 跳过单元测试
mvn site 站点生成和报告
mvn exec:java -Dexec.mainClass=xxx.xxx.xxx 运行某个类
添加jetty支持,添加servlet依赖
<project>
[...]
<build>
<finalName>simple-webapp</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
</plugins>
</build>
[...]
</project>
<project>
[...]
<dependencies>
[...]
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_2.4_spec</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
[...]
</project>
多模块项目
父项目配置:
<groupId>org.sonatype.mavenbook.ch06</groupId>
<artifactId>simple-parent</artifactId>
<packaging>pom</packaging> <!-- 注意packaging为pom,父项目为对其他项目的引用 -->
<version>1.0</version>
<name>Chapter 6 Simple Parent Project</name>
<modules>
<module>simple-weather</module>
<module>simple-webapp</module>
</modules>
子项目配置
<parent> <!-- 用parent标签对父项目进行声明 -->
<groupId>org.sonatype.mavenbook.ch06</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>simple-webapp</artifactId>
<packaging>war</packaging>
<name>simple-webapp Maven Webapp</name>