exec-maven-plugin说无法运行指定的程序,即使它在PATH上

编辑20140716

find解决scheme

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


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

当我运行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. 

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

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

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

exec-maven-plugin是否使用PATH环境variables,还是需要告诉这个可执行文件以其他方式存在?


编辑:

这个文档build议PATH上的可执行文件应该被find,所以它会让我更加困惑。

除了bguiz的答案,这将是最好的解决scheme,我已经创build了一个使用Mavenconfiguration文件的解决方法,绕过了这个问题。

这是一个临时的解决scheme,直到maven-exec-plugin的bug得到修复。

请在这里上传错误报告: http : //jira.codehaus.org/browse/MEXEC-118

编辑:错误已解决,您可以指向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> 

我深入了exec-maven-plugin的源代码,并find了这个片段。

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 ); } 

我把这个与另外一个插件从maven中跑出来, 发现了这个

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

…而且为我工作。 所以基本上后者的工作原理是因为WIndows中的所有命令都以cmd /c作为前缀,而exec-maven-plugin没有,因为它只以.bat结尾。

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

  • node_modules (文件夹)
  • grunt (unix脚本文件)
  • grunt.cmd (windows脚本文件)

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

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