title: Gradle jacoco code coverage - Then publish/show in Jenkins
body: <p>I'm trying to setup code coverage for a Java application project.</p>
  
  <p>Project name     : NewApp</p>
  
  <p>Project structure: </p>
  
  <ul>
  <li>src/java/** (source code)</li>
  <li>src/java-test (unit tests - Jnuit)</li>
  <li>test/it-test (integration test)</li>
  <li>test/at-tests (acceptance tests)</li>
  <li>tomcat/* (contain tomcat start/stop scripts)</li>
  <li>xx/.. etc folders which are required for a usual application.</li>
  </ul>
  
  <p>Gradle version   : 1.6</p>
  
  <p>Environment      : Linux</p>
  
  <p>I have a running gradle build script that fetches application (NewApp) dependencies (i.e. service jars used by the app for build process) from a build artifact repository (artifactory/maven for ex), and builds the app.</p>
  
  <p>Now at this point, I wanted to get code coverage using JaCoCo plugin for my NewApp application project.</p>
  
  <p>I followed the documentation per Gradle/Jacoco but it doesn't seems to create any reports/... folder for jacoco etc where I can find what Jacoco coverage report did.</p>
  
  <p>My questions:
  1. For getting code coverage using Unit tests (Junit), I assume all I have to do is the following and it will NOT require me to start/stop the tomcat before running unit test (test task i.e. "gradle test") to get code coverage for/via using unit tests. Please advise/correct. The code (just for Gradle jacoco unit test part) - I'm using is:</p>
  
  <pre><code>apply plugin: 'jacoco'
  
  test {
        include 'src/java-test/**'
  }
  
  jacocoTestReport {
      group = "reporting"
      description = "Generate Jacoco coverage reports after running tests."
      reports {
          xml.enabled true
          html.enabled true
          csv.enabled false
      }
      //classDirectories = fileTree(dir: 'build/classes/main', include: 'com/thc/**')
      //sourceDirectories = fileTree(dir: 'scr/java', include: 'com/thc/**')
      additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
  }
  </code></pre>
  
  <p>and for Integration tests:</p>
  
  <pre><code>task integrationTest(type: Test) {
          include 'test/java/**'
  }
  </code></pre>
  
  <p>As jacocoTestReport is depends upon test task(s), thus they will be called first and then finally jacocoTestReport will report what it found for the code coverage.</p>
  
  <ol>
  <li>For getting code coverage for integration tests, I assume I must start tomcat first (i.e. before running / calling test target for integration tests), then call "gradle integrationTest" or "gradle test" task and then stop tomcat -- to get the code coverage report. From other blog posts I also found that one should setup JAVA_OPTS variable to assign jacoco agent before tomcat starts.</li>
  </ol>
  
  <p>for ex: setting JAVA_OPTS variable like:</p>
  
  <pre><code>export JACOCO="-Xms256m -Xmx512m -XX:MaxPermSize=1024m -javaagent:/production/jenkinsAKS/jobs/NewApp/workspace/jacoco-0.6.3.201306030806/lib/jacocoagent.jar=destfile=/production/jenkinsAKS/jobs/NewApp/workspace/jacoco/jacoco.exec,append=true,includes=*"
  export JAVA_OPTS="$JAVA_OPTS $JACOCO"
  </code></pre>
  
  <ol>
  <li><p>Being new to Gradle/groovy - I'm not sure what code should I write within build.gradle (build script) to get the above Integration/Unit tests working if it involves start/stop of tomcat. If someone can provide a sample script to do that, I'll try.</p></li>
  <li><p>I'm not getting any code coverage right now, when I publish Jacoco code coverage in Jenkins (using Jenkins post build action for publishing Jacoco reports). Jenkins build dashboard shows 0% for code coverage (i.e. bars showing all red color, no green for actual code coverage).</p></li>
  </ol>
  
  <p>Need your advice to get some traction on this.</p>