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.
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.
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.
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.
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”.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With