安装mavne首先需要安装JDK,并配置环境变量。这部分比较简单,就不再做演示了。
直接解压文章末尾提供的apache-maven-3.1.1-bin.zip文件到D:\maven\apache-maven-3.1.1(可自定义文件位置)即可完成安装。
d、输入mvn –version,出现界面如下,则安装Maven配置成功
b、用户settings.xml,拷贝%MAVEN_HOME%\conf\settings.xml文件到当前系统用户文件下的.m2下的settings.xml文件,并在其中修改为自定义的maven本地仓库存放位置(用户settings.xml并非一开始就有,它的意义在于不修改maven全局配置的情况下,更加合理对的配置用户自己的maven配置文件)。我们只在这里保存一份自定义的settings.xml,配置本地仓库(下载的jar都会放入到配置的D:\maven\repository下)。到时候构建项目的时候,需要用到的jar包,首先回来自定义仓库中寻找,如果没有找到,就从私服下载到这个仓库中!
<!-- 自定义本地仓库存放位置 --> <localRepository>D:\maven\repository</localRepository>
c、自定义settings.xml文件,即拷贝%MAVEN_HOME%\conf\settings.xml文件到当前自定义用户自定义的maven本地仓库存放位置的同级目录下(自定义settings.xml同用户settings.xml一样,它的出现也是为了更加合理的使用maven的配置文件。)
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!-- 设置本地仓库路径 --> <localRepository>D:/maven/repository</localRepository> <!-- 设置发布 jar 包时的用户名及密码 --> <servers> <server> <id>releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>snapshots</id> <username>admin</username> <password>admin123</password> </server> </servers> <!-- 设置 maven 的远程仓库为 nexus --> <mirrors> <mirror> <id>nexus</id> <mirrorOf>*</mirrorOf> <name>Local Repository</name> <url>http://192.168.24.128:8081/nexus/content/groups/public/</url> </mirror> </mirrors> <!-- 设置 nexus 的路径等 --> <profiles> <profile> <id>nexus</id> <repositories> <repository> <id>central</id> <name>local private nexus</name> <url>http://localhost:8081/nexus/content/groups/public</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <name>local private nexus</name> <url>http://localhost:8081/nexus/content/groups/public</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <!-- 激活 nexus --> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles> <!-- 配置eclipse插件 --> <pluginGroups> <pluginGroup>org.mortbay.jetty</pluginGroup> <pluginGroup>org.codehaus.cargo</pluginGroup> </pluginGroups> </settings>
1、本地仓库的路径
2、设置发布 jar 包时的用户名及密码。我们把各个模块构建成了jar或者war文件,发布到私服nexus中需要用到。
3、设置 maven 的远程仓库为 nexus并激活。项目添加jar依赖后,会从设置的本地仓库中查找,如果查找到了,自动添加到项目中;如果没有查到,会访问我们配置好的maven中央仓库,而这里使用<mirror>元素,拦截了对所有仓库的访问,换言之,对任何仓库的访问都会转成对nexus/content/groups/public仓库的访问,而这个仓库是一个仓库组,保持了对所有仓库的访问。
至此,Maven配置完成。
原先以为Maven只是一个构建工具,但其实Maven借助其丰富的插件实现了测试,打包,发布,构建等众多功能,如果有兴趣可以深入了解一下。