maven install時報錯 找不到類的情況


1 報錯

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project ly-item-interface: Compilation failure: Compilation failure:
[ERROR] /D:/javacode/idea/leyou/ly-item/ly-item-interface/src/main/java/com/leyou/item/api/GoodsApi.java:[3,28] 程序包com.leyou.common.dto不存在
[ERROR] /D:/javacode/idea/leyou/ly-item/ly-item-interface/src/main/java/com/leyou/item/api/GoodsApi.java:[4,27] 程序包com.leyou.common.vo不存在
[ERROR] /D:/javacode/idea/leyou/ly-item/ly-item-interface/src/main/java/com/leyou/item/api/GoodsApi.java:[23,5] 找不到符號
[ERROR] 符號:   類 PageResult
[ERROR] 位置: 接口 com.leyou.item.api.GoodsApi
[ERROR] /D:/javacode/idea/leyou/ly-item/ly-item-interface/src/main/java/com/leyou/item/api/GoodsApi.java:[37,42] 找不到符號
[ERROR] 符號:   類 CartDTO
[ERROR] 位置: 接口 com.leyou.item.api.GoodsApi
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :ly-item-interface

Process finished with exit code 1

1.1 根本原因(以其中一個error為例分析)

ly-item-interface: Compilation failure: Compilation failure:
[ERROR] com/leyou/item/api/GoodsApi.  程序包com.leyou.common.dto不存在

ly-item-interface在打包時要依賴com.leyou.common包
就很奇怪 明明這個包這個類都是存在的啊,為什么就是找不到???
其次做了幾項檢查:

  • pom.xml依賴是否成功引入或者存在不存在單詞拼寫錯誤(一般情況不會有錯啊)
  • common包中dto是否成功import (這么簡單必然成功import了啊…)

2 解決辦法

最終原因,其實就是沒有依賴。。。原因就是spring-boot-maven-plugin
這個坑!!!用這個插件打包的Jar包可以直接運行,但是不可依賴!!!所以interface自始至終就沒有依賴,自然會說找程序包不存在或者找不到類

最后修改pom.xml的依賴:

 <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <classifier>execute</classifier>
            </configuration>
        </plugin>
    </plugins>
 </build>

 

3 總結

一個微服務通常有兩個子module,一般一個寫實體類和接口一個寫實現方法

建議

  • common類或者實體類或者被被依賴的類,打包插件配置為:
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>none</phase>
                    </execution>
                </executions>
                <configuration>
                    <classifier>execute</classifier>
                </configuration>
            </plugin>
        </plugins>
</build>

注:

<executions>
    <execution>
        <phase>none</phase>
    </execution>
</executions>

是為了解決Unable to find main class的問題,當然,如果寫了簡單的main函數這幾行可以不寫~

  • 其他類或者微服務可以正常配置:
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
</build>

4 最后

在這里插入圖片描述


免責聲明!

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



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