Java exec-maven-plugin 说不能运行指定的程序,即使它在 PATH 上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22393785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:30:08  来源:igfitidea点击:

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

javanode.jsmavenpathmaven-plugin

提问by bguiz

Edit 20140716:

编辑 20140716

Solution found

找到解决方案

tl;dr = exec-maven-plugin does notrecognise .cmdfiles, but only .batfiles, as executable scripts. Rename grunt.cmd --> grunt.bat, bower.cmd --> bower.bat, etc. as a workaround.

tl;dr = exec-maven-plugin不能.cmd文件识别.bat为可执行脚本,而只能将文件识别为可执行脚本。重命名grunt.cmd --> grunt.batbower.cmd --> bower.bat等作为解决方法。



Having done npm install -g grunt-clion my system, gruntis most certainly on the PATH

npm install -g grunt-cli我的系统上完成后,grunt肯定是在PATH

When I run maven installhowever, this doesn't seem to register.

maven install但是,当我运行时,这似乎没有注册。

    [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec 
    (build-spa-bower) on project foobar: Command execution failed. 
    Cannot run program "grunt" (in directory "C:\workspace\foobar\src\main\spa"): 
    CreateProcess error=2, The system cannot find the file specified -> [Help 1]
    org.apache.maven.lifecycle.LifecycleExecutionException: 
    Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec 
    (build-spa-bower) on project foobar: Command execution failed.

Just to be sure, in the same terminal, I have executed this

可以肯定的是,在同一个终端中,我已经执行了这个

cd C:\workspace\foobar\src\main\spa
grunt build

... in the same terminal as I issued the maven command above, and grunt executes just fine.

...在我上面发出 maven 命令的同一个终端中,grunt 执行得很好。

Does exec-maven-pluginuse the PATHenvironment variable, or does it need to be told that this executable exisst in some other way?

是否exec-maven-plugin使用PATH环境变量,或者是否需要告知该可执行文件以其他方式存在?



编辑:

This documentationsuggests that executables on PATHshould be found, so it stumps me further.

这个文档表明PATH应该找到可执行文件,所以它让我更难受。

采纳答案by Piet van Dongen

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

除了 bguiz 的答案(这将是最佳解决方案)之外,我还使用 Maven 配置文件创建了一个解决方法,绕过了该问题。

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

这是一个临时解决方案,直到 maven-exec-plugin 的错误得到修复。

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

请在此处对错误报告进行投票:http: //jira.codehaus.org/browse/MEXEC-118

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

编辑:错误已解决,您可以指向 1.4-SNAPSHOT 进行修复。

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

回答by bguiz

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

我挖掘了源代码exec-maven-plugin并找到了这个片段。

From the source of ExecMojo#getExecutablePath:

从来源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

我将它与另一个从 maven 运行 grunt 任务的插件进行了比较,并发现了这个

        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-plugindid not, because it only did so for file ending in .bat.

......这对我有用。所以基本上后者的工作,因为在Windows中的所有命令都用前缀cmd /c,而exec-maven-plugin没有,因为它只能这样做的文件中结束.bat

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

往里看C:\Users\USER\AppData\Roaming\npm,我看到:

  • node_modules(folder)
  • grunt(unix script file)
  • grunt.cmd(windows script file)
  • node_modules(文件夹)
  • grunt(Unix 脚本文件)
  • grunt.cmd(windows 脚本文件)

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

当我重命名grunt.cmd--> 时grunt.bat,这解决了问题,并且exec-maven-plugin能够运行此命令。

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

(这也适用于使用创建的其他可执行文件npm install -g,例如boweryo

回答by Yves

I had the same issue with 1.5.0 of the plugin.

我对插件的 1.5.0 有同样的问题。

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.

在我的情况下,原因是我的用户名中的空格导致了一个咕噜声:C:\Users\My name with space\AppData\Roaming\npm。

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

当我将 npm 目录的内容移动到没有空格的路径时,它起作用了。