maven笔记一

maven的用途

maven是一个项目构建和管理的工具,提供了帮助管理 构建、文档、报告、依赖、scms、发布、分发的方法。可以方便的编译代码、进行依赖管理、管理二进制库等等。
maven的好处在于可以将项目过程规范化、自动化、高效化以及强大的可扩展性
利用maven自身及其插件还可以获得代码检查报告、单元测试覆盖率、实现持续集成等等。

maven的核心概念介绍

Pom

pom是指project object Model。pom是一个xml,在maven2里为pom.xml。是maven工作的基础,在执行task或者goal时,maven会去项目根目录下读取pom.xml获得需要的配置信息
pom文件中包含了项目的信息和maven build项目所需的配置信息,通常有项目信息(如版本、成员)、项目的依赖、插件和goal、build选项等等
pom是可以继承的,通常对于一个大型的项目或是多个module的情况,子模块的pom需要指定父模块的pom
pom文件中节点为
1. project pom文件的顶级元素
2. modelVersion 所使用的object model版本,为了确保稳定的使用,这个元素是强制性的。除非maven开发者升级模板,否则不需要修改
3. groupId 是项目创建团体或组织的唯一标志符,通常是域名倒写,如groupId org.apache.maven.plugins就是为所有maven插件预留的
4. artifactId 是项目artifact唯一的基地址名
5. packaging artifact打包的方式,如jar、war、ear等等。默认为jar。这个不仅表示项目最终产生何种后缀的文件,也表示build过程使用什么样的lifecycle
6. version artifact的版本,通常能看见为类似0.0.1-SNAPSHOT,其中SNAPSHOT表示项目开发中,为开发版本
7. name 表示项目的展现名,在maven生成的文档中使用
8. url表示项目的地址,在maven生成的文档中使用
9. description 表示项目的描述,在maven生成的文档中使用
10. dependencies 表示依赖,在子节点dependencies中添加具体依赖的groupId artifactId和version
11. build 表示build配置
12. parent 表示父pom
其中groupId:artifactId:version唯一确定了一个artifact

Artifact

这个有点不好解释,大致说就是一个项目将要产生的文件,可以是jar文件,源文件,二进制文件,war文件,甚至是pom文件。每个artifact都由groupId:artifactId:version组成的标识符唯一识别。需要被使用(依赖)的artifact都要放在仓库(见Repository)中

Repositories

Repositories是用来存储Artifact的。如果说我们的项目产生的Artifact是一个个小工具,那么Repositories就是一个仓库,里面有我们自己创建的工具,也可以储存别人造的工具,我们在项目中需要使用某种工具时,在pom中声明dependency,编译代码时就会根据dependency去下载工具(Artifact),供自己使用。
对于自己的项目完成后可以通过mvn install命令将项目放到仓库(Repositories)中
仓库分为本地仓库和远程仓库,远程仓库是指远程服务器上用于存储Artifact的仓库,本地仓库是指本机存储Artifact的仓库,对于windows机器本地仓库地址为系统用户的.m2/repository下面。
对于需要的依赖,在pom中添加dependency即可,可以在maven的仓库中搜索:http://mvnrepository.com/

Build Lifecycle

是指一个项目build的过程。maven的Build Lifecycle分为三种,分别为default(处理项目的部署)、clean(处理项目的清理)、site(处理项目的文档生成)。他们都包含不同的lifecycle。
Build Lifecycle是由phases构成的,下面重点介绍default Build Lifecycle几个重要的phase
• validate 验证项目是否正确以及必须的信息是否可用
• compile 编译源代码
• test 测试编译后的代码,即执行单元测试代码
• package 打包编译后的代码,在target目录下生成package文件
• integration-test 处理package以便需要时可以部署到集成测试环境
• verify 检验package是否有效并且达到质量标准
• install 安装package到本地仓库,方便本地其它项目使用
• deploy 部署,拷贝最终的package到远程仓库和替他开发这或项目共享,在集成或发布环境完成
以上的phase是有序的(注意实际两个相邻phase之间还有其他phase被省略,完整phase见lifecycle),下面一个phase的执行必须在上一个phase完成后
若直接以某一个phase为goal,将先执行完它之前的phase,如mvn install
将会先validate、compile、test、package、integration-test、verify最后再执行install phase

Goal

goal代表一个特定任务
mvn package表示打包的任务,通过上面的介绍我们知道,这个任务的执行会先执行package phase之前的phase
mvn deploy表示部署的任务
mven clean install则表示先执行clean的phase(包含其他子phase),再执行install的phase。
maven用法
主要讲下Archetype以及几种常用项目的创建
maven创建项目是根据Archetype(原型)创建的。下面先介绍下

Archetype

Archetype
原型对于项目的作用就相当于模具对于工具的作用,我们想做一个锤子,将铁水倒入模具成型后,稍加修改就可以了。
类似我们可以根据项目类型的需要使用不同的Archetype创建项目。通过Archetype我们可以快速标准的创建项目。利用Archetype创建完项目后都有标准的文件夹目录结构
既然Archetype相当于模具,那么当然可以自己再造模具了啊,创建Archetype
下面介绍利用maven自带的集中Archetype创建项目。创建项目的goal为mvn archetype:generate,并且指定archetypeArtifactId,其中archetypeArtifactId见maven自带的archetypeArtifactId
quick start工程
创建一个简单的quick start项目,指定 -DarchetypeArtifactId为maven-archetype-quickstart,如下命令

mvn archetype:generate -DgroupId=com.trinea.maven.test -DartifactId=maven-quickstart -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false  

其中DgroupId指定groupId,DartifactId指定artifactId,DarchetypeArtifactId指定ArchetypeId,
DinteractiveMode表示是否使用交互模式,交互模式会让用户填写版本信息之类的,非交互模式采用默认值
这样我们便建好了一个简单的maven项目,目录结构如下:

编译 mvn compile

打包 mvn package,此时target目录下会出现maven-quickstart-1.0-SNAPSHOT.jar文件,即为打包后文件
打包并安装到本地仓库mvn install,此时本机仓库会新增maven-quickstart-1.0-SNAPSHOT.jar文件。
web工程
创建一个简单的web项目,只需要修 -DarchetypeArtifactId为maven-archetype-webapp即可,如下命令

mvn archetype:generate -DgroupId=com.trinea.maven.web.test -DartifactId=maven-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false  

其他:

src\main\resources文件夹是用来存放资源文件的,maven工程默认没有resources文件夹,如果我们需要用到类似log4j.properties这样的配置文件,就需要在src\main文件夹下新建resources文件夹,并将log4j.properties放入其中。
test需要用到资源文件,类似放到src\test下
maven常用参数和命令
主要介绍maven常用参数和命令以及简单故障排除
mvn常用参数
mvn -e 显示详细错误
mvn -U 强制更新snapshot类型的插件或依赖库(否则maven一天只会更新一次snapshot依赖)
mvn -o 运行offline模式,不联网更新依赖
mvn -N仅在当前项目模块执行命令,关闭reactor
mvn -pl module_name在指定模块上执行命令
mvn -ff 在递归执行命令过程中,一旦发生错误就直接退出
mvn -Dxxx=yyy指定java全局属性
mvn -Pxxx引用profile xxx
Build Lifecycle中介绍的命令
mvn test-compile 编译测试代码
mvn test 运行程序中的单元测试

mvn compile 编译项目
mvn package 打包,此时target目录下会出现maven-quickstart-1.0-SNAPSHOT.jar文件,即为打包后文件
mvn install 打包并安装到本地仓库,此时本机仓库会新增maven-quickstart-1.0-SNAPSHOT.jar文件。
每个phase都可以作为goal,也可以联合,如之前介绍的mvn clean install
maven 日用三板斧
mvn archetype:generate 创建maven项目
mvn package 打包,上面已经介绍过了
mvn package -Prelease打包,并生成部署用的包,比如deploy/*.tgz
mvn install 打包并安装到本地库
mvn eclipse:eclipse 生成eclipse项目文件
mvn eclipse:clean 清除eclipse项目文件
mvn site 生成项目相关信息的网站
maven插件常用参数
mvn -Dwtpversion=2.0 指定maven版本
mvn -Dmaven.test.skip=true 如果命令包含了test phase,则忽略单元测试
mvn -DuserProp=filePath 指定用户自定义配置文件位置
mvn -DdownloadSources=true -Declipse.addVersionToProjectName=true eclipse:eclipse 生成eclipse项目文件,尝试从仓库下载源代码,并且生成的项目包含模块版本(注意如果使用公用POM,上述的开关缺省已打开)
maven简单故障排除
mvn -Dsurefire.useFile=false如果执行单元测试出错,用该命令可以在console输出失败的单元测试及相关信息
set MAVEN_OPTS=-Xmx512m -XX:MaxPermSize=256m 调大jvm内存和持久代,maven/jvm out of memory error
mvn -X maven log level设定为debug在运行
mvndebug 运行jpda允许remote debug
mvn –help 这个就不说了。。

maven profiles

Profiles是maven的一个很关键的术语:profile是用来定义一些在build lifecycle中使用的environmental variations,profile可以设置成在不同的环境下激活不同的profile(例如:不同的OS激活不同的profile,不同的JVM激活不同的profile,不同的database激活不同的profile等等)。

定义Profiles

你可以把profiles定义在4个地方:
%M2_HOME%/conf/settings.xml,这是针对该部电脑的所有user的profiles,是global profiles,它会影响所有的maven project build
/.m2/settings.xml,这是针对per user的profiles,是user级的profiles,它会影响当前user的所有maven project build
定义在pom.xml文件里面,这是仅针对该project的profiles,是project级的profiles
profiles.xml,它和pom.xml在同一个目录下,也是project级的profiles,使用profiles.xml的目的是希望把profiles的设置从pom.xml里抽离出来设置。
定义在这4个地方的profiles中,涉及范围越窄的profiles会覆盖范围越宽的profiles。即:定义在pom.xml里profiles会覆盖profiles.xml的,profiles.xml的会覆盖/.m2/settings.xml的,/.m2/settings.xml的会覆盖%M2_HOME%/conf/settings.xml的。
不过请注意:设置在pom.xml里的profiles是最最推荐的,因为pom.xml会被deploy到repository里,所以pom.xml里的profiles才会available for subsequent builds originating from the repository or as transitive dependencies。而settings.xml和profiles.xml里定义的profiles不会被deploy到repository,则有诸多限制,因此,只有下面几个profiles能够在settings.xml和profiles.xml里定义:
• repositories
• pluginRepositories
• properties
• 其他类型的profiles必须在pom.xml里定义(上面3个profiles也可以在pom.xml里定义)。
Pom.xml能够定义的profiles包括:

<repositories><pluginRepositories><dependencies><plugins><properties> (not actually available in the main POM, but used behind the scenes) 
   <modules><reporting><dependencyManagement><distributionManagement>
   a subset of the <build> element, which consists of: 
   <defaultGoal><resources><testResources><finalName>

激活Profiles

激活profiles有下列几种方式:

   Explicitly 
   Through Maven settings 
   Based on environment variables 
   OS settings 
   Present or missing files 

1)通过mvn命令的-P参数来显示激活profiles,该参数值是profile id list(之间用逗号连接)。如:
mvn groupId:artifactId:goal -P profileId-1,profileId-2

2) 通过在settings.xml里设置 element来激活(当然也必须在settings.xml里定义)

<settings>
 ...
          <profiles>
 <profile>
    <id>profile1</id>
    ...
 </profile>
          </profiles>

 <activeProfiles>
    <activeProfile>profile-1</activeProfile>
 </activeProfiles>
 ...
</settings>
        列在<activeProfiles>里的profiles list会在每一个project执行时被激活

3)Profiles还可以基于detect到的build environment 的state来自动激活,而不需要象上面2种方式显式激活。这只需要在profile定义时使用 element。如:

<profiles>
 <profile>
    <activation>
      <jdk>1.4</jdk>
    </activation>
    ...
 </profile>
</profiles>

上面的代码表示:如果JDK version start with 1.4 (eg. “1.4.0_08”, “1.4.2_07”, “1.4”),该profile会被激活

<profiles>
 <profile>
    <activation>
      <property>
        <name>debug</name>
      </property>
    </activation>
    ...
 </profile>
</profiles>

上面的代码表示:如果存在system propertie “debug”,该profile会被激活。为了激活它,输入的命令类似于:
mvn groupId:artifactId:goal –Ddebug

<profiles>
 <profile>
    <activation>
      <property>
        <name>environment</name>
        <value>test</value>
      </property>
    </activation>
    ...
 </profile>
</profiles>

上面的代码表示:如果存在system propertie “environment”的值为test,该profile会被激活。为了激活它,输入的命令类似于:
mvn groupId:artifactId:goal -Denvironment=test
使用Profiles时要注意的2个问题
不是定义在pom.xml里的properties都称为external properties。举例说明最明了:
第一、external properties

不是定义在pom.xml里的properties都称为external properties。举例说明最明了:
pom.xml:

<project>
 ...
 <build>
    <plugins>
      <plugin>
        <groupId>org.myco.plugins</groupId>
        <artifactId>spiffy-integrationTest-plugin</artifactId>
        <version>1.0</version>
        <configuration>
          <appserverHome>${appserver.home}</appserverHome>
        </configuration>
      </plugin>
      ...
    </plugins>
 </build>
 ...
</project>

~/.m2/settings.xml

<settings>
 ...
 <profiles>
    <profile>
      <id>appserverConfig</id>
      <properties>
        <appserver.home>/path/to/appserver</appserver.home>
      </properties>
    </profile>
 </profiles>

 <activeProfiles>
    <activeProfile>appserverConfig</activeProfile>
 </activeProfiles>
 ...
</settings>

当你执行该pom时,运行正常。但如果another user执行时,则运行失败,因为无法解析${appserver.home}(这是由于该properties是定义在user级别的settings.xml)。

解决方法就是把该profile放到pom.xml里定义,但这样做的缺点是所有使用该profile的pom.xml每个都要定义一次该profile。

第二、pom.xml里定义的profiles不符合激活条件
依然是举个例子:
pom.xml:

<project>
 ...
 <profiles>
    <profile>
      <id>appserverConfig-dev</id>
      <activation>
        <property>
          <name>env</name>
          <value>dev</value>
        </property>
      </activation>
      <properties>
        <appserver.home>/path/to/dev/appserver</appserver.home>
      </properties>
    </profile>

    <profile>
      <id>appserverConfig-dev-2</id>
      <activation>
        <property>
          <name>env</name>
          <value>dev-2</value>
        </property>
      </activation>
      <properties>
        <appserver.home>/path/to/dev/appserver2</appserver.home>
      </properties>
    </profile>
 </profiles>

 <build>
    <plugins>
      <plugin>
        <groupId>org.myco.plugins</groupId>
        <artifactId>spiffy-integrationTest-plugin</artifactId>
        <version>1.0</version>
        <configuration>
          <appserverHome>${appserver.home}</appserverHome>
        </configuration>
      </plugin>
      ...
    </plugins>
 </build>
 ...
</project>

上面定义的pom.xml定义了两个profile:不同的”env”参数值会激活不同的profile。当执行命令:

mvn -Denv=dev-2 integration-test

就会激活profile “appserverConfig-dev-2”

当执行命令:
mvn -Denv=dev integration-test
就会激活profile “appserverConfig-dev”

而当执行命令:
mvn -Denv=production integration-test
则运行失败,因为没有激活任何一个profile,因此无法解析${appserver.home}。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值