maven2交流

33
林长琦 Email[email protected]

Upload: changqi-lin

Post on 08-Jul-2015

478 views

Category:

Documents


0 download

DESCRIPTION

内部交流培训文档,主要介绍Maven的基础知识,安装,使用

TRANSCRIPT

Page 1: Maven2交流

林长琦 Email:[email protected]

Page 2: Maven2交流

} 了解Maven2 } 安装Maven2 } 使用Maven2

Page 3: Maven2交流

}  Maven2的目标: ◦ 使项目构建过程更容易; ◦ 提供统一构建系统; ◦ 提供高质量的项目信息; ◦ 提供开发的最佳实践指南; ◦ 能无缝的加入新的特性;

} 对Maven2的错误认识: ◦  Maven2是一个站点和文档制作工具; ◦  Maven2扩展了Ant,使其可以下载到各种依赖包; ◦  Maven2是一系列可以重用的Ant脚本;

Page 4: Maven2交流

}  项目标准化 ◦  定义了项目的标准模板 ◦  参考业界的最佳实践而成 ◦  兼容Eclipse/Netbean/IntelliJ IDEA/… ◦  使用统一的 maven 命令

}  项目代码管理 ◦  标准化生命周期管理:

�  编译->测试->打包->报告生成->部署 ◦  ANT实现类似功能得写很多代码 ◦  可重用ANT脚本 ◦  开源社区有很多好的扩展

�  Tomcat/Jetty的集成 �  静态代码检查 �  持续集成

}  一键部署,构建更容易

Page 5: Maven2交流

} 类库&依赖管理 ◦ 解决JAR包依赖问题 ◦ 解决多项目时对不同版本的依赖问题

} 多项目管理 ◦ 和IDE很好的集成 ◦ 自动解决子项目中的依赖

Page 6: Maven2交流
Page 7: Maven2交流
Page 8: Maven2交流
Page 9: Maven2交流

} 项目唯一标识符 } 构件仓库 } 依赖管理 } 生命周期 } 目标(Goals) } 原型(archetype)

Page 10: Maven2交流

groupId:artifactId:packaging:version

Page 11: Maven2交流

远程仓库服务器

本地仓库服务器

本机仓库

Project1

Project2

Project3

pom.xml

pom.xml

pom.xml

本地计算机

Page 12: Maven2交流

}  多种类型:public / private / locale / … }  Maven仓库组成 ◦  构件的集合 (POM.xml + JAR/WAR) ◦  目录结构:与Maven项目唯一标识符匹配 ◦  /<groupId>/<artifactId>/<version>/<artifactId>-

<version>.<packaging> �  org.apache.commons:commons-email:1.1 �  /org/apache/commons/commons-email/1.1/

}  mvn install }  Maven仓库搜索 ◦  http://repository.sonatype.org/index.html ◦  http://www.mvnbrowser.com/ ◦  http://www.mvnrepository.com/

Page 13: Maven2交流

<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> }  支持传递性依赖 }  需要考虑使用什么JAR包?版本号使多少?所在生命周期是什么?

Scope • compile • provided • test • runtime • system

Page 14: Maven2交流
Page 15: Maven2交流

validate validate the project is correct and all necessary information is available.

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.

Page 16: Maven2交流

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

test run tests 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.

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.

Page 17: Maven2交流

} 一个插件完成某个特定的功能集 }  Maven = 生命周期 + 核心插件 } 一个插件是一个单个或者多个目标的集合 } 一个目标是一个明确的任务,可作为单独的目标运行

$mvn jar:jar $mvn archetype:create …

Page 18: Maven2交流
Page 19: Maven2交流

}  mvn compile }  mvn clean }  mvn test }  mvn package }  mvn install }  Mvn jetty:run }  mvn javadoc:javadoc }  mvn site }  …

Page 20: Maven2交流

} 了解Maven2 } 安装Maven2 } 使用Maven2

Page 21: Maven2交流

} 从Maven主站下载apache-maven-2.1.0-bin.zip }  2. 按照上述“推荐目录结构”将下载的文件直接解压到%DEV_HOME%\tools

}  3. 设置环境变量M2_HOME:%DEV_HOME%\tools\apache-maven-2.1.0

}  4. 设置环境变量M2: %M2_HOME%\bin }  5. 将%M2%加入到Path环境变量设置中 }  6. 打开CMD窗口,运行c:\mvn --version 测试安装是否成功

Page 22: Maven2交流

} 了解Maven2 } 安装Maven2 } 使用Maven2

Page 23: Maven2交流

} 构建项目(Builds) } 文档编制(Documentation) } 报告(Reporting) } 依赖管理(Dependencies) } 配置管理(SCMs) } 发布管理(Releases)

Page 24: Maven2交流

}  Maven2可以构建哪些类型的项目: ◦  EAR ◦  WAR ◦  JAR ◦  ZIP ◦  EJB

Page 25: Maven2交流

} 注意观察生成的项目的目录路径;

}  mvn archetype:create -DgroupId=com.mycompany.ap -DartifactId=my-app ◦ 需要修改的地方: �  -DgroupId= �  -DartifactId=

Page 26: Maven2交流
Page 27: Maven2交流

src/main/java Application/Library sources src/main/resources Application/Library resources

src/main/webapps Web application sources

src/test/java Test sources

src/test/resources Test resources

src/site Site

LICENSE.txt Project's license

README.txt Project's readme

Page 28: Maven2交流

} 所有项目的构建必须遵从标准目录结构;

} 使所有使用Maven2管理的项目从目录上看都是一样的;

} 便于管理,便于协作;

} 遵从业界统一标准,有助于团队管理;

Page 29: Maven2交流

} 总项目代码名称:ebiz-app } 总的Java Package名为:com.xxxx.ebiz } 模块划分 ◦  app-util(jar): 公用工具类 ◦  app-dao(jar): model / dao / ibatis ◦  app-web-common(jar) ◦  app-web-portal(war): 商城前端 ◦  app-web-console(war): 商城管理后端

Page 30: Maven2交流

远程仓库服务器

本地仓库服务器

个人开发环境

个人开发环境

个人开发环境

Page 31: Maven2交流

} 使用Plugin:m2eclipse } 基本过程 ◦ 从版本库中导出项目代码 ◦  Coding ◦  mvn test ◦  mvn jetty:run ◦  Code Checkin ◦  mvn package ◦  mvn install

Page 32: Maven2交流
Page 33: Maven2交流

}  mvn compile }  mvn test }  mvn package }  mvn install }  mvn jetty:run