Use exec-maven-plugin to execute shell script on Windows

liltitus27 picture liltitus27 · Sep 21, 2017 · Viewed 9.9k times · Source

I have a pom that uses the exec-maven-plugin to execute a shell script with three parameters. When running mvn clean install -X -e, it fails at that step with the error,

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

        ...
        <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. If I run this command in Cygwin, it completes successfully, executing the shell script with the correct parameters. Even with cmd.exe, I can execute this script.

But building the project using Maven, it fails every time. I even emptied the shell script so it literally was comprised of the following:

#!/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; 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.

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.

Answer

Samuel Kirschner picture Samuel Kirschner · Sep 25, 2017

Your commandline is*:

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

But on Windows the dependencies.sh is not an executable. To run it with cygwin you would have to run it like this*:

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


One possible solution should be to install "Windows Subsystem For Linux".


Another solution would be to create a dependencies.sh.bat containing something like:

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.


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:

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

*I omitted the folders for better readability