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

lil*_*s27 10 java windows shell maven exec-maven-plugin

我有一个使用 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.
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我觉得这可能与操作系统有关,我(唯一的)在 Windows 10 x64 上运行,而其他人在 Mac 上运行。如果我在 Cygwin 中运行此命令,它会成功完成,并使用正确的参数执行 shell 脚本。即使使用 cmd.exe,我也可以执行此脚本。

但是使用 Maven 构建项目,每次都失败。我什至清空了 shell 脚本,所以它实际上由以下内容组成:

#!/bin/sh
echo "hello world"
Run Code Online (Sandbox Code Playgroud)

虽然真正的原始 shell 脚本确实采用了三个参数,但我收到了关于 %1 不是有效的 Win32 应用程序的完全相同的错误消息,并且该脚本不采用任何参数,也不尝试引用任何参数;它只是回应“你好世界”。

我确实注意到各种参数中的斜杠是混合的,我不确定这是罪魁祸首;这似乎与尝试从 Maven 在 Windows 上执行 shell 脚本有关。

谁能帮我解决这个问题并解释发生了什么?如果需要任何其他详细信息,请告诉我,我会提供更多背景信息。

Sam*_*ner 6

您的命令行是*:

[dependencies.sh, project-in-question.dependencies, target, third-parameter]
Run Code Online (Sandbox Code Playgroud)

但在 Windows 上,dependencies.sh它不是可执行文件。要使用 cygwin 运行它,您必须像这样运行它*:

[c:\cygwin\bin\run.exe, dependencies.sh, project-in-question.dependencies, target, third-parameter]
Run Code Online (Sandbox Code Playgroud)

现在我想,其他人会不满意将 pom.xml 更改为那个。


一种可能的解决方案应该是安装“ Windows Subsystem For Linux ”。


另一种解决方案是创建一个包含以下内容的dependencies.sh.bat:

c:\cygwin\bin\run.exe dependencies.sh %*
Run Code Online (Sandbox Code Playgroud)

但是使用此解决方案,您可能必须重命名计算机上的 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>
Run Code Online (Sandbox Code Playgroud)

在你的系统上有一个 sh.bat PATH

c:\cygwin\bin\run.exe sh %*
Run Code Online (Sandbox Code Playgroud)

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


Jör*_*örg 3

尝试使用cmd作为您的可执行文件,后跟作为/C第一个参数和 shell 脚本的路径名作为下一个参数,然后是其余参数:

            <executable>cmd</executable>
            <arguments>
                <argument>/C</argument>
                <argument>${project.basedir}/src/main/scripts/dependencies.sh</argument>
Run Code Online (Sandbox Code Playgroud)