简体   繁体   English

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

[英]Use exec-maven-plugin to execute shell script on Windows

I have a pom that uses the exec-maven-plugin to execute a shell script with three parameters.我有一个使用 exec-maven-plugin 执行带有三个参数的 shell 脚本的 pom。 When running mvn clean install -X -e , it fails at that step with the error,运行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.

Relevant portion of the pom.xml: 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>

I feel like this may be related to Operating Systems, where I'm (the only one) running on Windows 10 x64, and others are running on Macs.我觉得这可能与操作系统有关,我(唯一的)在 Windows 10 x64 上运行,而其他人在 Mac 上运行。 If I run this command in Cygwin, it completes successfully, executing the shell script with the correct parameters.如果我在 Cygwin 中运行此命令,它会成功完成,并使用正确的参数执行 shell 脚本。 Even with cmd.exe, I can execute this script.即使使用 cmd.exe,我也可以执行此脚本。

But building the project using Maven, it fails every time.但是使用 Maven 构建项目,每次都失败。 I even emptied the shell script so it literally was comprised of the following:我什至清空了 shell 脚本,所以它实际上由以下内容组成:

#!/bin/sh
echo "hello world"

While the real, original shell script does take three parameters, I get the exact same error message about %1 not being a valid Win32 application, and this script does not take any arguments, nor does it try to reference any either;虽然真正的原始 shell 脚本确实采用了三个参数,但我收到了关于 %1 不是有效的 Win32 应用程序的完全相同的错误消息,并且该脚本不采用任何参数,也不尝试引用任何参数; it just echoes "hello world."它只是回应“你好世界”。

I did notice that the slashes in the various parameters are mixed, and I'm not sure that's the culprit;我确实注意到各种参数中的斜杠是混合的,我不确定这是罪魁祸首; it seems more to do with attempting to execute a shell script on Windows from Maven.这似乎与尝试从 Maven 在 Windows 上执行 shell 脚本有关。

Can anyone help me with this and explain what's going on?谁能帮我解决这个问题并解释发生了什么? If any additional details are needed, just let me know and I'll provide more context.如果需要任何其他详细信息,请告诉我,我会提供更多背景信息。

Your commandline is*:您的命令行是*:

[dependencies.sh, project-in-question.dependencies, target, third-parameter]

But on Windows the dependencies.sh is not an executable.但是在 Windows 上, dependencies.sh不是可执行文件。 To run it with cygwin you would have to run it like this*:要使用 cygwin 运行它,您必须像这样运行它*:

[c:\cygwin\bin\run.exe, dependencies.sh, project-in-question.dependencies, target, third-parameter]

Now I guess, that the others would not be happy with changing the pom.xml to that.现在我想,其他人不会满意将 pom.xml 更改为那个。


One possible solution should be to install " Windows Subsystem For Linux ".一种可能的解决方案应该是安装“ Windows Subsystem For Linux ”。


Another solution would be to create a dependencies.sh.bat containing something like:另一种解决方案是创建一个包含以下内容的dependencies.sh.bat:

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

but with this solution you probably have to rename the dependencies.sh on your computer so that windows will pick the .bat file first.但是使用此解决方案,您可能必须重命名计算机上的 dependencies.sh,以便 Windows 将首先选择 .bat 文件。


Another compromise might be to change the execution to另一个妥协可能是将执行更改为

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

And on your system have a sh.bat in your PATH with:在你的系统上有一个 sh.bat 在你的PATH中:

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

*I omitted the folders for better readability *我省略了文件夹以提高可读性

Try using cmd as your executable, followed by /C as first argument and pathname of your shell script as next argument, followed by the rest of your arguments:尝试使用cmd作为您的可执行文件,后跟/C作为第一个参数,shell 脚本的路径名作为下一个参数,然后是其余参数:

            <executable>cmd</executable>
            <arguments>
                <argument>/C</argument>
                <argument>${project.basedir}/src/main/scripts/dependencies.sh</argument>

I would add the cygwin\\bin path to the Windows %PATH% environment variable, since Cygwin provides a bash.exe executable.我会将cygwin\\bin路径添加到 Windows %PATH%环境变量中,因为 Cygwin 提供了bash.exe可执行文件。 Then modify the .pom to be platform independent:然后将.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>
           ...

As @Samuel Kirschner pointed out above you can also use Windows Subsystem for Linux (WSL) instead of Cygwin which automatically provides the bash executable on the Windows %PATH% and works with the .pom config given正如@Samuel Kirschner 上面指出的那样,您还可以使用适用于 Linux 的 Windows 子系统(WSL)而不是 Cygwin,它会自动在 Windows %PATH% 上提供bash可执行文件并使用给定的.pom配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM