java - 在 Windows 上使用 exec-maven-plugin 执行 shell 脚本

标签 java windows shell maven exec-maven-plugin

我有一个 pom,它使用 exec-maven-plugin 来执行带有三个参数的 shell 脚本。运行 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 应用程序的完全相同的错误消息,并且该脚本不采用任何参数,也不会尝试引用任何参数;它只是回应“hello world”。

我确实注意到各种参数中的斜线混合在一起,但我不确定那是罪魁祸首;这似乎与尝试从 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>

在您的系统上,在您的 PATH 中有一个 sh.bat :

c:\cygwin\bin\run.exe sh %*

*我省略了文件夹以提高可读性

关于java - 在 Windows 上使用 exec-maven-plugin 执行 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46347880/

相关文章:

c - 如果从另一个文件夹调用程序将无法工作

windows - 从带有随机数字分隔符的字符串中获取变量 [cmd]

python - 更改 python 解释器窗口

linux - 收集文件名的 bash 脚本似乎被空格弄糊涂了

java - Java/Eclipse无法播放声音

java - Swing 逐页转换(MultiPages Widget)

java - Bean Machine Galton Box Java 展示

Java volatile 语义,JMM 保证

linux - 从文本文件中删除注释行

php - GET 命令给出两种输出,为什么?