Gradle Common global file under init.d - Could not find method test() / jacocoTestReport()


(arun_sangal) #1

Summary:

•Platform Linux / Java, gradle 1.6 and Java/jdk 1.6.0.xx

•Project name: GigaProject

I have a couple of projects like GigaProject. All of them have a build.gradle file which right now contains the following lines specially for PMD, CheckStyle, FindBugs and Jacoco code coverage for Unit tests. Everything is working fine. When I run “gradle clean build”, project is compiled successfully and shows me the code coverage reports (jacoco) and findbugs/checkstyle/pmd reports.

PNote: I mentioned, there is other piece of code for build/compile which I have not listed here. The following code is only an addition to get pmd/findbugs/checkstyle and Jacoco features to work.

allprojects {
   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")
   }
       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)
       }
}

What I want is: Instead of having the above code in every project, I wanted to take it out from each project’s build.gradle file and put it under /init.d/ commmons file.

i.e. if you have gradle 1.6 installed at /opt/gradle-1.6, then I want to add the above code to /opt/gradle-1.6/init.d/extra1.common-somename.gradle file. Any files which are present inside INIT.D folder are read first by Gradle so now we dont have to put the above code in every project’s individual build.gradle file.

When I’m trying this approach on my local Windows machine (where gradle is stored at C:\gradle-1.6), I added the code in a file: extra1.common-thids.gradle and I removed the same from project’s build.gradle. It works successfully like when the code was in project’s build.gradle file only if I dont include test { } and jacocoTestReport { } sections. But with these 2, I’m getting the following error.

$ gradle clean build The code-quality plugin HAS BEEN DEPRECATED AND IS SCHEDULED TO BE REMOVED IN GRADLE 2.0. Please use the checkstyle or codenarc plugin instead.

FAILURE: Build failed with an exception.

  • Where: Initialization script ‘C:\gradle-1.6\init.d\extra1.common-thids.gradle’ line: 34

  • What went wrong: Failed to notify action. > org.gradle.api.internal.MissingMethodException: Could not find method test() for arguments [extra1_common_thids_5e0u69jd6ubohvgcf1qr8mhamk$_run_closure1_closure7@56461f58] on build ‘GigaProject’.

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2.695 secs

Why we selected to do this: So that when a Developer wants to run “gradle build clean” he won’t be able to see pmd/checkstyle/findbugs/jacoco funcationalities in his local build (as we’ll tell him NOT to put the above code in his Gradle’s init.d common file) BUT when we’ll run the same builds from Jenkins (SCM/CM team), then Jenkins server’s Gradle-x.x/init.d/xxx.common-xxx.gradle build file will contain the above code and Jenkins will show all features at that time.

This will help developers to get quick builds at their local machines and Jenkins will show them what they want from pmd/findbugs/checkstyle/jacoco side.

What do I need to include in the common file that will let me include test { } and jacocoTestReport { } sections in global common file and fix this issue?

Thanks a lot.


(arun_sangal) #2

Including the following line as well, now :slight_smile: seems like I can remove the following from project’s specific build.gradle file.

apply plugin: 'java'

did the trick


(arun_sangal) #3

Still has the following issue though.

http://stackoverflow.com/questions/18365618/gradle-pmd-checkstyle-findbugs-which-sourceset-directory-to-work-java-build