Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2537681
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-11-09 09:19:10

Apache Maven是一个创新的项目管理工具,提供一种新的概念,通过POM(Project Object Model)来管理项目的build(构建),dependency(依赖)和documentation(文档).它最有用的功能是可以自动的下载项目依赖的库(jar). 

本教程主要记录apache-maven-3.0.3的使用。 


Maven的安装与配置 


在windows上安装maven 

1) 解压maven安装包到合适(任意)的位置 unzip apache-maven-3.0.x.zip 

2) 目录 "apache-maven-3.0.x"将会被创建 

3) 将bin目录加入环境变量PATH, 如: 

Windows 2000/XP/Vista/7
set PATH="c:\program files\apache-maven-3.0.x\bin";%PATH% 

4) 确保JAVA_HOME 已经设置为JDK的安装位置 

5) 运行 "mvn --version" 验证是否安装成功. 

更完整的文档, 查看 




Maven 本地库(Maven local repository)用于存储所有的项目依赖库(library,plugin jars), .当你用Maven 去build 你的项目的时候,它会自动下载依赖的库到本地库。 

Maven 本地库默认的路径: 
Unix/Mac OS X – ~/.m2 on 
Windows – C:\Documents and Settings\username\.m2 on Windows 
1. Maven 配置文件 
Maven local repository 被定义在maven的配置文件中, {M2_HOME}\conf\setting.xml. 
2. 编辑“setting.xml” 

找到 “localRepository” pattern,在“” 元素之间定义新的Maven local repository,如下:

  1. <settings>
  2.   <!-- localRepository
  3.    | The path to the local repository maven will use to store artifacts.
  4.    |
  5.    | Default: ~/.m2/repository
  6.   <localRepository>/path/to/local/repo</localRepository>
  7.   -->
  8.  
  9. <localRepository>D:/maven_repo</localRepository>


3. 保存 
现在Maven local repository的位置变为D:/maven_repo, 将来所有项目依赖的库和相关文件都会被存放在这里. 

maven central repository 
当你使用maven构建项目, Maven将会检查 pom.xml 文件,下载所有的依赖库. 

如果maven在你的本地库(Maven local repository)找不到, 它将尝试从Maven central repository下载, 默认为 


Maven remote repository 

使用Maven, 当你需要引入一些不存在于Maven 中心库( Maven center repository)的资源(jar), 处理将会停止并抛出错误。

例如 
org.jvnet.localizer这个类库在 Maven central repository中不存在, Maven remote repository, 地址为 


  1. <dependency> <groupId>org.jvnet.localizer</groupId>
  2.       <artifactId>localizer</artifactId>
  3.       <version>1.8</version>
  4.     </dependency>


To tell Maven to go to Maven remote repository like java.net, you need to declared a “remote repository” in your Maven’s pom.xml file like this : 
  1. <repositories>
  2.     <repository>
  3.       <id>java.net</id>
  4.       <url>http://download.java.net/maven/2</url>
  5.     </repository>
  6.   </repositories>


Now, Maven’s dependency libraries look-up sequences is changed to : 
Search in Maven local repository, if not found, continue step 2, else exit.
Search in Maven central repository, if not found, continue step 3, else exit.
Search in java.net Maven remote repository – , if not found, prompt error message, else exit. 


怎样让 Maven 自动的下载项目依赖的类库 


案例 

假设你想在你的项目中使用 Log4J进行日志记录 
1. 传统的方式 
访问  
下载 Log4j jar library
手动的将它包含在你的项目依赖里
所有的一切都有你自己管理 


如果 Log4j 版本升级了, 你需要重复上述步骤. 
2. Maven方式 
你需要了解 log4j “Maven coordinates“, 

  1. <groupId>log4j</groupId>
  2.     <artifactId>log4j</artifactId>
  3.     <version>1.2.14</version>

它将会自动的下载log4j 1.2.14 这个版本.如果 “version” tag 被忽略了, 它将会它会在类库升级后自动更新到最新版本 
2.包含 “Maven coordinates” 到“pom.xml” 文件, under “” tag

  1. <dependencies>
  2.     <dependency>
  3.     <groupId>log4j</groupId>
  4.     <artifactId>log4j</artifactId>
  5.     <version>1.2.14</version>
  6.     </dependency>
  7. </dependencies>


3.While Maven is compiling or building, the log4j will download automatically and put it into your Maven local repository 

4.这一切都由maven自动的管理. 
See the different? So what just happened in Maven? 

When you use Maven to build your project, “pom.xml” will be parsed, and the Maven search the log4j library in this order : 
Search log4j in Maven local repository. 
Search log4j in Maven central repository. 
Search log4j in Maven remote repository (if define in pom.xml). 

The Maven dependency library management is quite impressive and handy ~ nice tool. 
How to find the Maven coordinates? 

The only problem is how do you know what Maven coordinates you want to put? To get it, you always can refer to the Maven Central Repository for detail, or using this Google workaround – . 

原文查看:

阅读(1341) | 评论(0) | 转发(0) |
0

上一篇:java 面试题

下一篇:使用maven生成java项目

给主人留下些什么吧!~~