【问题标题】:mvn exec:exec with '<' in commandlineArgsmvn exec:exec with '<' in commandlineArgs
【发布时间】:2017-04-10 08:48:47
【问题描述】:

我需要运行一个包含“

我可以从命令行运行它,但是当我将它放入 mvn exec 时会引发错误。

命令:

c:\apps\putty\plink.exe myuser@myhost -T -ssh -2 $SHELL /dev/stdin 'a b c d' < test.sh

test.sh:

#!/bin/bash
echo "execution parameters: $@"

命令行输出:

执行参数:a b c d

pom.xml:

<plugin> 
                <groupId>org.codehaus.mojo</groupId> 
                <artifactId>exec-maven-plugin</artifactId> 
                <version>1.4.0</version> 
                <executions> 
                    <execution>
                        <id>test</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration><executable>c:\apps\putty\plink.exe</executable>
                            <commandlineArgs>"myuser@myhost -T -ssh -2 $SHELL /dev/stdin 'a b c d' < test.sh"</commandlineArgs>
                        </configuration>
                    </execution>
            </executions> 
        </plugin> 

我尝试将 '

[DEBUG] Executing command line: [c:\apps\putty\plink.exe, > myuser@myhost -T -ssh -2 -pw tomcat  $SHELL /dev/stdin 'a b c d' &lt; test.sh] 
Unable to open connection: Host does not exist[INFO]
------------------------------------------------------------------------ 
[INFO] BUILD FAILURE

或:

[DEBUG] Executing command line: [c:\apps\putty\plink.exe, myuser@myhost, -T, -ssh, -2, -pw, tomcat, $SHELL /dev /stdin 'a b c d' &lt; test.sh] 
bash: test.sh: No such file or directory [INFO]
------------------------------------------------------------------------ 
[INFO] BUILD FAILURE

我怀疑 '

有什么建议吗?

更新:当我说“我尝试将 '” -我是认真的!

【问题讨论】:

  • 试过了:
  • 也试过了:
  • 也试过了:
  • 只是为了和你在同一个页面上:你试图在远程机器上执行一个命令,然后将它的结果用作像 Maven 的 args 这样的命令?由此:&lt;commandlineArgs&gt;"myuser@myhost -T -ssh -2 $SHELL /dev/stdin 'a b c d' &lt; test.sh"&lt;/commandlineArgs&gt; Maven 以某种方式得出结论,myhost -T -ssh -2 $SHELL /dev/stdin 'a b c d' &lt; test.sh 是主机。
  • 问题不在于 plink 也不在于 bash,因为上面的命令行正在运行。问题是我应该如何将它传递给 mvn exec:exec 以使其运行相同:-)

标签: maven ssh putty plink


【解决方案1】:

如果我将它包装在 .bat 文件中,它就可以工作:

@echo off
set plinkExec=%1
set env=%2
set user=%3
set pass=%4
set shellPath=%5
...

%plinkExec% %user%@%env% -T -ssh -2 -pw %pass% $SHELL /dev/stdin '...' < %shellPath%

不好,但有魔力:-)

【讨论】:

    【解决方案2】:

    系统命令和shell命令是有区别的。管道和流重定向是 shell 语法。

    系统命令通常只启动具有给定参数的程序,例如

    • notepad.exe myfile.txt
    • java -jar my-program.jar

    系统命令可以传递给各种 API(Java 的 java.lang.Runtime.getRuntime().exec()、PHP 的反引号等)的一些 exec 函数的简单调用。

    然后,Shell 命令是 shell 处理它的特定语法,Maven 不知道您使用什么 shell。

    通常,shell 提供了一种方法来执行其命令作为其可执行文件的参数。所以如果你想使用 shell 命令,你需要传递它,例如像这样:

                <configuration>
                    <executable>bash</executable>
                    <arguments>
                        <argument>-c</argument>
                        <argument>java -jar myprogram.jar < input.txt > output.txt</argument>
                    </arguments>
                </configuration>
    

    对于 Windows 来说

                <configuration>
                    <executable>path/to/cmd.exe</executable>
                    <arguments>
                        <argument>/C</argument>
                        <argument>java -jar myprogram.jar < input.txt > output.txt</argument>
                    </arguments>
                </configuration>
    

    您可以使用一些cross-platform shell,例如时髦的。

    希望对您有所帮助。

    【讨论】:

    • 您错过了插件的工件部分。这是使用exec-maven-plugin 的工作示例吗?关于 groovy 提示:我已经发现:如果你需要用 maven 做一些复杂的事情,那么:a)你使用了错误的工具 b)你可以用 groovy-maven-plugin 来做 ;-)
    • 是的,这是针对标准 exec 插件的。我没有尝试过这个确切的代码,但我记得我在 linux 和 windows 上都使用了这个。实际上它变成了:bash "-c" "java ..."
    【解决方案3】:

    您特别要求mvn exec:exec。但是假设您需要运行带有重定向的命令,另一种方法是使用可以自己处理流的插件,例如Maven 蚂蚁插件。注意input="..."

            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals> <goal>run</goal> </goals>
                        <configuration>
                            <target name="run">
                                <exec dir="${work.dir}" executable="java" input="input.txt">
                                    <arg value="-jar"/>
                                    <arg file="${project.build.directory}/${project.build.finalName}.jar"/>
                                </exec>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>            
    

    【讨论】:

    • Ok :) 更新 - 添加了必要的阶段,以及如何指向构建生成的 jar 的示例。从布尔诺到布达佩斯的问候。
    猜你喜欢
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多