《Maven 实战》笔记之setting.xml介绍

maven是什么?有什么用?

  Maven是一个跨平台项目管理工具,主要服务于Java平台的项目构建,依赖管理项目信息管理。项目构建包括创建项目框架、清理、编译、测试、到生成报告,再到打包和部署,项目信息包括项目描述,开发者列表,版本,项目文档,测试报告,静态分析报告,源码版本日志报告。

Windows上安装Maven

1.检查JDK:java -v

2.下载maven:https://maven.apache.org/download.cgi 选择apache-maven-3.3.3-bin.zip

3.本地安装:

  解压文件到指定目录下,如D:\apache-maven-3.3.3,在系统变量中新一个M2_HOME变量名,变量值为该安装目录,然后在Path中添加%M2_HOME%\bin,重启命令窗口,输入echo %M2_HOME%,如果输出安装目录,表示配置没问题,再输入mvn -v 输出版本号和path中信息表示安装成功。

seeting.xml文件配置详解

1.配置本地仓库:

  maven默认仓库是:${user.home}/.m2/repository,通常在C盘,随着仓库内容越来越大,放在C盘显然不太好,下面配置是自定义仓库目录。

<localRepository>F:/maven/apache-maven-3.2.5/repo</localRepository>

这里我放在了maven的安装目录下。

2.设置离线模式

如果不想maven连接远程仓库,只使用本地仓库,可将maven设置为离线模式,默认为false。

<offline>false</offline>

3.设置HTTP代理

如果当前网络无法直接连接maven中央仓库,那么就可设置代理连接了。

    <proxy>
          <!-- id -->
          <id>optional</id>
          <!-- 是否启用 -->
          <active>true</active>
          <!-- 协议 -->
          <protocol>http</protocol>
          <username>proxyuser</username>
          <password>proxypass</password>
          <host>proxy.host.net</host>
          <port>80</port>
          <!-- 不使用代理的主机地址 -->
          <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>    

4.设置插件组

  Maven在解析插件仓库元数据的时候,会默认使用org.apache.maven.plugins和org.codehaus.mojo两个groupId。如果增加自定义的插件组可在setting.xml中配置,这样maven在解析命令先会在org/apache/maven/plugins/maven-metadata.xml中找,如果没找到再去org/codehaus/mojo/maven-metadata.xml,如果还没找到就去下面自定义的插件组,如果还没搜索到就报错。

<pluginGroups>
    <pluginGroup>com.your.plugins</pluginGroup>
</pluginGroups>

5.配置服务器认证

如果需要每一个连接maven仓库的连接提供认证信息,才提供相关下载服务,那么可以配置认证。

<servers>
   <server>
      <!-- id与pom中配置的id相同 -->
      <id>depRepo</id>
      <username>user</username>
      <password>pass</password>
   </server>
</servers>
或者
<server>
      <id>siteServer</id>
      <!-- 密匙 -->
      <privateKey>/path/to/private/key</privateKey>
      <!-- 密码 -->
      <passphrase>optional; leave empty if not used.</passphrase>
</server>

6.配置镜像

通常我们直接连接maven中央库下载jar包会比较慢(一是使用的人多,二是中央库在国外),这时如果国内有一个服务器同样可以下载中央库里的所有资源,我们就称它为中央库的镜像,国内镜像越多就能很好的分流,从而提高下载速度。

<mirrors>
   <mirror>
      <id>jboss-public-repository-group</id>
      <name>JBoss Public Repository Group</name>
      <url>http://repository.jboss.org/nexus/content/groups/public</url>
      <mirrorOf>central</mirrorOf>
   </mirror>
</mirrors>

这些镜像地址可以在网上搜索到,其中mirrorOf表示任何对maven中央库的请求都会转到这个镜像上,从而屏蔽掉了maven中央库,如果这个镜像忽然不可用了,那么maven也不会去连接maven中央库。

7.个性化配置

场景一:实现多环境自动切换,如开发环境,生产环境,测试环境。

<profiles>
<!-- 测试环境 -->
  <profile>
    <id>env-test</id>
    <activation>
         <!-- 默认激活该环境 --> 
        <activeByDefault>true</activeByDefault>
     </activation>
      <!-- 定义一个属性文件,其他地方只需要${profiles.activation}即可引用里面的值 -->
    <properties>
       <profiles.activation>xmj_old</profiles.activation>
    </properties>
    <build>
      <plugins>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <version>1.1</version>
            <configuration>
               <!-- 配置项目自动发布服务器 -->
               <url>http://xx.xx.xx.xx:8080/manager/text</url>
               <path>/xmj-manager</path>
               <server>Tomcat</server>
               <warFile>target/${profiles.activation}.war</warFile>
             </configuration>
          </plugin>
       </plugins>
    </build>
  <profile>
  <!-- 生产环境 -->
  <profile>
    <id>env-production</id>
      <!-- 定义一个属性文件,其他地方只需要${profiles.activation}即可引用里面的值 -->
    <properties>
       <profiles.activation>xmj_new</profiles.activation>
    </properties>
    <build>
      <plugins>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <version>1.1</version>
            <configuration>
               <!-- 配置项目自动发布服务器 -->
               <url>http://xx.xx.xx.xx:8080/manager/text</url>
               <path>/xmj-manager</path>
               <server>Tomcat</server>
               <warFile>target/${profiles.activation}.war</warFile>
             </configuration>
          </plugin>
       </plugins>
    </build>
  <profile>
</profiles> 
执行profile
命令:clean instance 使用默认激活环境,也就是dev-test
   clean instance -P env-production 使用 env-production环境构建
 

场景二:配置Nexus仓库

<profile>
        <id>nexus</id>
        <!-- 激活配置 -->
        <activeProfiles>
            <activeProfile>nexus</activeProfile>
        </activeProfiles>
        <!-- 构件仓库 -->
        <repositories>
            <repository>
                <id>public</id>
                <name>Nexus-public</name>
                <url>http://localhost:8081/nexus/content/groups/public/</url>
                <releases><enabled>true</enabled></releases>
                <snapshots><enabled>false</enabled></snapshots>
            </repository>
        </repositories>
        <!-- 插件仓库 -->
        <pluginRepositories>
            <pluginRepository>
                <id>public</id>
                <name>Nexus-public</name>
                <url>http://localhost:8081/nexus/content/groups/public/</url>
                <releases><enabled>true</enabled></releases>
                <snapshots><enabled>false</enabled></snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>
<!-- 激活列表,构建时列表的中的profile均被激活 -->
<activeProfiles>
    <activeProfile>env-test</activeProfile>
    <activeProfile>nexus</activeProfile>
</activeProfiles>

 

  

 

posted @ 2015-07-20 11:17  lavel  阅读(405)  评论(0编辑  收藏  举报