0

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)
Chusya
  • 90
  • 2
  • 9

3 Answers3

2

Maybe com.myProj.myApi is a single directory? whereas it should be 3 directories nested into each other com / myProj / myApi

Godin
  • 9,801
  • 2
  • 39
  • 76
  • And there is. By the way, above I under the heading EDIT indicated a possible reason, am I right? – Chusya Jul 12 '21 at 08:20
  • 1
    You're mixing many unrelated things together - "Classes in bundle 'myProj' do no match with execution data" has nothing to do with with original "Source file was not found" and with version of Java. – Godin Jul 12 '21 at 10:46
  • 1
    And IMO there is no https://stackoverflow.com/help/minimal-reproducible-example in your description, so unlikely that somebody will be able to blindly guess what is wrong in your case. Have a look at https://docs.gradle.org/current/samples/sample_jvm_multi_project_with_code_coverage.html and compare your setup with following minimal-reproducible-example https://github.com/gradle/gradle/tree/4e03f54df0ddfdc9eca268cb38bc6034d25e38ff/subprojects/docs/src/samples/java/jvm-multi-project-with-code-coverage – Godin Jul 12 '21 at 10:52
  • I got the same result. This is strange because when I use Intellij IDEA Run> Show Code Coverage> test.exec, the IDE toolkit shows me the highlight code. – Chusya Jul 12 '21 at 13:32
1

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.

Chusya
  • 90
  • 2
  • 9
0

I was facing a similar issue. I was using some plugins which were generating source java files in the build folder. The coverage report (which included % coverage) for these source files generated in build folder was coming up but the exact source code file with coverage was not getting generated. I was getting the error: source file com/abc/xyz/aop/LoggingAspect.java was not found during generation of report.

I found the solution to my issue in JaCoCo's FAQ page: https://www.jacoco.org/jacoco/trunk/doc/faq.html Source files must be properly supplied at report generation time. I.e. specified source folders must be the direct parent of the folders that define the Java packages.

The sourceSet that I added for the generated code in build directory was not the direct parent of the Java package.

Project structure:

project-root
- build
-- generated
--- sources
---- com/abc/xyz ('com.abc.xyz' Java package)

- src/main/java
-- com/abc/xyz ('com.abc.xyz' Java package)

-build.gradle

The initial incorrect build.gradle file to add generated code to sourceSets:

sourceSets {
  main {
    java {
      srcDirs += 'build/generated/sources/com'
    }
  }
  test {
    java {
      srcDirs += 'build/generated/test-sources/com'
    }
  }
}

I configured the source directory for generated code to build/generated/sources instead of build/generated/sources/com and it worked.

Because build/generated/sources is a direct parent of com.abc.xyz but build/generated/sources/com isn't as it includes the path com in it.

himan085
  • 21
  • 3