[英]Use exec-maven-plugin to execute shell script on Windows
我有一个使用 exec-maven-plugin 执行带有三个参数的 shell 脚本的 pom。 运行mvn clean install -X -e
,它在该步骤失败并出现错误,
[DEBUG] Toolchains are ignored, 'executable' parameter is set to C:\dev\intellij\projects\project-in-question\driver/src/main/scripts/dependencies.sh
[DEBUG] Executing command line: [C:\dev\intellij\projects\project-in-question\driver\src\main\scripts\dependencies.sh, C:\dev\intellij\projects\project-in-question\driver\target/project-in-question.dependencies, C:\dev\intellij\projects\project-in-question\driver\target, third-parameter]
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (dependencies) on project project-in-question: Command execution failed.: Cannot run program "C:\dev\intellij\projects\project-in-question\driver\src\main\scripts\dependencies.sh" (in directory "C:\dev\intellij\projects\project-in-question\driver"): CreateProcess error=193, %1 is not a valid Win32 application -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (dependencies) on project project-in-question: Command execution failed.
pom.xml 的相关部分:
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
...
</execution>
<execution>
<id>dependencies</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<workingDirectory>${project.basedir}</workingDirectory>
<executable>${project.basedir}/src/main/scripts/dependencies.sh</executable>
<arguments>
<argument>${project.build.directory}/${project.artifactId}.dependencies</argument>
<argument>${project.build.directory}</argument>
<argument>project-in-question</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
我觉得这可能与操作系统有关,我(唯一的)在 Windows 10 x64 上运行,而其他人在 Mac 上运行。 如果我在 Cygwin 中运行此命令,它会成功完成,并使用正确的参数执行 shell 脚本。 即使使用 cmd.exe,我也可以执行此脚本。
但是使用 Maven 构建项目,每次都失败。 我什至清空了 shell 脚本,所以它实际上由以下内容组成:
#!/bin/sh
echo "hello world"
虽然真正的原始 shell 脚本确实采用了三个参数,但我收到了关于 %1 不是有效的 Win32 应用程序的完全相同的错误消息,并且该脚本不采用任何参数,也不尝试引用任何参数; 它只是回应“你好世界”。
我确实注意到各种参数中的斜杠是混合的,我不确定这是罪魁祸首; 这似乎与尝试从 Maven 在 Windows 上执行 shell 脚本有关。
谁能帮我解决这个问题并解释发生了什么? 如果需要任何其他详细信息,请告诉我,我会提供更多背景信息。
您的命令行是*:
[dependencies.sh, project-in-question.dependencies, target, third-parameter]
但是在 Windows 上, dependencies.sh
不是可执行文件。 要使用 cygwin 运行它,您必须像这样运行它*:
[c:\cygwin\bin\run.exe, dependencies.sh, project-in-question.dependencies, target, third-parameter]
现在我想,其他人不会满意将 pom.xml 更改为那个。
一种可能的解决方案应该是安装“ Windows Subsystem For Linux ”。
另一种解决方案是创建一个包含以下内容的dependencies.sh.bat:
c:\cygwin\bin\run.exe dependencies.sh %*
但是使用此解决方案,您可能必须重命名计算机上的 dependencies.sh,以便 Windows 将首先选择 .bat 文件。
另一个妥协可能是将执行更改为
<executable>sh</executable>
<arguments>
<argument>-c</argument>
<argument>${project.basedir}/src/main/scripts/dependencies.sh ${project.build.directory}/${project.artifactId}.dependencies ${project.build.directory} project-in-question</argument>
在你的系统上有一个 sh.bat 在你的PATH
中:
c:\cygwin\bin\run.exe sh %*
*我省略了文件夹以提高可读性
尝试使用cmd
作为您的可执行文件,后跟/C
作为第一个参数,shell 脚本的路径名作为下一个参数,然后是其余参数:
<executable>cmd</executable>
<arguments>
<argument>/C</argument>
<argument>${project.basedir}/src/main/scripts/dependencies.sh</argument>
我会将cygwin\\bin
路径添加到 Windows %PATH%
环境变量中,因为 Cygwin 提供了bash.exe
可执行文件。 然后将.pom修改为与平台无关:
...
<execution>
<id>dependencies</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<workingDirectory>${project.basedir}</workingDirectory>
<executable>bash</executable>
<arguments>
<argument>${project.basedir}/src/main/scripts/dependencies.sh</argument>
<argument>${project.build.directory}/${project.artifactId}.dependencies</argument>
<argument>${project.build.directory}</argument>
<argument>project-in-question</argument>
</arguments>
</configuration>
</execution>
...
正如@Samuel Kirschner 上面指出的那样,您还可以使用适用于 Linux 的 Windows 子系统(WSL)而不是 Cygwin,它会自动在 Windows %PATH% 上提供bash
可执行文件并使用给定的.pom配置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.