maven

maven是一个异常强大的构建工具,能 够帮我们自动化构建过程,从清理、编译、测试到生成报告,再到打包和部署。我们不需要也不应该一遍又一遍地输入命令,一次又一次地点击鼠标,我们要做的是 使用Maven配置好项目,然后输入简单的命令(如mvn clean install),Maven会帮我们处理那些烦琐的任务。

官方入门文档

Maven in 5 minutes

Pom Reference

maven 下载

Maven与ant两者之间的比较: 
第一:ant脚本是可以直接运行在maven中的。maven和ant最大的差别就是在于maven的编译以及所有的脚本都有一个基础,就是POM (project object model)。这个模型定义了项目的方方面面,然后各式各样的脚本在这个模型上工作,而ant完全是自己定义,显然maven更胜一筹。 
第二:Maven对所依赖的包有明确的定义,如使用那个包,版本是多少,一目了然。而ant则通常是简单的inclde 所有的jar。导致的最终结果就是,你根本无法确定JBoss中的lib下的common-logging 是哪个版本的,唯一的方法就是打开 META-INF 目录下MANIFEST.MF。估计JBoss迟早会转向Maven的。 
第三:Maven是基于中央仓库的编译,即把编译所需要的资源放在一个中央仓库里,如jar,tld,pom等。当编译的时候,maven会自动在仓库中 找到相应的包,如果本地仓库没有,则从设定好的远程仓库中下载到本地。这一切都是自动的,而ant需要自己定义了。这个好处导致的结果就是,用maven 编译的项目在发布的时候只需要发布源码,小得很,而反之,ant的发布则要把所有的包一起发布,显然maven又胜了一筹。 
第四:maven有大量的重用脚本可以利用,如生成网站,生成javadoc,sourcecode reference等。而ant都需要自己去写。
第五:maven目前不足的地方就是没有象ant那样成熟的GUI界面,不过mavengui正在努力中。目前使用maven最好的方法还是命令行,又快又方便。

创建maven项目:mvn archetype:create -DgroupId=yourcompanyname -DartifactId=yourAppName
测试:mvn test
编译:mvn compile
打包:mvn package
生成site:mvn site
清除:mvn clean

 

POM:Project Object Model,项目对象模型。它通过xml格式保存成pom.xml文件,作用类似ant的build.xml文件,功能更强大。

该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。

<project xmlns="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/maven-v4_0_0.xsd">
  <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>

project是所有pom.xml的根元素,它还声明了一些POM相关的命名空间及xsd元素,虽然这些属性不是必须的,但使用这些属性能够让第三方工具(如IDE中的XML编辑器)帮助我们快速编辑POM。

根元素下的第一个子元素modelVersion指定了当前POM模型的版本,对于Maven2及Maven 3来说,它只能是4.0.0。

这段代码中最重要的是groupId,artifactId和version三行。这三个元素定义了一个项目基本的坐标,在Maven的世界,任何的jar、pom或者war都是以基于这些基本的坐标进行区分的。

groupId定义了项目属于哪个组,这个组往往和项目所在的组织或公司存在关联,譬如你在googlecode上建立了一个名为myapp的项目,那么groupId就应该是com.googlecode.myapp,如果你的公司是mycom,有一个项目为myapp,那么groupId就应该是com.mycom.myapp。

artifactId定义了当前Maven项目在组中唯一的ID,我们为这个Hello World项目定义artifactId为hello-world,本书其他章节代码会被分配其他的artifactId。而在前面的groupId为com.googlecode.myapp的例子中,你可能会为不同的子项目(模块)分配artifactId,如:myapp-util、myapp-domain、myapp-web等等。

顾名思义,version指定了Hello World项目当前的版本——1.0-SNAPSHOT。SNAPSHOT意为快照,说明该项目还处于开发中,是不稳定的版本。随着项目的发展,version会不断更新,如升级为1.0、1.1-SNAPSHOT、1.1、2.0等等。

没有任何实际的Java代码,我们就能够定义一个Maven项目的POM,这体现了Maven的一大优点,它能让项目对象模型最大程度地与实际代码相独立,我们可以称之为解耦,或者正交性,这在很大程度上避免了Java代码和POM代码的相互影响。比如当项目需要升级版本时,只需要修改POM,而不需要更改Java代码;而在POM稳定之后,日常的Java代码开发工作基本不涉及POM的修改。

packaging指的是打包的机制,如pom, jar, maven-plugin, ejb, war, ear, rar, par 。classifier即分类。

pom的关系包括依赖,继承,合成。

依赖关系:

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

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

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

scope:用于限制相应的依赖范围

scope有以下几种取值:
1. compile,缺省值,适用于所有阶段,会随着项目一起发布。
2. provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
3. runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
4. test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
5. system,类似provided,需要显式提供包含依赖的jar,Maven不会在 Repository中查找它。

继承关系:

<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>

合成关系:

<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>

如果需要查到依赖的类库,可以到 http://www.mvnrepository.com/ 进行搜索。

build设置

基本元素:

<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的值

resource:资源

<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: 测试资源列表

插件:

<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目标,一个插件可以有多个目标。

scm:软件配置管理

  <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>

 

编写主代码-->编译(mvn clean compile; clean告诉Maven清理输出目录target/,compile告诉Maven编译项目主代码)-->编写测试代码-->测试(mvn clean test)-->打包(mvn clean package; 生成jar包)-->安装(mvn clean install; 将项目输出的jar安装到Maven本地仓库中)-->修改plugin插件配置-->运行。

 

参考:http://www.blogjava.net/zyl/archive/2006/12/30/91055.html

  http://www.cnblogs.com/dcba1112/archive/2011/05/01/2033805.html

 

posted @ 2012-12-07 10:37  cubika  阅读(317)  评论(0编辑  收藏  举报