Search code examples
javagradlejacocomulti-module

Source file was not found during generation of report


help me understand what's wrong, please. I've worked with multi-module project and faced one problem. My architecture look like this:

proj1
-src
--main
---java
----com.myProj.myApi
-----directories containing .java files

... // same proj2 and proj3

rootProject src
-main
--java
---com.myProj.myApi
----Application.java
-test
--test .java files

In the root gradle, I have defined the following:

  1. Variable with project names.
  2. After testing useTestNG(), the jacocoTestReport() task starts.
  3. Inside the jacocoTestReport(), I have determined which java files need to be loaded and checked. My root build.gradle:
  repositories {
    mavenLocal()
    mavenCentral()
    tasks.withType(Javadoc).all { enabled = false }
    tasks.withType(JavaCompile).all { options.encoding = 'UTF-8'  }
  }

  group = 'com.myProj.myApi'
  version = '1.0.0-SNAPSHOT'

  apply plugin: 'java'
  apply plugin: 'jacoco'
  apply plugin: 'java-library'

  jacoco {
    toolVersion = "0.8.5"
  }

  def otherProjects = [':proj1', ':proj2', ':proj3']

test {
  useTestNG()
  finalizedBy jacocoTestReport
}

otherProjects.each {
  evaluationDependsOn it
}

jacocoTestReport {
  FileTree sourceTree = files().getAsFileTree()
  FileTree classTree = files().getAsFileTree()

  otherProjects.each {
    sourceTree += project(it).sourceSets.main.allJava
    classTree += project(it).sourceSets.main.output.asFileTree
  }

  sourceTree = sourceTree.matching {
    exclude '**/nonTestedClass.java'
  }

  classTree = classTree.matching {
    exclude '**/nonTestedClass.class'
  }

  getAdditionalSourceDirs().setFrom(sourceTree)
  getAdditionalClassDirs().setFrom(classTree)
  reports {
    html.enabled = true
    xml.enabled = true
    csv.enabled = false
  }
}

dependencies {
  implementation project(':proj1')
  implementation project(':proj2')
  implementation project(':proj3')
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
  testImplementation 'org.testng:testng'
}

Gradle version is 7.0.1. Java 11. I've looked this post, but my paths for each project are correct as you can see in architecture.

enter image description here

EDIT 1 I found that in each subproject, the build directories contain class files only for those java files that are in a specific subproject. Maybe this is the problem? Maybe I need to send all class files to the root build directory or not? P.S. build.gradle of subprojects do not contain any jacoco settings.

EDIT 2 I've added print for getAdditional...Dirs() and learned that all paths are correct and found. Also terminal gives me this info:

> Task :jacocoTestReport
[ant:jacocoReport] Classes in bundle 'myProj' do no match with execution data. For report generation the same class files must be used as at runtime.

But it's very strange, because when i've added this code

println(JavaVersion.current())

I've got this result: Java Version: 11. It's correct version of my Java:

$ java --version
openjdk 11.0.11 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)

Solution

  • I've fixed it when I changed work with files. I've deleted all code from 'jacocoTestReport {' to 'reports {' (non include) and insert this:

      for (proj in otherProjects) {
        sourceSets project(module).sourceSets.main
      }
    

    I don't understand why getAdditionalSourceDirs() and getAdditionalClassDirs() aren't working. Or, most likely, i don't know difference between these 2 methods of interaction with files. If you know, you can comment under this answer.