一、Maven简介
Maven是一个Java项目的构建和管理工具。
二、Maven安装
这里讲述ubuntu系统的安装过程。
Maven依赖于Java,首先确保Java已经正确安装,JAVA_HOME和PATH已经正确设置。
(1)从maven.apache.org下载maven安装包apache-maven-3.0.5-bin.zip
(2)解压到目录/usr/local/apache-maven-3.0.5
(3)配置maven环境变量 M2_HOME=/usr/local/apache-maven-3.0.5,并将$M2_HOME/bin加入PATH路径
(4)测试mvn --version
mvn --version
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 21:51:28+0800)
Maven home: /usr/local/apache-maven-3.0.5
Java version: 1.7.0_07, vendor: Oracle Corporation
Java home: /usr/jdk1.7.0_07/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.2.0-32-generic", arch: "amd64", family: "unix"
三、Maven配置
maven的配置有三个层次:
(1)项目:任何一个项目都有一个pom.xml文件
(2)用户:~/.m2/setting.xml:当前用户的maven配置
(3)全局:$M2_HOME/conf/setting.xml:如果没有配置用户级别的maven配置,则默认是该配置
maven中的重要的一个配置是本地仓库(local repository),默认地址是~/.m2/repository。
可以根据需要自己配置 :
<localRepository>/path/to/local/repo</localRepository>
下载的构件将保存在该目录下,本地生成的构建安装时(mvn install)也将拷贝到该目录下。
远程仓库的配置可以通过项目文件配置也可以通过setting.xml配置,详细可参加私服配置一节。
四、Maven常用命令
0、mvn archetype:generate:创建mvn项目:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
1、mvn clean:清理项目文件
2、mvn compile:编译
3、mvn test:测试
4、mvn package:打包,默认是jar包
5、mvn install:安装到本地仓库
6、mvn deploy:部署远程仓库
7、mvn eclipse:eclipse:构建eclipse项目
8、mvn -Declipse.workspace=<path-to-eclipse-workspace> eclipse:add-maven-repo:将maven本地仓库加入到eclipse工作区。
五、Maven座标和依赖
管理项目依赖是Maven的核心功能之一。Maven是通过构件的座标来管理依赖的。任何一个构件都有一个唯一的座标:groupId、artifactId、version、[classifier]、packaging(默认是jar)
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.6.1</version>
<type></type>
<scope></scope>
<optional></optional>
<exclusions>
<exclusion>
</exclusion>
</exclusions>
</dependency>
其中groupId、artifactId和version是必不可少的。
type表示依赖的类型,也就是packaging的类型jar、war、ear等。默认是jar
scope表示依赖的范围:包括compile、test、provided、runtime、system等。
optional表示依赖是否可选。
exclusions表示要排除的传递性依赖。
依赖常用命令:
mvn dependency:list
mvn dependency:tree
mvn dependency:analyze
六、MVN仓库
Maven仓库的构件存储是文件系统存储。Maven构件存储在文件系统中的目录格式为:groupId/artifactId/version/artifactId-version-classifier.packaging。
Maven仓库主要分为本地仓库和远程仓库。远程仓库可以为中央仓库、第三方仓库、私服仓库等
1、本地仓库
本地仓库的配置在~/.m2/settings.xml或者$M2_HOME/conf/settings.xml中的<localRepository>/path/to/local/repo</localRepository>。默认值是~/.m2/repository。
当maven根据座标寻找构件的时候,它会先从本地仓库寻找,如果找不到则去远程仓库下载。
mvn install命令会将本地构件安装到本地仓库,供本机其他需要的项目使用。
远程仓库可以是中央仓库、第三方仓库也可以私服。
对于一个用户来说,只能有一个本地仓库,却可以有多个远程仓库。
2、中央仓库
中央仓库是maven核心自带的远程仓库(http://repo1.maven.org/maven2)。在默认配置下,当本地仓库找不到构件的时候,它就从中央仓库下载。
3、私服
为了减轻中央仓库的压力,也为了统一管理内部的构建,一般在局域网内自建私服。
当maven需要下载构件的时候,它会首先从私服下载,如果私服上不存在该构件,私服会从配置的远程仓库下载,然后再提供服务给本地maven。此外,一些无法从外部仓库找到的构件也可以上传到本地私服供内部使用。
4、第三方仓库
有的时候中央仓库无法满足需要,需要从其他仓库中下载,比如apache、jboss等
MVN远程仓库的配置方式
可以在pom.xml中配置:下面例子配置了一个本地私服。
<repositories>
<repository>
<id>nexus</id>
<name>Nexus</name>
<url>http://10.241.227.69:8081/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Nexus</name>
<url>http://10.241.227.69:8081/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
也可以在settings.xml中配置
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<name>Nexus</name>
<url>http://10.241.227.69:8081/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Nexus</name>
<url>http://10.241.227.69:8081/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
注意,别忘了激活
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
七、镜像
镜像可以提供原仓库提供服务,比如中央仓库在中国的镜像的配置方式如下:
<mirrors>
<mirror>
<id>maven.net.cn</id>
<name>one of the central mirrors in China</name>
<url>http://maven.net.cn/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
镜像一般结合私服使用,这样私服可以作为所有外部仓库的镜像:假设已经配置了名为nexus的私服仓库
<mirrors>
<mirror>
<id>nexus</id>
<url>http://10.241.227.69:8081/nexus/content/groups/public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>