[英]Use exec-maven-plugin to execute shell script on Windows
我有一個使用 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.
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>
我覺得這可能與操作系統有關,我(唯一的)在 Windows 10 x64 上運行,而其他人在 Mac 上運行。 如果我在 Cygwin 中運行此命令,它會成功完成,並使用正確的參數執行 shell 腳本。 即使使用 cmd.exe,我也可以執行此腳本。
但是使用 Maven 構建項目,每次都失敗。 我什至清空了 shell 腳本,所以它實際上由以下內容組成:
#!/bin/sh
echo "hello world"
雖然真正的原始 shell 腳本確實采用了三個參數,但我收到了關於 %1 不是有效的 Win32 應用程序的完全相同的錯誤消息,並且該腳本不采用任何參數,也不嘗試引用任何參數; 它只是回應“你好世界”。
我確實注意到各種參數中的斜杠是混合的,我不確定這是罪魁禍首; 這似乎與嘗試從 Maven 在 Windows 上執行 shell 腳本有關。
誰能幫我解決這個問題並解釋發生了什么? 如果需要任何其他詳細信息,請告訴我,我會提供更多背景信息。
您的命令行是*:
[dependencies.sh, project-in-question.dependencies, target, third-parameter]
但是在 Windows 上, dependencies.sh
不是可執行文件。 要使用 cygwin 運行它,您必須像這樣運行它*:
[c:\cygwin\bin\run.exe, dependencies.sh, project-in-question.dependencies, target, third-parameter]
現在我想,其他人不會滿意將 pom.xml 更改為那個。
一種可能的解決方案應該是安裝“ Windows Subsystem For Linux ”。
另一種解決方案是創建一個包含以下內容的dependencies.sh.bat:
c:\cygwin\bin\run.exe dependencies.sh %*
但是使用此解決方案,您可能必須重命名計算機上的 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>
在你的系統上有一個 sh.bat 在你的PATH
中:
c:\cygwin\bin\run.exe sh %*
*我省略了文件夾以提高可讀性
嘗試使用cmd
作為您的可執行文件,后跟/C
作為第一個參數,shell 腳本的路徑名作為下一個參數,然后是其余參數:
<executable>cmd</executable>
<arguments>
<argument>/C</argument>
<argument>${project.basedir}/src/main/scripts/dependencies.sh</argument>
我會將cygwin\\bin
路徑添加到 Windows %PATH%
環境變量中,因為 Cygwin 提供了bash.exe
可執行文件。 然后將.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>
...
正如@Samuel Kirschner 上面指出的那樣,您還可以使用適用於 Linux 的 Windows 子系統(WSL)而不是 Cygwin,它會自動在 Windows %PATH% 上提供bash
可執行文件並使用給定的.pom配置
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.