Gradle PMD Checkstyle Findbugs - Which sourceSet directory to work Java build

AKS picture AKS · Aug 21, 2013 · Viewed 9.2k times · Source

Gradle 1.6 Linux .

Java build Project structure

  • Project_or_Module
    • src/java (contains java source code)
    • test/java (contains JUnit tests - unit tests)
    • src/java-test (contains Integration tests)
    • etc/etc (other .. .. folders under the project)

I have the following Global configuration / build.gradle file:

   apply plugin: 'java'
   apply plugin: 'pmd'
   apply plugin: 'findbugs'
   apply plugin: 'checkstyle'
   apply plugin: 'code-quality'
   apply plugin: 'jacoco'

   tasks.withType(Compile) {
     options.debug = true
     options.compilerArgs = ["-g"]
   }

   checkstyle {
        configFile = new File(rootDir, "config/checkstyle.xml")
        ignoreFailures = true
   }


   findbugs {
        ignoreFailures = true
   }

   pmd {
        ruleSets = ["basic", "braces", "design"]
        ignoreFailures = true
   }

   jacoco {
      toolVersion = "0.6.2.201302030002"
      reportsDir = file("$buildDir/customJacocoReportDir")
   }

   sourceSets {
      main {
         java {
            srcDir 'src/java'
         }
      }
      test {
         java {
            srcDir 'test/java'
         }
      }
      integrationTest {
         java {
            srcDir 'src/java-test'
         }
      }
   }

   test {
        jacoco {
            append = false
            destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
            classDumpFile = file("$buildDir/jacoco/classpathdumps")
        }
   }

   jacocoTestReport {
         group = "Reporting"
         description = "Generate Jacoco coverage reports after running tests."
         reports {
                xml{
                    enabled true
                    destination "${buildDir}/reports/jacoco/jacoco.xml"
                }
                csv.enabled false
                html{
                    enabled true
                    destination "${buildDir}/jacocoHtml"
                }
        }
        additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
   }

For few of the projects, everything works fine. i.e. when I run "gradle clean build" or "gradle clean build jacocoTestReport", everything is successful.

For few other projects, I'm seeing the following issues and need help.

1) "gradle clean build" command works if I remove / comment out the following lines for a project. But when those lines are uncommented like in the code snapshot shown above, then I see errors. Errors are mentioned below after the code snapshot.

       apply plugin: 'java'
//   apply plugin: 'pmd'
//   apply plugin: 'findbugs'
//   apply plugin: 'checkstyle'
//   apply plugin: 'code-quality'
   apply plugin: 'jacoco'

   tasks.withType(Compile) {
     options.debug = true
     options.compilerArgs = ["-g"]
   }

//   checkstyle {
//        configFile = new File(rootDir, "config/checkstyle.xml")
//        ignoreFailures = true
//   }


//   findbugs {
//       ignoreFailures = true
//   }

//   pmd {
//        ruleSets = ["basic", "braces", "design"]
//        ignoreFailures = true
//   }
//

   jacoco {
      toolVersion = "0.6.2.201302030002"
      reportsDir = file("$buildDir/customJacocoReportDir")
   }

   sourceSets {
      main {
         java {
            srcDir 'src/java'
         }
      }
      test {
         java {
            srcDir 'test/java'
         }
      }
      integrationTest {
         java {
            srcDir 'src/java-test'
         }
      }
   }

   test {
        jacoco {
            append = false
            destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
            classDumpFile = file("$buildDir/jacoco/classpathdumps")
        }
   }

   jacocoTestReport {
         group = "Reporting"
         description = "Generate Jacoco coverage reports after running tests."
         reports {
                xml{
                    enabled true
                    destination "${buildDir}/reports/jacoco/jacoco.xml"
                }
                csv.enabled false
                html{
                    enabled true
                    destination "${buildDir}/jacocoHtml"
                }
        }
        additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
   }

Errors: A. Task failed. Output shows the following.

..
....
:compileIntegrationTestJava
 /production/jenkinsAKS/jobs/Project_or_Module/workspace/src/java-test/com/tr/ids/application/project_or_module/Project_or_ModuleTestCase.java:11:
 package org.apache.cactus does not exist import
 org.apache.cactus.ServletTestCase;
                         ^ /production/jenkinsAKS/jobs/Project_or_Module/workspace/src/java-test/com/tr/ids/application/project_or_module/Project_or_ModuleTestCase.java:13:
> cannot find symbol symbol: class ServletTestCase public class
> Project_or_ModuleTestCase extends ServletTestCase
> 
 ...
  ....
  ..... 
  some more errors 
  ...
  ... 
  100 errors (similar errors).

You'll notice here is that the error is coming when Gradle is calling the above task for "src/java-test" folder.

**My question1 **: With those lines commented in the build.gradle file, I never see these errors and "clean build jacocoTestReport" tasks completes successfully but when the code for pmd/findbugs/checkstyle is enabled, I see these errors. Why it's compiling java-test code. I thought source code is only under src/java (actual java source code for the project: project_or_module)

B. One of the other project ProjectAUtilities is failing at PMD step even though ignore error is true in build.gradle for pmd. Error says 3 tests failed.

Error log:

20:06:20 :pmdIntegrationTest UP-TO-DATE
20:06:21 :pmdMain
20:06:21 Download http://devserver2:8081/artifactory/jcenter-cache/pmd/pmd/4.3/pmd-4.3.pom
20:06:21 Download http://devserver2:8081/artifactory/jcenter-cache/asm/asm/3.2/asm-3.2.pom
20:06:21 Download http://devserver2:8081/artifactory/jcenter-cache/asm/asm-parent/3.2/asm-parent-3.2.pom
20:06:21 Download http://devserver2:8081/artifactory/jcenter-cache/junit/junit/4.4/junit-4.4.pom
20:06:21 Download http://devserver2:8081/artifactory/jcenter-cache/xml-apis/xml-apis/1.3.02/xml-apis-1.3.02.pom
20:06:21 Download http://devserver2:8081/artifactory/jcenter-cache/pmd/pmd/4.3/pmd-4.3.jar
20:06:21 Download http://devserver2:8081/artifactory/jcenter-cache/asm/asm/3.2/asm-3.2.jar
20:06:21 Download http://devserver2:8081/artifactory/jcenter-cache/junit/junit/4.4/junit-4.4.jar
20:06:24 251 PMD rule violations were found. See the report at: file:///production/jenkinsAKS/jobs/ProjectAUtilities/workspace/build/reports/pmd/main.html
20:06:26 :pmdTest
20:06:26 16 PMD rule violations were found. See the report at: file:///production/jenkinsAKS/jobs/ProjectAUtilities/workspace/build/reports/pmd/test.html
20:06:26 :test
20:06:26 Download http://devserver2:8081/artifactory/jcenter-cache/org/jacoco/org.jacoco.agent/0.6.2.201302030002/org.jacoco.agent-0.6.2.201302030002.pom
20:06:26 Download http://devserver2:8081/artifactory/jcenter-cache/org/jacoco/org.jacoco.build/0.6.2.201302030002/org.jacoco.build-0.6.2.201302030002.pom
20:06:27 Download http://devserver2:8081/artifactory/jcenter-cache/org/jacoco/org.jacoco.agent/0.6.2.201302030002/org.jacoco.agent-0.6.2.201302030002.jar
20:06:29 Xlib: connection to "localhost:13.0" refused by server
20:06:29 Xlib: PuTTY X11 proxy: wrong authorisation protocol attempted
20:06:29 
20:06:29 com.tr.ids.util.test.chart.TestChartUtilities > getPieChart FAILED
20:06:29     java.lang.InternalError at TestChartUtilities.java:89
20:06:29 
20:06:29 com.tr.ids.util.test.chart.TestChartUtilities > getLegend FAILED
20:06:29     java.lang.NoClassDefFoundError at TestChartUtilities.java:103
20:06:29 
20:06:29 com.tr.ids.util.test.chart.TestChartUtilities > useString FAILED
20:06:29     java.lang.NoClassDefFoundError at TestChartUtilities.java:143
20:06:29 
20:06:29 140 tests completed, 3 failed
20:06:29 :test FAILED
20:06:29 
20:06:29 FAILURE: Build failed with an exception.
20:06:29 
20:06:29 * What went wrong:
20:06:29 Execution failed for task ':test'.
20:06:29 > There were failing tests. See the report at: file:///production/jenkinsAKS/jobs/ProjectAUtilities/workspace/build/reports/tests/index.html
20:06:29 
20:06:29 * Try:
20:06:29 Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
20:06:29 
20:06:29 BUILD FAILED

My question2: What configuration should I set for PMD section in build.gradle that will ignore tests if they fail. I think I already have i.e. ignoreFailures = true

C. How to use include/exclude for Checkstyle/PMD/Findbugs in build.gradle file (not ANT). i.e. it'll not go and do anything other than "src/java" folder.

Thanks.

Answer

AKS picture AKS · Aug 21, 2013

The following did the trick for all issues except B(Question 2) i.e. added a sourceSets line and told those 3 tools (pmd/checkstyle/findbugs) to see only main (source code which is "src/java") under sourceSets definition. By the way, I took the "sourceSets" section and defined it before the following lines as well.

   checkstyle {
        configFile = new File(rootDir, "config/checkstyle.xml")
        ignoreFailures = true
        sourceSets = [sourceSets.main]
   }


   findbugs {
        ignoreFailures = true
        sourceSets = [sourceSets.main]
   }

   pmd {
        ruleSets = ["basic", "braces", "design"]
        ignoreFailures = true
        sourceSets = [sourceSets.main]
   }