maven之Nexus的配置【setting.xml里配置<profile>】(六)

在上一篇文章中,说到了在项目中通过使用<repositories>标签来配置一个Nexus的工厂,使得依赖都能够先从局域网共享仓库(nexus )里寻找。但是这种配置还是不太方便,因为我们每创建一个项目,都要在项目的pom.xml文件里配置<repositories>。

当然了,如果所有项目都继承一个根类项目,那么就只是在根类项目里配置一次<repositories>就OK,其他项目都继承这个根类项目,这种方式也可以。

在现实实际中,不可能所有的项目都会用到继承,而我们又不想老是去配置<repositories>(其实配置一下也OK,看个人喜好啦。),这时可以把<repositories>这部分配置到maven的setting.xml的节点<profiles>里。<profiles>里可以配置多个<profile>,<repositories>里也是可以配置多个<repository>。按照我自己的理解,每个<profile>就相当于<repositories>的<repository>。不同点在于:<repositories>里配置的多个<repository>都是有效的;而<profiles>里配置了多个<profile>,需要使用<activeProfiles>来进行激活,激活了哪个<profile>,哪个<profile>才生效。

<repositories>里配置<repository>在上文中已经说过,这里就不再重复记录了。在这里主要是记录如何把pom.xml中的<repositories>配置,转移到setting.xml里。

【记一个无语活该的错误:因为我是直接在maven/conf/setting.xml进行修改配置,然后在myeclipse里也指定这个setting.xml,导致配置<profile>的时候不起作用(不知道为什么会这样,按照我自己的理解,setting.xml是哪个应该没关系吧,流程是myeclipse会从配置的setting.xml去解析,然后获取到本地仓库地址和是否配置了profile。但是不知道为什么,使用了maven/conf/setting.xml这个后,激活了profile也不起效。)。百度了半天,发现大家的配置都一样。后来我把setting.xml拷贝了一份到E盘,myeclipse里修改setting的路径为E:/setting.xml。一下子配置都没问题了,<profile>的配置也都起作用了。在此记录一下!】

setting.xml中profile的配置如下图:


配上代码:

<profile>
      <id>nexusProfile</id>
      <repositories>
        <repository>
			<id>xx</id>
			<name>111</name>
			<url>http://localhost:8081/nexus/content/groups/public/</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
			<releases>
				<enabled>true</enabled>
			</releases>
			<layout>default</layout>
		</repository>
      </repositories>
    </profile>

<activeProfiles>
		<!--激活了才生效-->
		<activeProfile>nexusProfile</activeProfile>
	</activeProfiles>

在setting.xml中配置了profile以后,pom.xml中的<repositories>就可以去掉了。

测试一下配置是否生效:在pom..xml里添加一个jetty-servlet.jar的依赖,在控制台可以看到如下打印信息:



打开nexus的管理界面,可以在nexus里找到刚下载的jetty-servlet.jar依赖包:


至此,setting.xml里配置profile就成功了。


总述:不管是前一篇文章提到的在pom.xml配置<repositories>,还是在该文章中说到的在setting.xml里配置<profile>,目的都是相同的,都是为了配置一个局域网共享仓库(nexus),起到的作用是,当在同一局域网(一般都是一个项目组,在同一局域网中的。当然了也可能是外网,如分公司与总公司的开发人员,但是如果是外网,那我觉得就没必要配置nexus了,直接去maven中央仓库下载更方便。)的用户更新maven项目,或者往maven里添加依赖的时候,此时会先从本地仓库查找,如果本地仓库不存在某些依赖,会去局域网共享仓库(nexus)里寻找,如果也没有,nexus就会和maven中央仓库打交道,下载对应的依赖包,然后再提交到本地仓库。

所以按照我个人的看法,pom.xml配置<repositories>也好,setting.xml里配置<profile>也罢。同一种效果的不同配置。就像数学问题一样,可以有多种解法。就看个人喜好啦。嘿嘿~~~不过我自己还是偏向于后者,一劳永逸嘛。


  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Maven 的 `settings.xml` 文件是用来配置 Maven 的全局设置和本地仓库等信息的,它位于 Maven 安装目录下的 `conf` 目录中。 下面是一个简单的 `settings.xml` 文件示例: ```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> <mirrors> <mirror> <id>aliyun</id> <mirrorOf>*</mirrorOf> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </mirror> </mirrors> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <env>dev</env> </properties> <repositories> <repository> <id>central</id> <url>https://repo.maven.apache.org/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </profile> <profile> <id>prod</id> <properties> <env>prod</env> </properties> <repositories> <repository> <id>central</id> <url>https://repo.maven.apache.org/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </profile> </profiles> <activeProfiles> <activeProfile>dev</activeProfile> </activeProfiles> </settings> ``` 上述示例中,`<localRepository>` 指定了本地 Maven 仓库的路径,`<mirrors>` 配置了阿云的镜像,`<profiles>` 配置了两个环境的配置信息,`<activeProfiles>` 指定了当前使用的环境。 需要注意的是,`settings.xml` 文件中的配置是全局生效的,如果有多个项目使用同一份配置文件,那么它们都会受到该文件中的配置影响。如果需要针对某个项目单独配置,可以在项目的 `pom.xml` 文件中添加相应的配置

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值