How can I ensure all output from Ant's exec ta

2020-02-04 11:04发布

The Ant exec task has an output property which can be used to tell Ant where the output goes. I've used it to redirect the output to a file. The thing is, if I don't do something with the output, the stuff that Ant prints isn't that much of a help - it's not complete.

Is there someway of setting the output property to System.out?

标签: ant exec
7条回答
手持菜刀,她持情操
2楼-- · 2020-02-04 11:21

When executing a batch file with ant's apply or exec tasks on Windows, I found there are special cases where some of the stdout and stderr is not captured by ant. (For example: if you call a batch file that in turn calls other commands (like node.exe), then the stdout and stderror from the child node.exe process is lost.)

I spent a long time trying to debug this! It seems that the batch file's stdout and stderr is captured, however commands called by the batch file are somehow not seen by ant. (perhaps because they are separate child processes). Using the output and error attributes as suggested above doesn't help because only some of the stdout and/or stderr is captured.

The solution I came up with (a hack) is to add these arguments at the end of the command:

<!--Next arg: forces node's stderror and stdout to a temporary file-->
<arg line=" &gt; _tempfile.out 2&lt;&amp;1"/>

<!--Next arg: If command exits with an error, then output the temporary file to stdout, -->
<!--delete the temporary file and finally exit with error level 1 so that    -->
<!--the apply task can catch the error if @failonerror="true"                -->
<arg line=" || (type _tempfile.out &amp; del _tempfile.out &amp; exit /b 1)"/>

<!--Next arg: Otherwise, just type the temporary file and delete it-->
<arg line=" &amp; type _tempfile.out &amp; del _tempfile.out &amp;"/>

Because this hack only applies to windows, remember to add @osfamily="windows" to the apply or exec task. And create similar task(s) for `@osfamily="unix", etc but without these extra arguments.

查看更多
够拽才男人
3楼-- · 2020-02-04 11:28

If you want to output to System.out, then simply do not specify the "output" attribute. If you would like to redirect to a file AND print it to System.out, you can use the tee command, which will redirect output to a given file and also echo it to standard out... I do not know if Windows supports "tee" or an equivalent.

查看更多
闹够了就滚
4楼-- · 2020-02-04 11:28

Maybe you want to look at the error, logError, and errorproperty attributes of the exec task too. These deal with the handling of the standard error stream from the exec'd process. There may be useful information there that is going awol for some reason - which might account for the incompleteness you see.

But, if the exec'd process decides to close stdout or stderr and send them elsewhere - there's little you can do.

查看更多
我命由我不由天
5楼-- · 2020-02-04 11:31

The output of exec does go to standard out unless you specify the output attribute.

查看更多
The star\"
6楼-- · 2020-02-04 11:43

I have faced similar problem: the output of command execution was suppressed. Perhaps that is the side effect when running cmd under WinXP (I an using maven-antrun-plugin). Anyway setting output="con" worked out perfectly:

<configuration>
    <target>
        <exec executable="cmd" output="con">
            <arg value="/c" />
            <arg value="..." />
        </exec>
    </target>
</configuration>
查看更多
祖国的老花朵
7楼-- · 2020-02-04 11:44

I was experiencing this same kind of issue trying to get the build process to fail in Ant after Karma tests intentionally failed, and executing them with "grunt test".

Just added /c before "grunt test", and it worked like a charm

<target name="unittest">
    <echo>*** KARMA UNIT TESTING ***</echo>
    <exec dir="api_ui" executable="cmd" osfamily="windows" logError="yes" failonerror="true">
        <arg value="/c grunt test"/>
    </exec>
</target>
查看更多
登录 后发表回答