Maven学习(5)仓库和setting.xml配置

1、仓库是什么

用来统一存储所有Maven共享构建的位置就是仓库。根据Maven坐标定义每个构建在仓库中唯一存储路径大致为:groupId/artifactId/version/artifactId-version.packaging

2、仓库分类

本地仓库

远程仓库

  • 中央仓库(开源jar) http://repo1.maven.org/maven2
  • 私服(即用户到私服去找jar,找不到则私服到中央仓库去下载jar;用户也可以把自己的jar传到私服,其他用户可以从私服获得该jar)

3、setting.xml标签介绍(红色为常用配置)

<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>/path/to/local/repo</localRepository> 配置本地仓库位置,默认值:~/.m2/repository

 <interactiveMode>true</interactiveMode> 表示maven是否需要和用户交互以获得输入。如果maven需要和用户交互以获得输入,则设置成true,反之则应为false。默认为true。

 <offline>false</offline> 表示maven是否需要在离线模式下运行。如果构建系统需要在离线模式下运行,则为true,默认为false。
当由于网络设置原因或者安全因素,构建服务器不能连接远程仓库的时候,该配置就十分有用。

<pluginGroups>

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

</pluginGroups>

当插件的组织id(groupId)没有显式提供时,供搜寻插件组织Id(groupId)的列表。
该元素包含一个pluginGroup元素列表,每个子元素包含了一个组织Id(groupId)。
当我们使用某个插件,并且没有在命令行为其提供组织Id(groupId)的时候,Maven就会使用该列表。默认情况下该列表包含了org.apache.maven.pluginsorg.codehaus.mojo

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

<proxies>

<proxy>

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

</proxies>

用来配置不同的代理。

<servers>

    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>

</servers>

一般,仓库的下载和部署是在pom.xml文件中的repositoriesdistributionManagement元素中定义的。然而,一般类似用户名、密码(有些仓库访问是需要安全认证的)等信息不应该在pom.xml文件中配置,这些信息可以配置在settings.xml中。

<mirrors>

<mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
  </mirrors>

<!--搭建镜像,所有jar都从镜像中下载-->
<!--此处配置所有的构建均从私有仓库中下载 *代表所有,也可以写central -->

如:配置个私服

<mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
</mirror>

 <profiles>

<profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>

<profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>

 </profiles>

根据环境参数来调整构建配置的列表。
settings.xml中的profile元素是pom.xmlprofile元素的裁剪版本
它包含了idactivationrepositoriespluginRepositories和 properties元素。这里的profile元素只包含这五个子元素是因为这里只关心构建系统这个整体(这正是settings.xml文件的角色定位),而非单独的项目对象模型设置。如果一个settings.xml中的profile被激活,它的值会覆盖任何其它定义在pom.xml中带有相同id的profile

<activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>

自动触发profile的条件逻辑。
pom.xml中的profile一样,profile的作用在于它能够在某些特定的环境中自动使用某些特定的值;这些环境通过activation元素指定。
activation元素并不是激活profile的唯一方式。settings.xml文件中的activeProfile元素可以包含profileidprofile也可以通过在命令行,使用-P标记和逗号分隔的列表来显式的激活(如,-P test)。

</settings>

 

 

 

 

猜你喜欢

转载自blog.csdn.net/Carl_changxin/article/details/86525167