IDEA中創建父子工程與maven打包Springboot聚合工程報錯程序包不存在問題處理


公司新項目需使用java技術棧,便使用IDEA搭建了一個多SpringBoot項目的聚合工程,因為初次使用,遇到了很多問題,maven打包時各種報錯,在網上查了好多終於解決了,為鞏固記憶,特作此記錄。

 

一、先記錄一下創建父子工程一些需要注意的地方:

 

1.創建父子工程
在IDEA中使用Spring Initializr的方式創建SpringBoot工程,GroupId為域.公司名,例如com.company,Artifact為項目名,例如testproject,主要注意父子項目保持組名一致
父項目創建好后,將.mvm、src文件夾,mvnw、mvnw.cmd文件直接刪除,並修改pom.xml的packaging更改為pom。


2.創建子工程
選中父項目,鼠標右鍵,然后點擊New—>Module,同樣方式創建SpringBoot工程,注意項目的保存路徑要加上'\子項目名',否則項目工程會亂掉


3.修改子項目的pom.xml文件,將其中的parent更改為對應父項目的信息,如下:

 

4.父項目的pom.xml中增加modules節點,並增加新增的子項目

5.添加依賴引用

一般情況下是,在父項目的pom.xml中用dependencyManagement統一版本管理,子項目中根據需要自行聲明引用。
如下:
父項目pom.xml的properties中聲明jar包版本號

<dependencyManagement>
    <dependencies>
        <!-- SpringBoot的依賴配置-->
        <dependency>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.3.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!--阿里數據庫連接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>

       <!--Token生成與解析-->
<dependency>
         <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
         <version>${jwt.version}</version>
</dependency>

<!--驗證碼 -->
<dependency>
        <groupId>com.github.penggle</groupId>
        <artifactId>kaptcha</artifactId>
        <version>${kaptcha.version}</version>
</dependency>

....

        <!-- 通用工具-->
        <dependency>
            <groupId>com.loxaump</groupId>
            <artifactId>loxaump-common</artifactId>
            <version>${loxaump.version}</version>
        </dependency>

    </dependencies>
</dependencyManagement>
dependencyManagement依賴聲明

子項目中聲明所需引用,為防止子項目引用時,因為依賴jar包版本不一致,導致打包時找不到對應jar包錯誤(maven本地中會自動下載有依賴的版本),盡量子項目中不單獨聲明版本號。

<dependencies>
    <!-- Spring框架基本的核心工具 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>

    <!-- SpringWeb模塊 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>

    <!-- spring security 安全認證 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

....
</dependencies>
子項目dependencies

 

二、maven打包時報錯 程序包不存在

父子項目創建好后,使用maven工具

其他的子項目依賴common項目,打包后,明明正常打好了common的jar包,在maven本地倉中也能找到對應的版本,但是在打其他項目時,仍會提示程序包 com.loxaump.common.service.contants不存在等一系列類找不到。
如下圖所示:

 

解決方法:
SpringBoot打成jar包我們一般使用spring-boot-maven-plugin這個插件,當配置了該插件后,運行“mvn package”進行打包時,會打包成一個可以直接運行的 JAR 文件,使用“java -jar”命令就可以直接運行。
1.因為spring-boot-maven-plugin在打成jar包時會默認找public static void main(String[] args){***}方法,這時候如果項目有多個main方法,就有問題了,需要刪掉其他的main方法,只留SpringBoot的入口main方法,或者在pom.xml中用start-class屬性指定項目main方法:

 

2..將父項目ROOT中的spring-boot-maven-plugin更換成org.apache.maven.plugins

更改為:

encoding即為:UTF-8

修改后,在重新打包,問題解決。

 

補充

如果打包時,報錯:Cannot resolve xxx.xxx.xxx-xxx:unknown等錯誤時,應該是本地maven倉庫中存在多個版本的jar包,並且因為其他原因導致某個jar包下載失敗,這時,就到本地倉將jar包的文件夾刪除,重新使用maven打包下載即可。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM