bash 如何即时显示 exec-maven-plugin 的输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23648304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display the output of the exec-maven-plugin instantaneously
提问by beaker
I'm using Maven 3.1.1 and the exec-maven-plugin
(1.3) in order to execute a bash script during a build job.
我正在使用 Maven 3.1.1 和exec-maven-plugin
(1.3) 以便在构建作业期间执行 bash 脚本。
The bash script produces output on stdout
with echo
and printf
. I've noticed that the output of the script is not written to the maven console output instantaneously. Instead the maven console output "freezes" until it gets updated with multiple output lines of the bash script at once. I don't know what's the trigger for an update of the maven output (timeout? full output buffer?) but it's very slow.
bash 脚本stdout
使用echo
和生成输出printf
。我注意到脚本的输出不会立即写入 Maven 控制台输出。相反,maven 控制台输出“冻结”,直到它一次被 bash 脚本的多个输出行更新。我不知道更新 Maven 输出的触发器是什么(超时?完整的输出缓冲区?)但它非常慢。
Let's take a very simple bash script, e.g. counter.sh
:
让我们来看一个非常简单的 bash 脚本,例如counter.sh
:
#!/usr/bin/env bash
for i in `seq 1 1000`; do
echo $i
sleep 0.5
done
And here's my plugin configuration in the pom.xml
:
这是我的插件配置pom.xml
:
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<version>1.3</version>
<executions>
<execution>
<id>execute-script</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${project.build.directory}/executable/counter.sh</executable>
</configuration>
</execution>
</executions>
</plugin>
When I execute the build job with mvn clean package
, the maven output freezes at the exec-maven-plugin
and shows no progress/output until the script has completed after ~8 minutes.
当我使用 执行构建作业时mvn clean package
,maven 输出冻结在exec-maven-plugin
并且不显示任何进度/输出,直到脚本在约 8 分钟后完成。
When I execute another script that is running even longer, I get a block of output each ~15 minutes.
当我执行另一个运行时间更长的脚本时,每约 15 分钟我都会得到一个输出块。
What I'm looking for is a way to see the output of the bash script instantaneously in the maven console output.
我正在寻找的是一种在 Maven 控制台输出中即时查看 bash 脚本输出的方法。
Update: Solution using maven-antrun-plugin(thanks to Ivan)
更新:使用 maven-antrun-plugin 的解决方案(感谢 Ivan)
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>execute-script</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<exec dir="${project.basedir}" executable="${project.build.directory}/executable/counter.sh" />
</target>
</configuration>
</execution>
</executions>
</plugin>
采纳答案by Ivan
The exec-maven-plugin uses an output stream that does not flush automatically.
exec-maven-plugin 使用不会自动刷新的输出流。
I think you have two options:
我认为你有两个选择:
- Copy and change the exec-maven-plugin to suit your needs.
- Use antrun plugin with ants exec task. This does flush the output stream so you can see the output when it comes. See http://sanchitbahal.wordpress.com/2011/09/19/maven-exec-plugin-vs-maven-antrun-plugin-for-running-command-line-tool/
- 复制并更改 exec-maven-plugin 以满足您的需要。
- 将 antrun 插件与 ants exec 任务一起使用。这确实会刷新输出流,因此您可以在输出时看到它。见http://sanchitbahal.wordpress.com/2011/09/19/maven-exec-plugin-vs-maven-antrun-plugin-for-running-command-line-tool/
Maybe the 2nd option could be slower, since maven calls ant, and ant then calls your script, but is pretty easy.
也许第二个选项可能会更慢,因为 maven 调用 ant,然后 ant 调用您的脚本,但很容易。
回答by dimas
JFYI, the issue is fixed in exec-maven-plugin v 1.3.2 - http://blog.soebes.de/blog/2014/07/28/mojo-exec-maven-plugin-version-1-dot-3-2-released/
JFYI,该问题已在 exec-maven-plugin v 1.3.2 中修复 - http://blog.soebes.de/blog/2014/07/28/mojo-exec-maven-plugin-version-1-dot-3 -2-发布/
回答by denahiro
As Ivan already pointed out exec-maven-plugin:1.3 doesn't automatically flush the output stream. This can lead to delayed output.
正如 Ivan 已经指出的 exec-maven-plugin:1.3 不会自动刷新输出流。这会导致延迟输出。
I observed that this behaviour is only present in the plugin from version 1.3 onward.
我观察到此行为仅存在于 1.3 版以后的插件中。
exec-maven-plugin:1.2.1 actually has the desired behaviour and flushes the output stream. So instead of using ant you could switch to an older version of exec-maven-plugin.
exec-maven-plugin:1.2.1 实际上具有所需的行为并刷新输出流。因此,您可以切换到旧版本的 exec-maven-plugin,而不是使用 ant。