Maven

系统 1972 0

 

搞了3天,终于搞定了maven + hudson + p4的集成配置。

下面一一介绍下各个工具的使用。

Maven:

 

  • 安装 , 解压缩,设置环境变量:

C:\Users\tobrien > set M2_HOME=c:\Program Files\maven-2.0.9

C:\Users\tobrien > set PATH=%PATH%;%M2_HOME%\bin

  • Maven 目录结构:

Standard Location Description
pom.xml Maven’s POM, which is always at the top-level of a project.
LICENSE.txt A license file is encouraged for easy identification by users and is optional.
README.txt A simple note which might help first time users and is optional.
target/ Directory for all generated output. This would include compiled classes, generated sources that may be compiled, the generated site or anything else that might be generated as part of your build.
target/generated-sources/plugin-id <plugin-id> <plugin-id> <plugin-id></plugin-id> </plugin-id> </plugin-id> Standard location for generated sources. For example, you may generate some sources from a JavaCC grammar.
src/main/java/ Standard location for application sources.
src/main/resources/ Standard location for application resources.
src/main/filters/ Standard location for resource filters.
src/main/assembly/ Standard location for assembly filters.
src/main/config/ Standard location for application configuration filters.
src/test/java/ Standard location for test sources.
src/test/resources/ Standard location for test resources.
src/test/filters/ Standard location for test resource filters.

 

 

  • mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch03 -DartifactId=simple -DpackageName=org.sonatype.mavenbook

Maven

 

 

 

 

groupId

Maven

 

groupId:

团体,公司,小组,组织,项目,或者其它团体。团体标识的约定是,它以创

建这个项目的组织名称的逆向域名 (reverse domain name) 开头。来自 Sonatype

的项目有一个以 com.sonatype 开头的 groupId ,而 Apache Software 的项目有以

org.apache 开头的 groupId

ArtifactId:

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

Version:

一个项目的特定版本。发布的项目有一个固定的版本标识来指向该项目的某一个

特定的版本。而正在开发中的项目可以用一个特殊的标识,这种标识给版本加上

一个“ SNAPSHOT” 的标记。


项目的打包格式也是 Maven 坐标的重要组成部分,但是它不是项目唯一标识符的一个部

分。一个项目的 groupId:artifactId:version 使之成为一个独一无二的项目;你不能同

时有一个拥有同样的 groupId , artifactId version 标识的项目。


packaging

项目的类型,默认是 jar ,描述了项目打包后的输出。类型为 jar 的项目产生一个

JAR 文件,类型为 war 的项目产生一个 web 应用。



Maven 也提供了不同的依赖范围 (dependency scope) Simple 项目的 pom.xml 包含了

一个依赖—— junit:junit:jar:3.8.1 —— 范围是 test 。当一个依赖的范围是 test

时候,说明它在 Compiler 插件运行 compile 目标的时候是不可用的。它只有在运

compiler:testCompile surefire:test 目标的时候才会被加入到 classpath 中。


当用 Maven 来创建 WAR 或者 EAR ,你可以配置 Maven 让它在生成的构件中捆绑依赖,

你也可以配置 Maven ,使用 provided 范围,让它排除 WAR 文件中特定的依赖。 provided

围告诉 Maven 一个依赖在编译的时候需要,但是它不应该被捆绑在构建的输出中。当你

开发 web 应用的时候 provided 范围变得十分有用,你需要通过 Servlet API 来编译你的代

码,但是你不希望 Servlet API JAR 文件包含在你 web 应用的 WEB-INF/lib 目录中。

 

 

Maven 运行命令 :

mvn archetype:create -DgroupId=org.victor.mvn \

-DartifactId=simple-webapp -DpackageName=org.victor.mvn -DarchetypeArtifactId=maven

 

mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.MyApp

 

mvn compile

 

mvn test

mvn install (mvn clean install)

mvn package

 

mvn assembly:assembly

 

 

 

 

 

 

忽略测试失败

通常,你会开发一个带有很多失败单元测试的系统。 如果你正在实践测试驱动开发

(TDD) ,你可能会使用测试失败来衡量你离项目完成有多远。 如果你有失败的单元

测试,但你仍然希望产生构建输出,你就必须告诉 Maven 让它忽略测试失败。 当

Maven 遇到一个测试失败,它默认的行为是停止当前的构建。 如果你希望继续构

建项目,即使 Surefire 插件遇到了失败的单元测试,你就需要设置 Surefire

testFailureIgnore 这个配置属性为 true

< project >

[...]

< build >

< plugins >

< plugin >

< groupId >org.apache.maven.plugins< /groupId >

< artifactId >maven-surefire-plugin< /artifactId >

< configuration >

< testFailureIgnore >true< /testFailureIgnore >

< /configuration >

< /plugin >

< /plugins >

< /build >

[...]

< /project >

 

 

跳过单元测试

< project >

[...]

< build >

< plugins >

< plugin >

< groupId >org.apache.maven.plugins< /groupId >

< artifactId >maven-surefire-plugin< /artifactId >

< configuration >

< skip >true< /skip >

< /configuration >

< /plugin >

< /plugins >

< /build >

[...]

< /project >

 

 

Maven


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论