【问题标题】:ant - Running a target at the end of every buildant - 在每次构建结束时运行一个目标
【发布时间】:2011-07-14 15:56:12
【问题描述】:

有没有办法在每次构建的结束处始终运行一个目标?

我知道我可以做这样的事情......

<target name="runJob" depends="actuallyRunJob, teardown"/>

...但这太草率了,因为我需要为每个需要拆解的目标提供一个包装器。

有什么想法吗?

谢谢, 罗伊

【问题讨论】:

    标签: ant


    【解决方案1】:

    使用构建监听器,f.e. exec-listener 为每个构建结果提供一个任务容器
    (构建成功 | 构建失败)您可以将所有需要的任务放入其中
    详情请见Conditional Task on exec failure in Ant
    或滚动您自己的构建侦听器,请参阅 =
    http://ant.apache.org/manual/develop.html
    以及您的 ant 安装中的 api 文档 =
    $ANT_HOME/docs/manual/api/org/apache/tools/ant/BuildListener.html

    【讨论】:

      【解决方案2】:

      本着与 Rebse 的回答相同的精神,您可以使用 Javascript 编写 BuildListener,如下所示(如果您不介意将 Ant 扩展与 makefile 中的某些代码进行交易):

      <project name="test-ant" default="build">                                                           
      
      <target name="init">                                                                            
          <script language="javascript"> <![CDATA[                                                    
              function noop () {}                                                                     
              var listener = new org.apache.tools.ant.BuildListener() {                               
                  buildFinished: function(e) {                                                        
                      project.executeTarget("_finally");                                              
                  },                                                                                  
                  buildStarted: noop,                                                                 
                  messageLogged: noop,                                                                
                  targetStarted: noop,                                                                
                  targetFinished: noop,                                                               
                  taskStarted: noop,                                                                  
                  taskFinished: noop                                                                  
              }                                                                                       
              project.addBuildListener(listener)                                                      
          ]]></script>                                                                                
      </target>                                                                                       
      
      <target name="build" depends="init"/>                                                           
      
      <target name="fail" depends="init">                                                             
          <fail message="expected failure"/>                                                          
      </target>                                                                                       
      
      <target name="_finally">                                                                        
          <echo message="executing _finally target..."/>                                              
      </target>                                                                                       
      
      </project>
      

      如您所见,拦截其他事件应该很容易。

      【讨论】:

      • 如果script在目标内部,则buildStartedbuildFinished事件无法成功触发。可以通过将script直接放在project下面来解决。
      【解决方案3】:

      我也有类似的需求,最终编写了一个自定义任务,如下所示:

      <!-- a task that executes the embedded sequential element at the end of the build,
          but only if the nested condition is true; in this case, we're dumping out the
          properties to a file but only if the target directory exists (otherwise, the clean
          target would also generate the file and we'd never really be clean!) -->
      <build:whendone>
          <condition>
              <available file="${project_target-dir}" />
          </condition>
          <sequential>
              <mkdir dir="${project_final-build-properties-file}/.." />
              <echoproperties destfile="${project_final-build-properties-file}" />
          </sequential>
      </build:whendone>
      

      在这种情况下,我想要记录所有有助于构建的 Ant 属性,但是在构建开始时这样做会遗漏很多(如果它们被初始化为目标的一部分,则在转储它们之后到文件)。

      很遗憾,我不能分享具体的代码,但这只不过是实现了SubBuildListener 的Ant Task,特别是buildFinished(BuildEvent)。侦听器测试嵌入的条件,然后,如果是true,则执行嵌入的sequential

      我很想知道是否有更好的方法。

      【讨论】:

        【解决方案4】:

        如果它是一个 android 项目,您可以轻松地覆盖本地“build.xml”文件中的“-post-build”目标。这个目标就是为了这个目的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-08-27
          • 1970-01-01
          • 1970-01-01
          • 2011-07-29
          • 1970-01-01
          • 2016-04-10
          • 1970-01-01
          • 2010-09-13
          相关资源
          最近更新 更多