maven私有库和公有库混合使用的两种方式

私有库和公有库混合使用的两种方式

maven setting.xml 配置:

网上一堆资料:https://www.cnblogs.com/hongmoshui/p/10762272.html

1. nexus代理公有库

nexus搭建私有库这个方式也挺常见的,详细步骤百度。

私有库所在服务器是一台互联网机器,那么setting.xml的配置就可以是下面这种:

<?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:\repository</localRepository>

	<servers>
		<server>
			<id>myServer</id>
			<username>admin</username>
			<password>123456</password>
		</server>
	</servers>
	<mirrors>
		<mirror>
			<id>my</id>
			<mirrorOf>*</mirrorOf>
			<name>my Repository</name>
			<url>http://localhost:8081/repository/maven-group/</url>
		</mirror>
    </mirrors>
	<profiles>
		<profile>
			<id>dev</id>
			<activation>
				<activeByDefault>true</activeByDefault>
				<jdk>1.8</jdk>
			</activation>
			<repositories>
				<repository>
					<id>my</id>
					<url>http://locahost:8081/repository/maven-public</url>
					<releases>
						<enabled>true</enabled>
						<updatePolicy>always</updatePolicy>
					</releases>
					<snapshots>
						<enabled>true</enabled>
						<updatePolicy>always</updatePolicy>
					</snapshots>
				</repository>
			</repositories>
		</profile>

	</profiles>
	<activeProfiles>
		<activeProfile>dev</activeProfile>
	</activeProfiles>
</settings>

通过mirrorOf所有的maven拉取请求都通过上面配置的mirrorid为meiya的通道,而后对于依赖的拉取会对混合库里的仓库逐个拉取,直到拉取到jar(主要这里的mirrorOf值为*

2. setting.xml配置私有库和公有库

这种情况是私有仓库所在的服务器是不能连接外网的,那这时候去配置公有库代理是无意义的,但是,开发机本身可联网。

这种情况的setting.xml配置就可以是下面这种

<?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:\repository</localRepository>
<!--  <offline>true</offline>-->
  <servers>
	 <server>
      <id>myServer</id>
      <username>admin</username>
      <password>123456</password>
    </server>
  </servers>

  <mirrors>
	  <mirror>
		  <id>my</id>
		  <mirrorOf>my</mirrorOf>
		  <name>central repository</name>
		  <url>http://localhost:8081/repository/maven-public/</url>
	  </mirror>
  </mirrors>

<profiles>
    <profile>
	<id>nexus</id>
		<repositories>
			<repository>
				<id>my</id>  
					<url>http://localhost:8081/repository/maven-public</url>
				<releases>
					<enabled>true</enabled>
				</releases>
				<snapshots>
					<enabled>true</enabled>
				</snapshots>
			</repository>
		</repositories>
    </profile>
    <!--默认节点,可以不配置,那么就会从maven中央仓库拉取-->
	<profile>
		<id>ali</id>
		<repositories>
			<repository>
				<id>central</id>
				<url>https://maven.aliyun.com/repository/public</url>
				<releases>
					<enabled>true</enabled>
				</releases>
				<snapshots>
					<enabled>false</enabled>
				</snapshots>
			</repository>
		</repositories>
	</profile>
</profiles>

	<activeProfiles>
		<activeProfile>nexus</activeProfile>
		<activeProfile>ali</activeProfile>
	</activeProfiles>
</settings>

可以看出,这种方式多配置了一个阿里云仓库,其实,也可以按照第一种方式的配置,那么这里有几个知识点:

  1. 配置了mirror节点,会从自定义的mirror拉取依赖
  2. 如何判断走哪一个mirror节点,是根据mirrorOf属性判断,mirrorOf所对应的是repository的id,它可以配置:*、central、repositoryId,多个逗号隔开(mirrorOf的详细规则可以网上查一查);
  3. 它也存在默认拉取情况,默认拉取的配置前提是私有仓库镜像mirrorOf不是*,不然所有请求都会走*的节点,那就不存在默认拉取情况了,所以:
    1. mirrorOf节点,配置私有仓库的id,就可以了,会先找私有库,如果私有库找不到,那么会去默认的maven中央仓库找;
    2. 像上面的配置,在配置一个id为central的仓库,表示它是中央仓库,默认的都走这个节点,上面我们配置了阿里云的仓库那么默认就会走这个仓库;

猜你喜欢

转载自blog.csdn.net/qq_28911061/article/details/119766302
今日推荐