exec-maven-plugin says cannot run specified program, even though it is on the PATH

余生长醉 提交于 2019-12-01 03:10:36

In addition to bguiz' answer, which would be the best solution, I've created a workaround using Maven profiles, bypassing the problem.

This is a temporary solution, until the maven-exec-plugin's bug gets fixed.

Please upvote the bug report here: http://jira.codehaus.org/browse/MEXEC-118

Edit: The bug is resolved, you can point to 1.4-SNAPSHOT to fix it.

<project>
(...)
    <profiles>
        <profile>
            <id>grunt-exec-windows</id>
            <activation>
                <os>
                    <family>Windows</family>
                </os>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>${exec-maven-plugin.version}</version>
                        <executions>
                            <execution>
                                <id>grunt-default</id>
                                <phase>generate-resources</phase>
                                <configuration>
                                    <executable>cmd</executable>
                                    <arguments>
                                        <argument>/C</argument>
                                        <argument>grunt</argument>
                                    </arguments>
                                </configuration>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

I dug into the source code of exec-maven-plugin and found this snippet.

From the source of ExecMojo#getExecutablePath:

    CommandLine toRet;
    if ( OS.isFamilyWindows() && exec.toLowerCase( Locale.getDefault() ).endsWith( ".bat" ) )
    {
        toRet = new CommandLine( "cmd" );
        toRet.addArgument( "/c" );
        toRet.addArgument( exec );
    }
    else
    {
        toRet = new CommandLine( exec );
    }

I compared this to another plugin that ran grunt tasks from maven, and found this

        if (isWindows()) {
            command = "cmd /c " + command;
        }

... and that worked for me. So essentially the latter worked because all commands in WIndows were prepended with cmd /c, whereas the exec-maven-plugin did not, because it only did so for file ending in .bat.

Looking in C:\Users\USER\AppData\Roaming\npm, I see:

  • node_modules (folder)
  • grunt (unix script file)
  • grunt.cmd (windows script file)

When I rename grunt.cmd --> grunt.bat, this solves the problem, and exec-maven-plugin is able to run this command.

(this also applies to other executables created using npm install -g, such as bower and yo)

I had the same issue with 1.5.0 of the plugin.

The cause in my case was spaces in my user name resulting in a grunt path: C:\Users\My name with spaces\AppData\Roaming\npm.

When I moved the contents of the npm directory to a path without spaces, it worked.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!