我需要运行一个包含'<'的命令在它。
我可以从命令行运行它,但是当我把它放到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>
我试图改变'&lt;'到'&amp; lt;',将commandlineArgs放入CDATA,将doubleqoutes(“)放在任何地方,但无法使其工作。
[DEBUG] Executing command line: [c:\apps\putty\plink.exe, > myuser@myhost -T -ssh -2 -pw tomcat $SHELL /dev/stdin 'a b c d' < 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' < test.sh]
bash: test.sh: No such file or directory [INFO]
------------------------------------------------------------------------
[INFO] BUILD FAILURE
我怀疑'&lt;'参数,但我不确定什么是真正的问题。
任何提示?
更新:当我说“我试图改变'&lt;'时到'&amp; lt;',将commandlineArgs放入CDATA,把doubleqoutes(“)放到任何地方,但无法使其工作。” - 我的意思是!
答案 0 :(得分: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%
不好,但神奇的是: - )
答案 1 :(得分:0)
系统命令和shell命令之间存在差异。管道和流重定向是shell语法。
系统命令通常仅启动具有给定参数的程序,例如
notepad.exe myfile.txt
java -jar my-program.jar
系统命令可以传递给各种API的一些exec
函数的简单调用(Java的java.lang.Runtime.getRuntime().exec()
,PHP的反引号等)。
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,例如Groovy的。
希望有所帮助。
答案 2 :(得分:0)
您特意要求mvn exec:exec
。但是假设您需要使用重定向来运行命令,另一种方法是使用可以自己处理流的插件,例如Maven Ant插件。请注意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>