maven2.0学习笔记(转载)

from: http://www.360doc.com/content/06/1017/14/5874_232631.shtml

Maven最初的目的是在Jakarta Turbine项目中使构建处理简单化。几个项目之间使用到的Ant build文件差异很小,各个JAR都存入CVS。因此希望有一个标准的方法构建各个工程,清晰的定义一个工程的组成,一个容易的方法去发布项目信息并且去提供一种在各个项目之间共享JAR包。

结果出现了一种功能能用于构建和管理任何基于java的工程。Maven小组希望他们已经做到了一些事情,这将有助于Java开发者更容易的完成每天的工作并且有助于理解任何基于java的项目。

Maven的目标是:

  • 使构建过程更容易

  • 提供统一构建系统

  • 提供高质量的项目信息

  • 提供开发的最佳实践指南

  • 能无缝的加入新的特性

    扫描二维码关注公众号,回复: 1189001 查看本文章

Maven的错误理解

  • Maven是一个站点和文档制作工具。

  • Maven扩展了Ant,使其能下载到各种依赖包

  • Maven是一系列可重用的Ant脚本

Maven的版本。

Maven现在主要有Maven 1.xMaven 2.x,其中现在最新版本是Maven 2.02

Maven 2完成了对Maven 1的重写。重写的首要目的是要提供了强大的Jave构建和包含API的项目,允许Maven被植入任何地方,尤其是高级别的产品如IDEs、质量工具、报告工具等这些。Maven 2构建生命周期的概念正式化,其比Maven更易扩展。

因此现在我们主要研究的就是Maven 2

Maven的安装

  1. Windows 2000/xp下的安装

    1. 解压缩maven-2.0.2-bin.zip到你希望安装Maven 2.0.2的所在目录。这里假设你选择了C:\ProgramFiles\Apache Software Foundation\maven-2.0.2.

    2. C:\Program Files\Apache Software Foundation\maven-2.0.2\bin目录加入到你的%path%环境变量中。

    3. 同时,确认JAVA_HOME是否正确设置成功。

    4. 运行 mvn --version 确认是否安装成功。

显示Maven version: 2.0.2 则表示安装成功。

  1. 基于Unxibased的操作系统(LinuxSolaris and Mac OS X

    1. 解压缩发布包到你希望安装Maven 2.0.2的所在目录。这里假设你选择了/usr/local/maven-

    2. /usr/local/maven-2.0.2/bin目录加入到你的path环境变量中,例如:PATH=/usr/local/maven-2.0.2y/bin: $PATH

    3. 同时,确认JAVA_HOME是否正确设置成功。

    4. 运行 mvn --version 确认是否安装成功。

显示Maven version: 2.0.2 则表示安装成功。

Maven主要功能列表

Maven是一种对项目的管理工具,它提供了一种方式来管理以下项目中涉及到的工作内容,同时以下也是Maven的主要功能:

  • 构建项目(Builds

  • 文档编制(Documentation

  • 报告(Reporting

  • 依赖管理(Dependencies

  • 配置管理(SCMs

  • 发布管理(Releases

构建项目

  1. 首先创建一个Maven工程

Maven可用于构建java应用工程和java web应用工程。

  1.  
    1. WebApp

mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-webapp

-DarchetypeArtifactId=maven-archetype-webapp

my-webapp

|-- pom.xml

`-- src

`-- main

|-- webapp

| |-- WEB-INF

| | `-- web.xml

| `--index.jsp

`-- resources

其他的目录则需要自己补充。

pom.xml文件内容如下:

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

  <groupId>com.mycompany.app</groupId>

  <artifactId>my-webapp</artifactId>

  <packaging>war</packaging>

  <version>1.0-SNAPSHOT</version>

  <name>Maven Webapp Archetype</name>

  <url>http://maven.apache.org</url>

<dependencies>

<dependency>

  <groupId>junit</groupId>

  <artifactId>junit</artifactId>

  <version>3.8.1</version>

  <scope>test</scope>

  </dependency>

  </dependencies>

<build>

  <finalName>my-webapp</finalName>

  </build>

  </project>

 

  1.  
    1. App

mvn archetype:create -DgroupId=com.mycompany.ap -DartifactId=my-app

命令正确执行后,生成如下目录:

my-app

|-- pom.xml

`-- src

|-- main

| `-- java

| `-- com

| `-- mycompany

| `-- app

| `-- App.java

`-- test

`-- java

`-- com

`-- mycompany

`-- app

`-- AppTest.java

pom.xml文件内容如下:

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

  <groupId>com.mycompany.ap</groupId>

  <artifactId>my-app</artifactId>

  <packaging>jar</packaging>

  <version>1.0-SNAPSHOT</version>

  <name>Maven Quick Start Archetype</name>

  <url>http://maven.apache.org</url>

<dependencies>

<dependency>

  <groupId>junit</groupId>

  <artifactId>junit</artifactId>

  <version>3.8.1</version>

  <scope>test</scope>

  </dependency>

  </dependencies>

  </project>

 

 

  1. Maven项目的标准目录介绍

Maven提倡使用一个共同的标准目录结构,使开发人员能在熟悉了一个Maven工程后,对其他的Maven工程也能清晰了解。这样做也省去了很多设置的麻烦。

以下的文档介绍是Maven希望的目录结构,并且也是目录创建工程是采用的目录结构。Maven推荐大家尽可能的遵守这样的目录结构。

src/main/java

Application/Library sources

src/main/resources

Application/Library resources

src/main/filters

Resource filter files

src/main/assembly

Assembly descriptors

src/main/config

Configuration files

src/main/webapps

Web application sources

src/test/java

Test sources

src/test/resources

Test resources

src/test/filters

Test resource filter files

src/site

Site

LICENSE.txt

Project's license

README.txt

Project's readme

在顶级目录上是工程的描述文件pom.xml(如果使用Ant则还包括其他属性文件,maven.xmlbuild.xml,另外还包括提供给最终用户的文件,如,README.txt, LICENSE.txt等等。

 

顶级目录还包括两个子目录:srctarget。顶级目录下可能出现的其他目录仅仅是CVS.svn和其他多模块工程的工程目录,最好不要再有其他目录。

 

Target目录是所有工程编译构建的输出目录。

 

Src目录包含所有工程的源码文件,配置文件,资源文件等等。它下面的子目录一般包含main(主要的工程源文件),test(测试文件),site(项目站点文件)。

  1. 项目构建的生命周期的介绍

Maven 2是围绕着构建生命周期概念设计的。这意味着,构建或者发布的过程已经被清晰的定义了。

当我们使用Maven构建工程时,我们只需要了解几个Maven定义好的命令即可,其他的工作则交给POM来完成。

以下给出Maven提供的构建生命周期列表:

validate

validate the project is correct and all necessary information is available.

generate-sources

generate any source code for inclusion in compilation.

process-sources

process the source code, for example to filter any values.

generate-resources

generate resources for inclusion in the package.

process-resources

copy and process the resources into the destination directory, ready for packaging.

compile

compile the source code of the project.

process-classes

post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.

generate-test-sources

generate any test source code for inclusion in compilation.

process-test-sources

process the test source code, for example to filter any values.

generate-test-resources

create resources for testing.

process-test-resources

copy and process the resources into the test destination directory.

test-compile

compile the test source code into the test destination directory

test

run tests 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.

pre-integration-test

perform actions required before integration tests are executed. This may involve things such as setting up the required environment.

integration-test

process and deploy the package if necessary into an environment where integration tests can be run.

post-integration-test

perform actions required after integration tests have been executed. This may including cleaning up the environment.

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

分享到:
评论
iuottp
  • 浏览: 107582 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

猜你喜欢

转载自rept.iteye.com/blog/693547