javamavenmaven-pluginmaven-exec-plugin

maven-exec-plugin causes dependency check in clean phase


I have a project with a dependency which is installed and cleaned by the maven-exec-plugin.

<?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>

When "NonExistentDependency" is not in the local repository and I hit "mvn clean" it should clean up my project and NonExistentDependency by the exec-plugin but I receive this:

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)

When I comment out the execution block with id "clean-other-project" and hit mvn clean again, everything is working as expected (except that the other project is not cleaned up):

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] ------------------------------------------------------------------------

So somehow for no reason maven-exec-plugin seems to check the dependencies of the project for no reason.

"mvn validate" is also aborting with the same error.

Should this be the expected behaviour or do I miss something?


Solution

  • Some plugins require dependency resolution before they start. Maven-Exec-Plugin as well:

    @Mojo(name = "exec", threadSafe = true, requiresDependencyResolution = ResolutionScope.TEST)
    public class ExecMojo extends AbstractExecMojo {
    

    See https://github.com/mojohaus/exec-maven-plugin/blob/master/src/main/java/org/codehaus/mojo/exec/ExecMojo.java

    Generally, you don't write POMs like that. You either put both projects together into a multi-module-project or you do complicated builds by using a build server like GitHub or GitLab.