Maven使用总结

摘要:绝大部分Maven用户都称Maven是一个”构建工具”:一个用来把源代码构建成可发布的构件的工具。 构建工程师和项目经理会说Maven是一个更复杂的东西:一个项目管理工具。那么区别是什么?

1.Maven是什么


如何回答这个问题要看你怎么看这个问题。 绝大部分Maven用户都称Maven是一个”构建工具”:一个用来把源代码构建成可发布的构件的工具。 构建工程师和项目经理会说Maven是一个更复杂的东西:一个项目管理工具。那么区别是什么? 像Ant这样的构建工具仅仅是关注预处理,编译,打包,测试和分发。像 Maven 这样的一个项目管理工具提供了构建工具所提供功能的超集。 除了提供构建的功能,Maven还可以生成报告,生成Web站点,并且帮助推动工作团 队成员间的交流。


一个更正式的 Apache Maven的定义: Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(ProjectLifecycle),一个依赖管理系统(Dependency Management System),和用来运行定义在生命周期阶段(phase)中插件(plugin)目标(goal)的逻辑。 当你使用Maven的时候,你用一个明确定义的项目对象模型来描述你的项目,然后 Maven 可以应用横切的逻辑,这些逻辑来自一组共享的(或者自定义的)插件。

别让Maven是一个”项目管理”工具的事实吓跑你。如果你只是在找一个构建工具,Maven能做这个工作。


2.maven的好处


1. Maven的库是由开源组织维护,不需要我们再花精力去管第三方库,即使自己维护,也比较方便。

2. Maven对jar包的版本管理有工具上的支持,比如将Release版本和Snapshot版本区分开,有利于SCM管理。

3. Maven是标准,用过的人多,不需要额外培训。

4. Maven的plugin比较多,可以有更多功能,Maven现有体系比较开放,采用的技术相对比较通用和成熟,plugin的机制也可以便于我们扩展更多功能。

5. Maven的库下载是即用即下,不需要实现全部down下来。Maven的插件也是自动升级,可以方便的我们扩展新功能。

6. 可以很方便的与eclipse, IDEA这样的主流的IDE集成

7. 版本管理功能,这里的版本管理不是指第三方库的版本管理,而是项目的版本管理

8. 站点功能:它的出现让我们可以对项目的状态一目了然,可以自动的把项目的状态和各种报表以站点的形式发布到内部网或者外部网,可以随时随地查看项目状态。有很多中报表可以选择,包括,doc生成,代码规范的检查,自动bug检查,单元测试报表,单元测试的代码覆盖率报表。


3.Maven环境搭建


a)Apache Maven下载站点:http://maven.apache.org/download.html

b)maven的安装

把Maven解压到安装目录后,需要设置两个环境变量——PATH和M2_HOME。设置这两个环境变量,假设Maven安装目录是 c:\Program Files\maven-2.0.9,键入下面的命令:

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

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

c)验证Maven安装


当Maven安装完成后,你可以检查一下它是否真得装好了,通过在命令行运行 mvn -v。

如果Maven装好了,你应该能看到类似于下面的输出:

$ mvn -v

Maven 2.0.9


4.Setting.xml的配置


4.1.settings.xml基本结构

settings.xml对于maven来说相当于全局性的配置,用于所有的项目。在maven2中存在两个settings.xml,一个位于maven2的安装目录conf下面,作为全局性配置。对于团队设置,保持一致的定义是关键,所以maven2/conf下面的settings.xml就作为团队共同的配置文件。保证所有的团队成员都拥有相同的配置。当然对于每个成员,都需要特殊的自定义设置,如用户信息,所以另外一个settings.xml就作为本地配置。默认的位置为: user.dir/.m2/settings.xml  {user.dir} 指windows 中的用户目录)。settings.xml基本结构如下:


<settingsxmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
        http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository/>
<interactiveMode/>
<usePluginRegistry/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>



4.2. setting.xml主要配置简介


a)localRepository

表示本地库的保存位置,也就是maven2主要的jar保存位置,默认在${user.dir}/.m2/repository,如果需要另外设置,就换成其他的路径。


b)offline

如果不想每次编译,都去查找远程中心库,那就设置为true。当然前提是你已经下载了必须的依赖包。


c)Servers

在POM中的 distributionManagement元素定义了开发库。然而,特定的username和pwd不能使用于pom.xml,所以通过此配置来保存server信息

(红色部分是server的主要配置,想要将产物发布到私服,这三项缺一不可。)


<servers>
<server>  
<id>server001</id>
<username>my_login</username>
<password>my_password</password>  
<privateKey>${usr.home}/.ssh/id_dsa</privateKey>
<passphrase>some_passphrase</passphrase>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
<configuration></configuration>
</server>
</servers>


id:server 的id,用于匹配distributionManagement库id,比较重要。

username, password:用于登陆此服务器的用户名和密码

privateKey, passphrase:设置private key,以及passphrase

filePermissions, directoryPermissions:当库文件或者目录创建后,需要使用权限进行访问。


d)Mirrors

表示镜像库,指定库的镜像,用于增加其他库


<mirrors>
<mirror>
<id>planetmirror.com</id>
<name>PlanetMirror Australia</name>
<url>http://downloads.planetmirror.com/pub/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>


id,name:唯一的标志,用于区别镜像

url:镜像的url

mirrorOf:此镜像指向的服务id


e)Proxies

此设置,主要用于无法直接访问中心的库用户配置。


<proxies>
<proxy>
<id>myproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.somewhere.com</host>
<port>8080</port>
<username>proxyuser</username>
<password>somepassword</password>
<nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
</proxy>
</proxies>


id:代理的标志

active:是否激活代理

protocol, host, port:protocol://host:port 代理

username, password:用户名和密码

nonProxyHosts: 不需要代理的host


f)Profiles

类似于pom.xml中的profile元素,主要包括activation,repositories,properties和pluginRepositories元素。刚开始接触的时候,可能会比较迷惑,其实这是maven2中比较强大的功能。从字面上来说,就是个性配置。单独定义profile后,并不会生效,需要通过满足条件来激活。repositories 和pluginRepositories定义其他开发库和插件开发库。对于团队来说,肯定有自己的开发库。可以通过此配置来定义。

如下的配置,定义了本地开发库,用于release 发布。


<repositories>
<repository>
<id>repo-local</id>
<name>Internal 开发库</name>
<url>http://192.168.0.2:8082/repo-local</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo-local</id>
<name>Internal 开发库</name>
<url>http://192.168.0.2:8082/repo-local</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<layout>default</layout>
</pluginRepository>
</pluginRepositories>


releases, snapshots:每个产品的版本的Release或者snapshot(注:release和snapshot的区别,release一般是比较稳定的版本,而snapshot基本上不稳定,只是作为快照)。properties maven 的properties作为placeholder值,如ant的properties。包括以下的5种类型值:

env.X,返回当前的环境变量

project.x:返回pom中定义的元素值,如project.version

settings.x:返回settings.xml中定义的元素

java 系统属性:所有经过java.lang.System.getProperties()返回的值

x:用户自己设定的值

Activation用于激活此profile


<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.5</jdk>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
<property>
<name>mavenVersion</name>
<value>2.0.3</value>
</property>
<file>
<exists>${basedir}/file2.properties</exists>
<missing>${basedir}/file1.properties</missing>
</file>
</activation>


jdk:如果匹配指定的jdk版本,将会激活

os:操作系统

property:maven能检测到相应的属性

file: 用于判断文件是否存在或者不存在

除了使用activation来激活profile,同样可以通过activeProfiles来激活

g)Active Profiles

表示激活的profile,通过profile id来指定。

<activeProfiles>

<activeProfile>env-test</activeProfile> 指定的profile id

</activeProfiles>


5.Maven中pom.xml语法简介


5.1.什么是pom

pom作为项目对象模型。通过xml表示maven项目,使用pom.xml来实现。主要描述了项目:包括配置文件;开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性,以及其他所有的项目相关因素。

快速察看:


<project>
<modelVersion>4.0.0</modelVersion>
<!-- The Basics -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>
<!-- Build Settings -->
<build>...</build>
<reporting>...</reporting>
<!-- More Project Information -->
<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors>
<!-- Environment Settings -->
<issueManagement>...</issueManagement>
<ciManagement>...</ciManagement>
<mailingLists>...</mailingLists>
<scm>...</scm>
<prerequisites>...</prerequisites>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<distributionManagement>...</distributionManagement>
<profiles>...</profiles>
</project>



5.2.pom.xml主要属性简介

a)groupId:项目或者组织的唯一标志,并且配置时生成的路径也是由此生成,如org.codehaus.mojo生成的相对路径为:/org/codehaus/mojo

b)artifactId: 项目的通用名称

c)version:项目的版本

d)packaging: 打包的机制,如pom, jar, maven-plugin, ejb, war, ear, rar

e)build 设置:


主要用于编译设置,包括两个主要的元素,build和report

1)build主要分为两部分,基本元素和扩展元素集合

注意:包括项目build和profile build


<project>
<build>...</build>
<profiles>
  <profile>
     <build>...</build>
  </profile>
</profiles>
</project>


基本元素


<build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters>
<filter>filters/filter1.properties</filter>
</filters>
 ...
</build>


defaultGoal: 定义默认的目标或者阶段。如install

directory: 编译输出的目录

finalName: 生成最后的文件的样式

filter: 定义过滤,用于替换相应的属性文件,使用maven定义的属性。设置所有placehold的值

资源(resources)

你项目中需要指定的资源。如spring配置文件,log4j.properties


<project>
<build>
   ...
<resources>
<resource>
<targetPath>META-INF/plexus</targetPath>
<filtering>false</filtering>
<directory>${basedir}/src/main/plexus</directory>
<includes>
<include>configuration.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
<testResources>
     ...
</testResources>
   ...
</build>
</project>


resources: resource的列表,用于包括所有的资源

targetPath: 指定目标路径,用于放置资源,用于build

filtering: 是否替换资源中的属性placehold

directory: 资源所在的位置

includes: 样式,包括哪些资源

excludes: 排除的资源

testResources: 测试资源列表

插件

在build时,执行的插件,比较有用的部分,如使用jdk 5.0编译等等


<project>
<build>
   ...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.0</version>
<extensions>false</extensions>
<inherited>true</inherited>
<configuration>
<classifier>test</classifier>
</configuration>
<dependencies>...</dependencies>
<executions>...</executions>
</plugin>
</plugins>
</build>
</project>


extensions: true or false,是否装载插件扩展。默认false

inherited: true or false,是否此插件配置将会应用于poms,那些继承于此的项目

configuration: 指定插件配置

dependencies: 插件需要依赖的包

executions: 用于配置execution目标,一个插件可以有多个目标。


<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>echodir</id>
<goals>
<goal>run</goal>
</goals>
<phase>verify</phase>
<inherited>false</inherited>
<configuration>
<tasks>
<echo>Build Dir: ${project.build.directory}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>


 说明:

id:规定execution 的唯一标志

goals: 表示目标

phase: 表示阶段,目标将会在什么阶段执行

inherited: 和上面的元素一样,设置false maven将会拒绝执行继承给子插件

configuration: 表示此执行的配置属性

插件管理

pluginManagement:插件管理以同样的方式包括插件元素,用于在特定的项目中配置。所有继承于此项目的子项目都能使用。主要定义插件的共同元素、扩展元素集合。

主要包括以下的元素:

Directories用于设置各种目录结构,如下:


<build>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
   ...
</build>


Extensions

表示需要扩展的插件,必须包括进相应的build路径。


<project>
<build>
   ...
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ftp</artifactId>
<version>1.0-alpha-3</version>
</extension>
</extensions>
   ...
</build>
</project>


Reporting

用于在site阶段输出报表。特定的maven 插件能输出相应的定制和配置报表。


<reporting>
<plugins>
<plugin>
<outputDirectory>${basedir}/target/site</outputDirectory>
<artifactId>maven-project-info-reports-plugin</artifactId>
<reportSets>
<reportSet></reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>


Report Sets

用于配置不同的目标,应用于不同的报表


<reporting>
<plugins>
<plugin>
       ...
<reportSets>
<reportSet>
<id>sunlink</id>
<reports>
<report>javadoc</report>
</reports>
<inherited>true</inherited>
<configuration>
<links>
<link>http://java.sun.com/j2se/1.5.0/docs/api/</link>
</links>
</configuration>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>


更多的项目信息

name:项目除了artifactId外,可以定义多个名称

description: 项目描述

url: 项目url

inceptionYear:创始年份


Licenses


<licenses>
<license>
<name>Apache 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>


Organization

配置组织信息


<organization>
<name>Codehaus Mojo</name>
<url>http://mojo.codehaus.org</url>
</organization>


Developers

配置开发者信息


<developers>
<developer>
<id>eric</id>
<name>Eric</name>
<email>eredmond@codehaus.org</email>
<url>http://eric.propellors.net</url>
<organization>Codehaus</organization>
<organizationUrl>http://mojo.codehaus.org</organizationUrl>
<roles>
<role>architect</role>
<role>developer</role>
</roles>
<timezone>-6</timezone>
<properties>
<picUrl>http://tinyurl.com/prv4t</picUrl>
</properties>
</developer>
</developers>


Contributors


<contributors>
<contributor>
<name>Noelle</name>
<email>some.name@gmail.com</email>
<url>http://noellemarie.com</url>
<organization>Noelle Marie</organization>
<organizationUrl>http://noellemarie.com</organizationUrl>
<roles>
<role>tester</role>
</roles>
<timezone>-5</timezone>
<properties>
<gtalk>some.name@gmail.com</gtalk>
</properties>
</contributor>
</contributors>


Continuous Integration Management

连续整合管理,基于triggers或者timings


<ciManagement>
<system>continuum</system>
<url>http://127.0.0.1:8080/continuum</url>
<notifiers>
<notifier>
<type>mail</type>
<sendOnError>true</sendOnError>
<sendOnFailure>true</sendOnFailure>
<sendOnSuccess>false</sendOnSuccess>
<sendOnWarning>false</sendOnWarning>
<configuration><address>continuum@127.0.0.1</address></configuration>
</notifier>
</notifiers>
</ciManagement>


Mailing Lists


<mailingLists>
<mailingList>
<name>User List</name>
<subscribe>user-subscribe@127.0.0.1</subscribe>
<unsubscribe>user-unsubscribe@127.0.0.1</unsubscribe>
<post>user@127.0.0.1</post>
<archive>http://127.0.0.1/user/</archive>
<otherArchives>
<otherArchive>http://base.google.com/base/1/127.0.0.1</otherArchive>
</otherArchives>
</mailingList>
</mailingLists>


SCM

 软件配置管理,如cvs 和svn


<scm>
<connection>scm:svn:http://127.0.0.1/svn/my-project</connection>
<developerConnection>scm:svn:https://127.0.0.1/svn/my-project</developerConnection>
<tag>HEAD</tag>
<url>http://127.0.0.1/websvn/my-project</url>
</scm>


Repositories配置同setting.xml中的开发库

Plugin Repositories配置同 repositories

Distribution Management

用于配置分发管理,配置相应的产品发布信息,主要用于发布,在执行mvn deploy后表示要发布的位置


1 配置到文件系统


<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>file://${basedir}/target/deploy</url>
</repository>
</distributionManagement>


2 使用ssh2配置


<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>scp://sshserver.yourcompany.com/deploy</url>
</repository>
</distributionManagement>


3 使用sftp配置


<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>sftp://ftpserver.yourcompany.com/deploy</url>
</repository>
</distributionManagement>


4 使用外在的ssh配置

编译扩展用于指定使用wagon外在ssh提供,用于提供你的文件到相应的远程服务器。


<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>scpexe://sshserver.yourcompany.com/deploy</url>
</repository>
</distributionManagement>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh-external</artifactId>
<version>1.0-alpha-6</version>
</extension>
</extensions>
</build>


5 使用ftp配置


<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>ftp://ftpserver.yourcompany.com/deploy</url>
</repository>
</distributionManagement>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ftp</artifactId>
<version>1.0-alpha-6</version>
</extension>
</extensions>
</build>


repository 对应于你的开发库,用户信息通过settings.xml中的server取得

Profiles

类似于settings.xml中的profiles,增加了几个元素,如下的样式:


<profiles>
<profile>
<id>test</id>
<activation>...</activation>
<build>...</build>
<modules>...</modules>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<dependencies>...</dependencies>
<reporting>...</reporting>
<dependencyManagement>...</dependencyManagement>
<distributionManagement>...</distributionManagement>
</profile>
</profiles>



5.3.POM间的关系

Pom间的关系主要为依赖,继承,合成


a)依赖关系


<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
</dependency>
...
</dependencies>


groupId, artifactId, version:描述了依赖的项目唯一标志

type:相应的依赖产品包形式,如jar,war

scope:用于限制相应的依赖范围,包括以下的几种变量:

     compile :缺省值,适用于所有阶段,会随着项目一起发布

     provided:provided,类似compile,期望JDK、容器或使用者会提供这个依赖

     runtime:只在运行时使用,如JDBC驱动,适用运行和测试阶段

     test:只在测试时使用,用于编译和运行测试代码。不会随项目发布

     system:类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它

optional: 标注可选,当项目自身也是依赖时。用于连续依赖时使用

独占性:外在告诉maven你只包括指定的项目,不包括相关的依赖。此因素主要用于解决版本冲突问题


<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>2.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</exclusion>
</exclusions>
</dependency>


表示项目maven-embedder需要项目maven-core,但我们不想引用maven-core

b)继承关系

另一个强大的变化,maven带来的是项目继承。主要的设置:

父项目

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<packaging>pom</packaging>
</project
>


父项目packaging 类型必须是pom,我们需要增加相应的值给父pom,用于子项目继承。

子项目


<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<relativePath>../my-parent</relativePath>
</parent>
<artifactId>my-project</artifactId>
</project>


relativePath可以不需要,用于指明parent的目录,用于快速查询。

dependencyManagement:

用于父项目配置共同的依赖关系,主要配置依赖包相同因素,如版本,scope。

c)合成关系(或者多个模块)

一个项目有多个模块,也叫做多重模块,或者合成项目。

如下的定义:


<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<modules>
<module>my-project1<module>
<module>my-project2<module>
</modules>
</project>



(function(){
        Hover.bind(
('.comment-item'), 'del-comment'); });
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值