apply plugin: 'war'
apply plugin: 'org.akhikhl.gretty'
apply plugin: 'org.akhikhl.gretty.internal.integrationTests.IntegrationTestPlugin'
apply plugin: 'jacoco'

import org.akhikhl.gretty.*

dependencies {
  compile 'org.webjars:bootstrap:3.2.0'
  compile 'org.webjars:jquery:2.1.1'
  // We use Velocity for example of template processing within the webapp.
  compile 'org.apache.velocity:velocity:1.7'
}

jacoco {
  toolVersion = '+'
}

task('myIntegrationTest', type: Test, dependsOn: 'test') {

  finalizedBy { tasks.integrationTestClientReport }

  outputs.upToDateWhen { false }
  include '**/*IT.*'
  doFirst {
    systemProperty 'geb.build.reportsDir', reporting.file('geb')
  }

  jacoco {
    append = false
    destinationFile = new File(project.buildDir, 'jacoco/integrationTest_client.exec')
    // excludes.add 'hellogretty.RequestResponseIT'
  }
}

task('myBeforeIntegrationTest', type: JettyBeforeIntegrationTestTask) {

  integrationTestTask 'myIntegrationTest'

  jacoco {
    append = false
    destinationFile = new File(project.buildDir, 'jacoco/integrationTest_server.exec')
  }
}

task('myAfterIntegrationTest', type: JettyAfterIntegrationTestTask) {

  finalizedBy { tasks.integrationTestServerReport }

  integrationTestTask 'myIntegrationTest'
}

task('integrationTestClientReport', type: JacocoReport) {

  executionData project.tasks.myIntegrationTest

  sourceDirectories = project.files(project.sourceSets.test.allSource.srcDirs)
  classDirectories = project.sourceSets.test.output

  def reportDir = project.reporting.file("jacoco/integrationTest_client/html")
  reports {
    html.destination = reportDir
  }
  doLast {
    System.out.println "Jacoco report for client created: file://${reportDir.toURI().path}"
  }
}

task('integrationTestServerReport', type: JacocoReport) {

  executionData tasks.myBeforeIntegrationTest

  sourceDirectories = project.files(project.sourceSets.main.allSource.srcDirs)
  classDirectories = project.sourceSets.main.output

  def reportDir = project.reporting.file("jacoco/integrationTest_server/html")
  reports {
    html.destination = reportDir
  }
  doLast {
    System.out.println "Jacoco report for server created: file://${reportDir.toURI().path}"
  }
}

testAll.dependsOn project.tasks.myIntegrationTest
