You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

maven-exec-plugin在clean阶段触发依赖检查导致构建失败的问题咨询

maven-exec-plugin在clean阶段触发依赖检查导致构建失败的问题咨询

我遇到一个Maven构建的问题,场景如下:我有一个项目依赖另一个本地项目,计划通过exec-maven-plugin在当前项目的clean阶段清理依赖项目,同时在validate阶段安装该依赖项目。但实际执行时发现,当依赖的项目不在本地Maven仓库时,执行mvn clean会直接因为依赖无法解析而失败,根本跑不到exec插件的clean执行步骤。

以下是我的项目POM配置:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>de.hsc</groupId>
    <artifactId>exectest</artifactId>
    <version>4.0.0</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>21</maven.compiler.release>
        <exec.mainClass>de.hsc.exectest.Exectest</exec.mainClass>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.5.0</version>
                <executions>
                    <execution>
                        <id>clean-other-project</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>mvn.cmd</executable>
                            <workingDirectory>${basedir}/../NonExistentDependency</workingDirectory>
                            <commandlineArgs>clean</commandlineArgs>
                        </configuration>
                    </execution>
                    <execution>
                        <id>install-other-project</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>mvn.cmd</executable>
                            <workingDirectory>${basedir}/../NonExistentDependency</workingDirectory>
                            <commandlineArgs>install</commandlineArgs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>foo.bar</groupId>
            <artifactId>NonExistentDependency</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</project>

当依赖项目不在本地仓库时,执行mvn clean的输出如下:

mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< de.hsc:exectest >---------------------------
[INFO] Building exectest 4.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for foo.bar:NonExistentDependency:jar:1.0.0 is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.247 s
[INFO] Finished at: 2025-06-18T11:24:02+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project exectest: Could not resolve dependencies for project de.hsc:exectest:jar:4.0.0
[ERROR] dependency: foo.bar:NonExistentDependency:jar:1.0.0 (compile)

但当我注释掉id为clean-other-project的execution块后,执行mvn clean就能正常完成:

mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< de.hsc:exectest >---------------------------
[INFO] Building exectest 4.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ exectest ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.352 s
[INFO] Finished at: 2025-06-18T11:25:24+02:00
[INFO] ------------------------------------------------------------------------

更奇怪的是,执行mvn validate也会报同样的依赖解析错误。

我现在的疑问是:为什么仅仅在clean阶段配置了exec插件的执行,Maven会先触发项目的依赖检查?按我的理解,clean阶段不应该检查编译依赖才对。这是Maven的预期行为吗?还是我的配置哪里有问题?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 08:54:31