在 idea 多项目文件中,当我们之前已经全打包一次后,对一个项目修改了一下像单独打包时会报错,必须得整体再打包一次,如何解决?

控制台会获得如下报错信息:

[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.quanxiaoha:xiaohashu-auth >--------------------
[INFO] Building xiaohashu-auth 0.0.1-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from huaweicloud: https://mirrors.huaweicloud.com/repository/maven/com/quanxiaoha/xiaoha-framework/${revision}/xiaoha-framework-${revision}.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.776 s
[INFO] Finished at: 2024-05-16T16:22:17+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project xiaohashu-auth: Could not resolve dependencies for project com.quanxiaoha:xiaohashu-auth:jar:0.0.1-SNAPSHOT: Failed to collect dependencies at com.quanxiaoha:xiaoha-common:jar:0.0.1-SNAPSHOT: Failed to read artifact descriptor for com.quanxiaoha:xiaoha-common:jar:0.0.1-SNAPSHOT: The following artifacts could not be resolved: com.quanxiaoha:xiaoha-framework:pom:${revision} (absent): Could not transfer artifact com.quanxiaoha:xiaoha-framework:pom:${revision} from/to huaweicloud (https://mirrors.huaweicloud.com/repository/maven/): status code: 400, reason phrase:  (400) -> [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/DependencyResolutionException
 
Process finished with exit code 1

其中:

Could not transfer artifact com.quanxiaoha:xiaoha-framework:pom:${revision} from/to huaweicloud (https://mirrors.huaweicloud.com/repository/maven/)  

提示我们无法从中央仓库下载 com.quanxiaoha:xiaoha-framework:pom:${revision}

版本号并没有被正确解析,原因是单独打包子项目时忽略了 父项目中定义的 revision,因此可以使用 flatten-maven-plugin 来将所有的 pom 文件扁平化管理,这样当打包单个项目时实际上调用了项目所有的pom上下文。

solve

编辑项目最外层的 pom.xml 文件,声明 flatten-maven-plugin 版本号并添加该插件:

首先添加 properties 定义插件版本:

 <properties>
        // 省略...
        <flatten-maven-plugin.version>1.5.0</flatten-maven-plugin.version>
        // 省略...
    </properties>

接着在 build 中的 pluginManagement 之外新生命一个 plugins:(这样就能全局生效且不需要改子项目的pom.xml)

 <build>
        <!-- 统一插件管理 -->
        <pluginManagement>
            <plugins>
                // 省略...
            </plugins>
        </pluginManagement>
 
        <plugins>
            <!-- 统一 revision 版本, 解决子模块打包无法解析 ${revision} 版本号问题 -->
        </plugins>
    </build>

plugins 加入:

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>${flatten-maven-plugin.version}</version>
                <configuration>
                    <flattenMode>resolveCiFriendliesOnly</flattenMode>
                    <updatePomFile>true</updatePomFile>
                </configuration>
                <executions>
                    <execution>
                        <id>flatten</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>flatten</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>flatten.clean</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>