在springboot開發中如果項目比較復雜,我們會想到把項目拆解成多個模塊,也就是形成一個多層級模塊的maven項目。例如現在我們的項目組成是一個父模塊litchi,兩個子模塊litchi-common和litchi-admin。其中litchi-common模塊是一個公共模塊,里面定義了一些公共的方法操作,litchi-admin模塊是一個應用模塊,litchi-admin中定義了對litchi-common的依賴。整個項目非常清晰,構建也很簡單,一切都很順利,在eclipse中也可以正常運行。
litchi的pom
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> <relativePath /> </parent> <groupId>com.color</groupId> <artifactId>litchi</artifactId> <version>0.0.1</version> <packaging>pom</packaging> <name>litchi</name> <description>project for Spring Boot</description> <modules> <module>litchi-common</module> <module>litchi-admin</module> </modules>
litchi-common的pom
<parent> <groupId>com.color</groupId> <artifactId>litchi</artifactId> <version>0.0.1</version> </parent> <groupId>com.color.litchi</groupId> <artifactId>litchi-common</artifactId> <version>1.0.0</version> <name>litchi-common</name> <packaging>jar</packaging>
litchi-admin的pom
<parent> <groupId>com.color</groupId> <artifactId>litchi</artifactId> <version>0.0.1</version> </parent> <packaging>jar</packaging> <groupId>com.color.litchi</groupId> <artifactId>litchi-admin</artifactId> <name>litchi-admin</name> <version>1.0.0</version> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.color.litchi</groupId> <artifactId>litchi-common</artifactId> <version>1.0.0</version> </dependency> </dependencies>
現在需要對litchi-admin進行打包
1、由於litchi-admin對litchi-common的依賴,所以先把litchi-common安裝到本地倉庫,執行maven命令install,安裝成功,本地倉庫目錄已經出現對應的jar包了
這里需要注意一點,litchi-common的pom中不要定義任何插件,刪除build標簽,程序會自動調用默認的插件maven-compiler-plugin進行打包
2、對litchi-admin執行package命令,這個時候錯誤來了,報錯信息如下:
Could not resolve dependencies for project com.color.litchi:litchi-admin:jar:1.0.0: Failed to collect dependencies at com.color.litchi:litchi-common:jar:1.0.0: Failed to read artifact descriptor for com.color.litchi:litchi-common:jar:1.0.0: Failure to find com.color:litchi:pom:0.0.1 in http://maven.aliyun.com/nexus/content/groups/public/ was cached in the local repository, resolution will not be reattempted until the update interval of Sinotrans-Repositories has elapsed or updates are forced
報錯的原因看起來很清晰,litchi-common加載不成功。明明上一步已經安裝到本地倉庫了呀,為什么會出現這個問題呢?查了一下資料,各種大神紛紛登場,有說刪除litchi-common本地倉庫中_remote.repositories文件的,有說使用各種各種的插件的,各種辦法都嘗試了仍然無法解決。
自己靜下來仔細想了一下流程:按理說maven在打包的時候默認會優先從本地倉庫查找相應的jar包然后才會去遠程倉庫查找下載或者更新。從報錯來看,打包的時候直接就從遠程倉庫開始下載,完全無視本地的存在。在確信本地jar沒有問題的情況下,看來需要強行讓程序從本地加載這個jar包,好了直接上解決方案,修改litchi-admin的pom
1、依賴中增加scope、systemPath標簽指定jar的本地路徑
2、build標簽下增加resources把本地jar打包到生成的jar中
<dependencies> <dependency> <groupId>com.color.litchi</groupId> <artifactId>litchi-common</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>D:\softWare\repository\com\color\litchi\litchi-common\1.0.0\litchi-common-1.0.0.jar</systemPath> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <directory>D:\softWare\repository\com\color\litchi\litchi-common\1.0.0\</directory> <targetPath>BOOT-INF/lib/</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </resources> </build>
后記:maven為什么不能直接從本地倉庫加載這個jar包的原因仍然不清楚,希望有大神留言說明!