Gradle catch error from file in custom task

496 Views Asked by At

I'm new to Gradle and I don't know much about it, but in my gradle file I defined the following task:

task helloWorld {
  description "Custom task"
  ext.srcFile = file("build/reports/checkstyle/main.xml")
  inputs.file srcFile
  doLast{
    println "Hello World!"
    println project.version
    def errorCheckStyle  = new XmlParser().parse(srcFile)
    errorCheckStyle.row.each { row ->
      println row
    }
    throw new GradleException("Oops, there seems to be an error in the following file:\n" +
            ext.srcFile)
  }
}

I only have this as a base and I would like to know how I can go through this file and catch the first error, after that the exception appears.

1

There are 1 best solutions below

0
On
task helloWorld {
  description "Custom task"
  ext.srcFile = file("build/reports/checkstyle/main.xml")
  inputs.file srcFile
  doLast{
    println "Hello World!"
    println project.version
    try {
        def errorCheckStyle  = new XmlParser().parse(srcFile)
        errorCheckStyle.row.each { row ->
          println row
        }
    } catch (Exception ex) {
        throw new GradleException("Oops, there seems to be an error in the following file:\n" + ext.srcFile)
    }
  }
}