我正在使用Maven 3.1.1和exec-maven-plugin(1.3)来在构建作业期间执行bash脚本.

bash脚本使用echo和printf在stdout上生成输出.我注意到脚本的输出不会立即写入maven控制台输出.相反,maven控制台输出“冻结”,直到它立即使用bash脚本的多个输出行进行更新.我不知道更新maven输出的触发器是什么(超时?完整输出缓冲区?),但它非常慢.

我们来看一个非常简单的bash脚本,例如counter.sh:

#!/usr/bin/env bash
for i in `seq 1 1000`; do
  echo $i
  sleep 0.5
done 

这是我在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>

当我使用mvn clean包执行构建作业时,maven输出在exec-maven-plugin处冻结,并且在脚本在~8分钟后完成之前不显示进度/输出.

当我执行另一个运行时间更长的脚本时,我每隔15分钟就得到一个输出块.

我正在寻找的是一种在maven控制台输出中即时查看bash脚本输出的方法.

更新:使用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>

解决方法:

exec-maven-plugin使用不会自动刷新的输出流.

我想你有两个选择:

复制并更改exec-maven-plugin以满足您的需求.
使用带有蚂蚁执行任务的antrun插件.这会刷新输出流,以便在输出时看到输出.见http://sanchitbahal.wordpress.com/2011/09/19/maven-exec-plugin-vs-maven-antrun-plugin-for-running-command-line-tool/

也许第二个选项可能会变慢,因为maven会调用ant,然后ant会调用你的脚本,但这很简单.

标签: shell, bash, linux, maven, exec-maven-plugin

相关文章推荐

添加新评论,含*的栏目为必填