您好,欢迎来到飒榕旅游知识分享网。
搜索
您的当前位置:首页Maven实践

Maven实践

来源:飒榕旅游知识分享网
Maven2实践

2010年11月2日16:00

一、培训前问题1、学员简介

2、用便签纸写下培训期望值3、划分小组,确定小组长4、培训时间:

       上午:9:00‐‐12:00       下午:13:30‐‐16:30六、Introduction to Maven21、Maven2概述

构建项目(Builds)

文档编制(Documentation)报告(Reporting)

依赖管理(Dependencies)配置管理(SCMs)

发布管理(Releases)

2、搭建Maven2使用环境

A、解压缩\"\\software\\Maven\\apache-maven-2.2.1-bin.zip\"解压后的目录:c:\\java\\maven2.2.1B、设置环境变量

set M2_HOME=c:\\java\\maven2.2.1set PATH=%PATH%;%M2_HOME%\\bin//测试MVN命令是否可用mvn --version

C、解压缩“\\software\\Maven\\.m2.rar”解压缩目录:C:\\Users\\Administrator

D、解压缩\\EBook\\mvn-examples-1.0.tar.gz目标目录:C:\\java\\mavencaseE、测试

//测试能否能运行已配好环境的Maven项目cd C:\\java\\mavencase\\ch03-simplemvninstall

系统显示以下信息则为配置成功:[INFO] BUILD SUCCESSFULMaven2核心概念:* 坐标* 依赖* 仓库* 生命周期* 插件

约定优于配置(Convention Over Configuration)Maven项目能过定义POM文件定义项目的配置信息:项目对象模型 (Project Object Model)POM文件中包含的内容:

Maven插件和目标 (Plugins and Goals) Maven生命周期 (Lifecycle)Maven坐标 (Coordinates)

分区企业内训的第1 页

Maven仓库(Repositories)

Maven依赖管理 (Dependency Management)

站点生成和报告 (Site Generation and Reporting)

E、Eclipse中安装m2eclipse插件

m2Eclipse插件:

http://m2eclipse.sonatype.org/sites/m2e

mylyn插件:http://www.eclipse.org/mylyn/downloads/本例中:\\SVN\\mylyn-3.4.2-e3.4.zipSVN插件:http://subclipse.tigris.org本例中:\\SVN\\site-1.6.15.zipCodePro插件:

Eclipse 3.6 (Helios)

http://dl.google.com/eclipse/inst/codepro/latest/3.6Eclipse 3.5 (Galileo)

http://dl.google.com/eclipse/inst/codepro/latest/3.5Eclipse 3.4 (Ganymede)

http://dl.google.com/eclipse/inst/codepro/latest/3.4安装Nexus

复制\\Maven\\nexus-webapp-1.8.0.war到Tomcat webapp目录下在浏览器中执行:http://localhost:8080/nexus-webapp-1.8.0默认的NEXUS用户名和密码是\"admin\" 和 \"admin123\"

D、测试

//测试能否能运行已配好环境的Maven项目cd C:\\java\\mavencase\\ch03-simplemvn install

java -cp C:\\Users\\Administrator\\.m2\\repository\\org\\sonatype\\mavenbook\\ch03\\simple\\1-SNAPSHOT\\simple-1-SNAPSHOT.jar org.sonatype.mavenbook.Appmvn testmvn site 

mvn archetype:create-DgroupId=com.example -DartifactId=my-apppom.xml文件:项目对象模型 (Project Object Model)

要查看 Exec 插件的完整描述:mvn help:describe -Dplugin=exec -Dfull浏览你的项目依赖:mvn dependency:resolveMaven 的调试标记运行:mvn install -X

mvn test -Dmaven.test.failure.ignore=truemvn install -Dmaven.test.skip=true使用Maven管理Web Application ‐‐ch05cd C:\\java\\mavencase\\ch05-simple-webmvn clean installcd simple-webapp

分区企业内训的第2 页

mvn jetty:run

http://localhost:8080/simple-webapp/simple在Maven中多模块项目构建管理1、每个模块一个pom.xml文件

2、在总的项目pom.xml文件中定义以下内容:

simple-weather simple-webapp

Maven Phases

Although hardly a comprehensive list, these are the most common defaultlifecycle 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 defaultlist above. They areclean: cleans up artifacts created by prior buildssite: generates site documentation for this project

第三章:1、新建项目

cd C:\\java\\mavencase\\md traincd  trainmd chap03

cd  C:\\java\\mavencase\rain\\chap03

mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch03 -DartifactId=simple -DpackageName=org.sonatype.mavenbook新版本:

mvn archetype:generate-DgroupId=org.sonatype.mavenbook.ch03 -DartifactId=simple -DpackageName=org.sonatype.mavenbook2、mvn install

3、java -cp target/simple-1.0-SNAPSHOT.jar org.sonatype.mavenbook.ch03.AppgroupId,artifactId, packaging, version——是Maven的坐标(coordinates)

4、“有效的 (effective)”POMmvn help:effective-pom例:

[INFO] Scanning for projects...

[INFO] Searching repository for plugin with prefix: 'help'.

[INFO] ------------------------------------------------------------------------[INFO] Building simple

[INFO] task-segment: [help:effective-pom] (aggregator-style)

分区企业内训的第3 页

[INFO] ------------------------------------------------------------------------[INFO] [help:effective-pom {execution: default-cli}][INFO]

Effective POMs, after inheritance, interpolation, and profiles are applied:

4.0.0

org.sonatype.mavenbook.ch03 simple 1.0-SNAPSHOT simple

http://maven.apache.org

C:\\java\\mavencase\\book\\chap03\\simple\\src\\main\\java

src/main/scripts C:\\java\\mavencase\\book\\chap03\\simple\\src\est\\java

C:\\java\\mavencase\\book\\chap03\\simple\arget\\classes

C:\\java\\mavencase\\book\\chap03\\simple\arget\est-classes

resource-0

C:\\java\\mavencase\\book\\chap03\\simple\\src\\main\\resources

resource-1

C:\\java\\mavencase\\book\\chap03\\simple\\src\est\\resources

C:\\java\\mavencase\\book\\chap03\\simple\arget simple-1.0-SNAPSHOT

分区企业内训的第4 页

maven-antrun-plugin 1.3

maven-assembly-plugin 2.2-beta-2

maven-clean-plugin 2.2

maven-compiler-plugin 2.0.2

maven-dependency-plugin 2.0

maven-deploy-plugin 2.4

maven-ear-plugin 2.3.1

maven-ejb-plugin 2.1

maven-install-plugin 2.2

maven-jar-plugin 2.2

maven-javadoc-plugin 2.5

maven-plugin-plugin 2.4.3

maven-rar-plugin 2.2

maven-release-plugin

分区企业内训的第5 页

2.0-beta-8

maven-resources-plugin 2.3

maven-site-plugin 2.0-beta-7

maven-source-plugin 2.0.4

maven-surefire-plugin 2.4.3

maven-war-plugin 2.1-alpha-2

maven-help-plugin 2.1.1

false central

Maven Repository Switchboard http://repo1.maven.org/maven2

never

false central

Maven Plugin Repository http://repo1.maven.org/maven2

分区企业内训的第6 页

junit

junit 3.8.1 test

C:\\java\\mavencase\\book\\chap03\\simple\arget/site

UTF-8

[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------------[INFO] Total time: < 1 second

[INFO] Finished at: Tue Nov 02 15:34:18 CST 2010[INFO] Final Memory: 5M/15M

[INFO] ------------------------------------------------------------------------用生命周期阶段(phase)参数来运行Maven:rchetype插件的create -install

mvn package 等价于

mvn resources:resources compiler:compile resources:testResources compiler:testCompile surefire:test jar:jar

Maven坐标 (Coordinates)

分区企业内训的第7 页

groupId, artifactId, version和packaging。这些组合的标识符拼成了一个项目的坐标groupId:artifactId:packaging:version

在上面的pom.xml中,它的坐标可以表示为mavenbook:my-app:jar:1.0-SNAPSHOT.这个符号也适用于项目依赖,项目依赖JUnit的3.8.1版本,它包含了一个对junit:junit:jar:3.8.1的依赖。

groupId

d 团体,公司,小组,组织,项目,或者其它团体。团体标识的约定是,它以创建这个项目的组织名称的逆向域名(reverse domain name)开头。来自Sonatype的项目有一个以com.sonatype开头的groupId,而Apache Software的项目有以org.apache开头的groupId。

artifactId

在groupId下的表示一个单独项目的唯一标识符。

version

一个项目的特定版本。发布的项目有一个固定的版本标识来指向该项目的某一个特定的版本。而正在开发中的项目可以用一个特殊的标识,这种标识给版本加上一个“SNAPSHOT”的标记。packaging

项目的类型,默认是jar,描述了项目打包后的输出。类型为jar的项目产生一个JAR文件,类型为war的项目产生一个web应用。

Maven仓库的标准是按

照下面的目录格式来存储构件,相对于仓库的根目录:

////-.在Windows XP上,你的本地仓库很可能

分区企业内训的第8 页

在C:\\Documents and Settings\\USERNAME\\.m2\\repository,在Windows Vista上,会是C:\\Users\\USERNAME\\.m2\\repository。在Unix系统上,你的本地仓库在~/.m2/repositormvn install

Maven依赖管理 (Dependency Management)

在上图中,项目A依赖于项目B和C,项目B依赖于项目D,项目C依赖于项目E,但是项目A所需要做的只是定义对B和C的依赖。当你的项目依赖于其它的项目,而这些项目又有一些小的依赖时(向Hibernate, Apache Struts 或者 Spring Framework),传递性依赖使之变得相当的方便。Maven同时也提供了一种机制,能让你排除一些你不想要的传递性依赖。

你也可以配置Maven,使用provided范围,让它排除WAR文件中特定的依赖。站点生成和报告 (Site Generation and Reporting)另外一个Maven的重要特征是,它能生成文档和报告。mvnsite

定制Maven项目

1、cd  c:\\java\\mavencase\\bookmd chap04cd chap04

mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch04 -DartifactId=simple-weather -DpackageName=org.sonatype.mavenbook -Dversion=1.0maven 编译基于JDK1.5代码时,修改pom.xml文件,增加以下内容:

maven-compiler-plugin

1.5 1.5

UTF-8

增加依赖包信息:

org.apache.commons commons-io 1.3.2

分区企业内训的第9 页

增加Plugin配置:

org.apache.maven.plugins maven-jar-plugin

org.sonatype.mavenbook.weather.Main true

org.apache.maven.plugins maven-surefire-plugin

true

maven-assembly-plugin

jar-with-dependencies

cd C:\\java\\mavencase\\book\\chap04\\simple-weathermvn install

mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.weather.MainMaven Exec 插件

Exec 插件的完整描述:mvn help:describe -Dplugin=exec -Dfull更健壮的解决方案,使用Maven Assembly 插件浏览你的项目依赖

mvn dependency:resolve

项目的整个依赖树mvn dependency:tree

[INFO] Scanning for projects...

[INFO] Searching repository for plugin with prefix: 'dependency'.

[INFO] ------------------------------------------------------------------------[INFO] Building simple-weather

[INFO] task-segment: [dependency:tree]

[INFO] ------------------------------------------------------------------------[INFO] [dependency:tree {execution: default-cli}]

[INFO] org.sonatype.mavenbook.ch04:simple-weather:jar:1.0[INFO] +-log4j:log4j:jar:1.2.14:compile[INFO] +-dom4j:dom4j:jar:1.6.1:compile

[INFO] | \\-xml-apis:xml-apis:jar:1.0.b2:compile[INFO] +-jaxen:jaxen:jar:1.1.1:compile

分区企业内训的第10 页

[INFO] | +-jdom:jdom:jar:1.0:compile

[INFO] | +-xerces:xercesImpl:jar:2.6.2:compile[INFO] | \\-xom:xom:jar:1.0:compile

[INFO] | +-xerces:xmlParserAPIs:jar:2.6.2:compile[INFO] | +-xalan:xalan:jar:2.6.0:compile

[INFO] | \\-com.ibm.icu:icu4j:jar:2.6.1:compile[INFO] +-velocity:velocity:jar:1.5:compile

[INFO] | +-commons-collections:commons-collections:jar:3.1:compile[INFO] | +-commons-lang:commons-lang:jar:2.1:compile[INFO] | \\-oro:oro:jar:2.0.8:compile[INFO] +-junit:junit:jar:3.8.1:test

[INFO] \\-org.apache.commons:commons-io:jar:1.3.2:compile

[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------------[INFO] Total time: 2 seconds

[INFO] Finished at: Tue Nov 02 17:21:10 CST 2010[INFO] Final Memory: 10M/26M

[INFO] ------------------------------------------------------------------------查看完整的依赖踪迹,包含那些因为冲突或者其它原因而被拒绝引入的构件,打开 Maven 的调试标记运行:mvn install -X执行单元测试mvn test

忽略测试失败修改pom.xml文件

org.apache.maven.pluginsmaven-surefire-plugin

true

mvn test -Dmaven.test.failure.ignore=true

跳过单元测试方法一:

mvn install -Dmaven.test.skip=true方法一:修改pom.xml文件内容

org.apache.maven.pluginsmaven-surefire-plugin

true

分区企业内训的第11 页

构建一个打包好的命令行应用程序修改pom.xml文件内容:

maven-assembly-plugin

jar-with-dependencies

mvn install assembly:assembly

md targetcd  target 

java -cp target\\simple-weather-1.0-jar-with-dependencies.jar org.sonatype.mavenbook.weather.Main

创建web 应用程序md chap05cd chap05

mvn archetype:create-DgroupId=org.sonatype.mavenbook.simpleweb -DartifactId=simple-webapp

-Dpackage=org.sonatype.mavenbook -Dversion=1.0-SNAPSHOT配置 Jetty 插件修改pom.xml文件

simple-webapp

org.mortbay.jetty

maven-jetty-plugin

mvncompile

mvn clean install运行web 应用程序

cd C:\\java\\mavencase\\ch05-simple-web\\simple-webappmvn jetty:run

http://localhost:8080/simple-webapp/多模块应用程序pom.xml

simple-weathersimple-webapp

分区企业内训的第12 页

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- sarr.cn 版权所有 赣ICP备2024042794号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务