【问题标题】:Gradle Java build - Global init.d common gradle file - Could not find method test()Gradle Java 构建 - 全局 init.d 通用 gradle 文件 - 找不到方法 test()
【发布时间】:2013-08-16 14:37:47
【问题描述】:

总结:

  • 平台 Linux/Java、gradle 1.6 和 Java/jdk 1.6.0.xx
  • 项目名称:GigaProject

我有几个项目,比如 GigaProject。它们都有一个 build.gradle 文件,现在包含以下行,专门用于单元测试的 PMD、CheckStyle、FindBugs 和 Jacoco 代码覆盖率。一切正常。当我运行“gradle clean build”时,项目编译成功并显示代码覆盖率报告(jacoco)和 findbugs/checkstyle/pmd 报告。

PNote:我提到过,还有其他用于构建/编译的代码我没有在这里列出。以下代码只是使 pmd/findbugs/checkstyle 和 Jacoco 功能正常工作的补充。

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

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


   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)    
   }
}

我想要的是:我不想在每个项目中都有上面的代码,而是想从每个项目的 build.gradle 文件中取出它并将其放在 /init.d/ commmons 文件下。

即如果您在 /opt/gradle-1.6 上安装了 gradle 1.6,那么我想将上述代码添加到 /opt/gradle-1.6/init.d/extra1.common-somename.gradle 文件中。 Gradle 会首先读取 INIT.D 文件夹中存在的任何文件,因此现在我们不必将上述代码放入每个项目的单独 build.gradle 文件中。

当我在本地 Windows 机器(gradle 存储在 C:\gradle-1.6)上尝试这种方法时,我在文件中添加了代码:extra1.common-thids.gradle 并从项目的 build.gradle。仅当我不包含 test { } 和 jacocoTestReport { } 部分时,它才能像代码在项目的 build.gradle 文件中一样成功运行。但是有了这两个,我得到了以下错误。

$ 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

我们选择这样做的原因:这样当开发人员想要运行“gradle build clean”时,他将无法在本地构建中看到 pmd/checkstyle/findbugs/jacoco 功能(我们会告诉他不要将上面的代码放在他的 Gradle 的 init.d 通用文件中)但是当我们从 Jenkins(SCM/CM 团队)运行相同的构建时,然后是 Jenkins 服务器的 Gradle-x.x/init.d/xxx.common-xxx。 gradle 构建文件将包含上述代码,Jenkins 将显示当时的所有功能。

这将帮助开发人员在他们的本地机器上快速构建,Jenkins 将从 pmd/findbugs/checkstyle/jacoco 方面向他们展示他们想要的东西。

我需要在通用文件中包含哪些内容才能让我在全局通用文件中包含 test { } 和 jacocoTestReport { } 部分并解决此问题?

非常感谢。

【问题讨论】:

    标签: file testing gradle global jacoco


    【解决方案1】:

    还包括以下行 - 成功了。现在:) 似乎我可以从项目的特定 build.gradle 文件中删除以下内容。

    apply plugin: 'java' 
    

    但是,现在 JaCoCo 代码覆盖率报告 index.xml - 在浏览红色/绿色内容的代码覆盖率链接时不显示文件内容的链接。

    【讨论】:

    • 好的,必须在通用全局 init.d extra1.common...gradle 文件中包含 sourceSets 结构。现在又开始工作了。我将修复上面代码快照中的代码行。
    猜你喜欢
    • 2015-01-22
    • 2015-09-25
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    相关资源
    最近更新 更多