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相關項目源碼的同學參考。