徹底解決Failed to execute goal on project xxxxx

1、錯誤內容:Could not resolve dependencies for project

今天在使用mvn clean package命令對一個子項目打包的時候出現如下錯誤(但是使用maven插件卻沒有問題)

Failed to execute goal on project xxxx: Could not resolve
dependencies for project xxxx:jar:0.0.1-SNAPSHOT:
Could not find artifact 公共包:jar:0.0.1-SNAPSHOT

在網上找了很多資料都沒有找到相應的解決方法,有的說不要在公共模組添加maven插件,有的說在公共模組添加

<skip>true</skip>

但是都無濟於事。

2、解決方案

通過控制台給出的建議

[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] //cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException

看到這裡頭都大了,然後我自己思考了一下,為什麼我引用的中心倉庫的依賴打包時沒有問題,但是引用我本地倉庫的依賴(公共包)卻有問題(提前需要對公共包進行mvn clean install,保證本地倉庫有公共jar包)。然後就想到當時配置maven的時候配置了阿里雲的遠程倉庫,如果我配置一下本地的倉庫是不是也可以?因此,加入以下配置就解決了無法處理本地依賴包的問題

<localRepository>D:\maven\apache-maven-3.8.4\repository</localRepository>

添加的位置在mirrors標籤前面

3、解決原理

我認為在沒有配置本地倉庫的情況下,使用命令進行打包的時候先找配置的倉庫,找不到的話再尋找maven中心倉庫,再找不到就報錯。配置過本地倉庫的情況下,就可以直接在本地倉庫查找,因此就可以找到本地的公共包。

4、疑問

由於我需要使用函數計算對項目進行部署,想在部署前運行mvn clean package命令,結果出現了報錯,但是使用maven插件如下進行打包卻沒有問題,希望大佬進行指點。

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.6.4</version>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
Tags: