首页 文章

Ant脚本中的Groovy任务来自定义Cobertura?

提问于
浏览
2

我正在尝试自定义Cobertura的代码覆盖行为 . 默认情况下,Cobertura会对构建中的所有类进行检测,但我想读取一个特定的xml,通常看起来像:

<include>
    ....
    <targetclass name = "com.example.ExMain">
        <method name = "helloWorld" returnType="String">
    </target> 
    ....
</include>

我想阅读从外部源提供的这样一个xml,并自定义Cobertura只检测上面xml中指定的类 . 为此,我编写了一个groovy脚本,现在我需要将groovy脚本挂钩到Cobertura的ant构建脚本..

这是 Ant 部分的一部分,Cobertura实际上在这个部分 .

...
<cobertura-instrument todir="${instrumented.dir}">
  <ignore regex="org.apache.log4j.*" />
  <fileset dir="${classes.dir}">
   <exclude name="**/*.class" />//Custom change                                 
  </fileset>            
</cobertura-instrument>
...

请注意,在上一节中,我明确地排除了Cobertura的工具,以便能够挂钩我的脚本 .

显然文件集不允许我在其中包含一个groovy任务来调用我的自定义脚本来读取xml ..如果我将groovy任务放在外面,不知何故报告不会生成..所以我猜没有其他选项除了调用文件集中的groovy脚本以包含xml中提到的自定义类 . 如何做到这一点?

1 回答

  • 0

    您应该能够在单独的Groovy块中设置一个或多个属性,并在您的cobertura配置中引用它们 . 此简化示例显示了如何从Groovy代码段设置Ant属性 .

    <project name="MyProject" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
    
    <path id="groovyPath">
        <pathelement location="lib/groovy-all-1.8.6.jar"/>
    </path>
    
    <taskdef name="groovy"
             classname="org.codehaus.groovy.ant.Groovy"
             classpathref="groovyPath"/>
    <target name="loadXml">
        <groovy>
            properties.parsedXml = 'some pattern that can be used to configure a task'
        </groovy>
    </target>
    
    <target name="configureTask" depends="loadXml">
        <echo message="${parsedXml}"/>
    </target>
    

相关问题