Spring相关项目master分支Snapshot下载问题解决

Spring相关项目是一个庞大的家族,除了Spring Framework之外,还有Spring Boot,Spring Cloud等。这些项目和其他开源项目有个明显区别,就是他们之间是有相互依赖的。比如Spring Boot依赖Spring Framework,Spring Cloud又依赖Spring Boot等。

Spring相关项目的主开发分支是master分支。而master分支依赖的其他Spring项目也是snapshot版本的。比如,当我们下载了Spring Cloud Gateway项目之后,它的pom.xml中的parent是如下定义的:

   <parent>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-build</artifactId>
		<version>3.0.0.BUILD-SNAPSHOT</version>
		<relativePath/>
	</parent>

因此,它依赖的其他Spring项目的版本号也是Snapshot的。这体现了Spring项目家族同步开发,同步发布的特点。
而Snapshot在maven的中央仓库是没有的。因为它不稳定,只要重新构建就会去下载最新构建的版本。
所以,Spring在每个项目的pom.xml文件中都配置了repository。比如

        <profile>
			<id>spring</id>
			<repositories>
				<repository>
					<id>spring-snapshots</id>
					<name>Spring Snapshots</name>
					<url>https://repo.spring.io/libs-snapshot-local</url>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
					<releases>
						<enabled>false</enabled>
					</releases>
				</repository>
				<repository>
					<id>spring-milestones</id>
					<name>Spring Milestones</name>
					<url>https://repo.spring.io/libs-milestone-local</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</repository>
				<repository>
					<id>spring-releases</id>
					<name>Spring Releases</name>
					<url>https://repo.spring.io/release</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>spring-snapshots</id>
					<name>Spring Snapshots</name>
					<url>https://repo.spring.io/libs-snapshot-local</url>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
					<releases>
						<enabled>false</enabled>
					</releases>
				</pluginRepository>
				<pluginRepository>
					<id>spring-milestones</id>
					<name>Spring Milestones</name>
					<url>https://repo.spring.io/libs-milestone-local</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</pluginRepository>
				<pluginRepository>
					<id>spring-releases</id>
					<name>Spring Releases</name>
					<url>https://repo.spring.io/libs-release-local</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</pluginRepository>
			</pluginRepositories>
		</profile>

可以看出,Spring有自己的代码仓库。其中https://repo.spring.io/libs-snapshot-local这个仓库就是用于下载Snapshot的依赖的。
但是我们会发现,如果我们什么都不做,Spring还是无法下载Snapshot的依赖。所以项目中到处是红线,到处是找不到的类或者包。
刚开始,我以为是墙的问题。于是我在maven的settings.xml中配置了阿里云的镜像,但发现还是不行。而且我直接访问上面的地址,是能访问的,说明没有被墙。
于是这个问题折磨了我好几天。直到我突然发现Spring每个项目的根目录下都有一个.settings.xml文件。我突然意识到,可能这个就是解决方案。打开这个文件:

<profiles>
	<profile>
	  <id>spring</id>
	  <activation><activeByDefault>true</activeByDefault></activation>
	  <repositories>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/libs-snapshot-local</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone-local</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-releases</id>
			<name>Spring Releases</name>
			<url>https://repo.spring.io/release</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	  </repositories>
	  <pluginRepositories>
		<pluginRepository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/libs-snapshot-local</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</pluginRepository>
		<pluginRepository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone-local</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	  </pluginRepositories>
    </profile>
</profiles>

于是我把这个文件拷贝到某个文件夹下,重命名为settings.xml。并在IDEA中设置maven的配置文件使用这个文件。再重新reimport,发现问题迎刃而解了。
总结发现,其实我在网上搜了很多,但是基本没有说这个问题的。可能对熟悉maven的大神来说,这是个简单的问题吧。像我这种就不行了。于是简单记录一下,供想研究Spring相关项目源码的同学参考。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章