gradle ant排除多个文件

时间:2018-11-12 09:48:17

标签: gradle ant cobertura

我有一个gradle任务,它使用ant通过Cobertura创建代码覆盖率报告。排除一个文件时,这可以按预期工作:

fileset(dir: classes,
    includes:"**/*.class",
    excludes:"**/*Test.class")

但是当我要排除多个文件时,*Test.class个文件将被排除,而*Jar.class不是。

fileset(dir: classes,
    includes:"**/*.class",
    excludes:["**/*Test.class", "**/*Jar.java"])

我现在正在用一个文件进行测试,但最终目标是排除整个程序包。排除多个文件的正确语法是什么?

整个gradle任务:

logger.info "Configuring Cobertura Plugin"

configurations{
  coberturaRuntime {extendsFrom testRuntime}
}

dependencies {
  coberturaRuntime 'net.sourceforge.cobertura:cobertura:2.1.1'
}

def serFile="${project.buildDir}/cobertura.ser"
def classes="${project.buildDir}/"
def classesCopy="${classes}-copy"


task cobertura(type: Test){
  dependencies {
    testRuntime 'net.sourceforge.cobertura:cobertura:2.1.1'
  }

  systemProperty "net.sourceforge.cobertura.datafile", serFile
}

cobertura.doFirst  {
  logger.quiet "Instrumenting classes for Cobertura"
  ant {
    delete(file:serFile, failonerror:false)
    delete(dir: classesCopy, failonerror:false)
    copy(todir: classesCopy) { fileset(dir: classes) }

    taskdef(resource:'tasks.properties', classpath: configurations.coberturaRuntime.asPath)
    'cobertura-instrument'(datafile: serFile) {
      fileset(dir: classes,
              includes:"**/*.class",
              excludes:["**/*Test.class", "**/*Jar.class"])
    }
  }
}

cobertura.doLast{
  if (new File(classesCopy).exists()) {
    //create html cobertura report
    // ant.'cobertura-report'(destdir:"${project.reportsDir}/cobertura",
    //         format:'html', srcdir:"src/main/java", datafile: serFile)
    //create xml cobertura report
     ant.'cobertura-report'(destdir:"${project.reportsDir}/cobertura",
            format:'xml', srcdir:"src/main/java", datafile: serFile)
    ant.delete(file: classes)
    ant.move(file: classesCopy, tofile: classes)
  }
}

1 个答案:

答案 0 :(得分:0)

答案很简单。除了可以将包含和排除作为文件集的参数,还可以在文件集方法中使用多个包含和排除方法。因此,将我问题中的文件集行替换为:

fileset(dir: classes) {
    include(name: "**/*.class")
    exclude(name: "**/*Test.class")
    exclude(name: "**/*Jar.class")
}

我在gradles文档中找到了有关将嵌套元素传递给Ant任务的信息。
参见:https://docs.gradle.org/current/userguide/ant.html#example_passing_nested_elements_to_an_ant_task