Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 782
liltitus27 Avatar asked Sep 21 '17 15:09

liltitus27


People also ask

How do I run a shell script in maven?

You can place the pom file inside the resource folder after the maven project is created. You can use the following plugin to run the shell script. Assume that your shell script resides in <Project-home>/resource folder is run.sh.

What does Mvn exec exec do?

Maven exec plugin allows us to execute system and Java programs from the maven command. There are two goals of the maven exec plugin: exec:exec - can be used to execute any program in a separate process. exec:java - can be used to run a Java program in the same VM.

What does Mvn exec Java?

mvn exec:java is a goal from the exec plugin for maven. It lets you specify a main class to execute (see pom. xml). This lets you avoid having to figure out the proper java command to run and classpath arguments and the like.

What is the correct syntax for executing a maven plugin?

Usage of a Maven Plugin xml you can use the shorthand notation to execute the plugin: mvn <prefix>:<goal> , commonly the “prefix” is the artifact ID minus the “-maven-plugin”.


1 Answers

I would add the cygwin\bin path to the Windows %PATH% environment variable, since Cygwin provides a bash.exe executable. Then modify the .pom to be platform independent:

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

like image 137
Francois B Avatar answered Sep 18 '22 15:09

Francois B