scalamavenintellij-ideanosuchmethoderrorscoverage

error: java.lang.NoSuchMethodError: 'scala.tools.nsc.reporters.Reporter scala.tools.nsc.Global.reporter()' in scoverage-maven-plugin


I am trying to get some reports with the code coverage information for my project. I found that scoverage-maven-plugin produce xml files covering the % of the different modules that the unit test cases have covered so far.

So I tried to add that plugin in my pom, but I am getting the following error:

[ERROR] error: java.lang.NoSuchMethodError: 'scala.tools.nsc.reporters.Reporter scala.tools.nsc.Global.reporter()'
[INFO]  at scoverage.ScoverageInstrumentationComponent$$anon$1.run(plugin.scala:115)
[INFO]  at scala.tools.nsc.Global$Run.compileUnitsInternal(Global.scala:1514)
[INFO]  at scala.tools.nsc.Global$Run.compileUnits(Global.scala:1498)
[INFO]  at scala.tools.nsc.Global$Run.compileSources(Global.scala:1491)
[INFO]  at scala.tools.nsc.Global$Run.compile(Global.scala:1620)
[INFO]  at scala.tools.nsc.Driver.doCompile(Driver.scala:47)
[INFO]  at scala.tools.nsc.MainClass.doCompile(Main.scala:32)
[INFO]  at scala.tools.nsc.Driver.process(Driver.scala:67)
[INFO]  at scala.tools.nsc.Driver.main(Driver.scala:80)
[INFO]  at scala.tools.nsc.Main.main(Main.scala)
[INFO]  at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
[INFO]  at java.base/java.lang.reflect.Method.invoke(Method.java:577)
[INFO]  at scala_maven_executions.MainHelper.runMain(MainHelper.java:164)
[INFO]  at scala_maven_executions.MainWithArgsInFile.main(MainWithArgsInFile.java:26)
[INFO] java.lang.reflect.InvocationTargetException
[INFO]  at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
[INFO]  at java.base/java.lang.reflect.Method.invoke(Method.java:577)
[INFO]  at scala_maven_executions.MainHelper.runMain(MainHelper.java:164)
[INFO]  at scala_maven_executions.MainWithArgsInFile.main(MainWithArgsInFile.java:26)
[ERROR] Caused by: java.lang.NoSuchMethodError: 'scala.tools.nsc.reporters.Reporter scala.tools.nsc.Global.reporter()'
[INFO]  at scoverage.ScoverageInstrumentationComponent$$anon$1.run(plugin.scala:115)
[INFO]  at scala.tools.nsc.Global$Run.compileUnitsInternal(Global.scala:1514)
[INFO]  at scala.tools.nsc.Global$Run.compileUnits(Global.scala:1498)
[INFO]  at scala.tools.nsc.Global$Run.compileSources(Global.scala:1491)
[INFO]  at scala.tools.nsc.Global$Run.compile(Global.scala:1620)
[INFO]  at scala.tools.nsc.Driver.doCompile(Driver.scala:47)
[INFO]  at scala.tools.nsc.MainClass.doCompile(Main.scala:32)
[INFO]  at scala.tools.nsc.Driver.process(Driver.scala:67)
[INFO]  at scala.tools.nsc.Driver.main(Driver.scala:80)
[INFO]  at scala.tools.nsc.Main.main(Main.scala)
[INFO]  at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
[INFO]  ... 3 more

Not able to find from where I should get scala.tools.nsc.reporters.Reporter scala.tools.nsc.Global.reporter()

The full pom is:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>mygroupid</groupId>
    <artifactId>myartifact</artifactId>
    <version>1.0.0</version>
    <name>myproject</name>

    <properties>
        <app.build.version>1.0.0</app.build.version>
        <encoding>UTF-8</encoding>
        <scala.version>2.12.14</scala.version>
        <scala.compat.version>2.12</scala.compat.version>
        <scala.binary.version>2.12</scala.binary.version>
        <spark.version>3.2.1</spark.version>
        <scope.value>provided</scope.value>
        <scoverage.plugin.version>1.3.0</scoverage.plugin.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_${scala.binary.version}</artifactId>
            <version>${spark.version}</version>
            <scope>${scope.value}</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_${scala.binary.version}</artifactId>
            <version>${spark.version}</version>
            <scope>${scope.value}</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-hive_${scala.binary.version}</artifactId>
            <version>${spark.version}</version>
            <scope>${scope.value}</scope>
        </dependency>

        <dependency>
            <groupId>io.delta</groupId>
            <artifactId>delta-core_2.12</artifactId>
            <version>2.0.0</version>
            <scope>${scope.value}</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-streams</artifactId>
            <version>3.1.2</version>
            <scope>${scope.value}</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql-kafka-0-10_${scala.binary.version}</artifactId>
            <version>${spark.version}</version>
            <scope>${scope.value}</scope>
        </dependency>

        <dependency>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest_2.12</artifactId>
            <version>3.2.14</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <build>
        <finalName>myproject-${app.build.version}</finalName>
        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>
        <plugins>

            <plugin>
                <groupId>org.scoverage</groupId>
                <artifactId>scoverage-maven-plugin</artifactId>
                <version>${scoverage.plugin.version}</version>
                <configuration>
                    <scalaVersion>${scala.version}</scalaVersion>
                    <highlighting>true</highlighting>
                    <aggregate>true</aggregate>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <phase>test</phase>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.2</version>
                <executions>
                    <execution>
                        <id>Scaladoc</id>
                        <goals>
                            <goal>doc</goal>
                        </goals>
                        <phase>prepare-package</phase>
                        <configuration>
                            <args>
                                <arg>-no-link-warnings</arg>
                            </args>
                        </configuration>
                    </execution>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>doc-jar</goal>
                        </goals>
                    </execution>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <args>
                                <arg>-dependencyfile</arg>
                                <arg>${project.build.directory}/.scala_dependencies</arg>
                            </args>
                            <jvmArgs>
                                <jvmArg>-Xss4m</jvmArg>
                                <jvmArg>-Xms512m</jvmArg>
                                <jvmArg>-Xmx4096m</jvmArg>
                            </jvmArgs>
                            <scalaVersion>${scala.version}</scalaVersion>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <scalaVersion>${scala.version}</scalaVersion>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                            <appendAssemblyId>false</appendAssemblyId>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.scalatest</groupId>
                <artifactId>scalatest-maven-plugin</artifactId>
                <version>1.0</version>
                <configuration>
                    <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                    <junitxml>.</junitxml>
                    <filereports>WDF TestSuite.txt</filereports>
                    <htmlreporters>${project.build.directory}/html/scalatest</htmlreporters>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <phase>test</phase>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

Edits after @Dmytro feedback:

I added in the pom the following dependencies:

        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-compiler</artifactId>
            <version>${scala.version}</version>
        </dependency>

        <dependency>
            <groupId>org.scoverage</groupId>
            <artifactId>scalac-scoverage-plugin_2.12</artifactId>
            <version>1.4.1</version>
        </dependency>

And changed the scope to

<scope.value>compile</scope.value>

But I am still getting the error: java.lang.NoSuchMethodError: 'scala.tools.nsc.reporters.Reporter scala.tools.nsc.Global.reporter()'


Solution

  • Could you try to add

    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-compiler</artifactId>
        <!--<version>2.12.14</version>-->
        <version>${scala.version}</version>
    </dependency>
    

    ?

    https://mvnrepository.com/artifact/org.scala-lang/scala-compiler

    Does this change anything for you?


    scoverage-maven-plugin 1.3.0 depends on scalac-scoverage-plugin 1.3.0

    https://github.com/scoverage/scoverage-maven-plugin/blob/scoverage-maven-plugin-1.3.0/pom.xml#L93-L101

    scalac-scoverage-plugin 1.3.0 depends on scala-compiler 2.12.0

    https://index.scala-lang.org/scoverage/scalac-scoverage-plugin/artifacts/scalac-scoverage-plugin/1.3.0

    https://github.com/scoverage/scalac-scoverage-plugin/blob/v1.3.0/build.sbt#L89

    https://github.com/scoverage/scalac-scoverage-plugin/blob/v1.3.0/build.sbt#L15

    scala-compiler is marked as provided in the dependencies of scalac-scoverage-plugin so should be added manually when needed.

    The method scala.tools.nsc.Global.reporter() returns Reporter in scala-compiler 2.12.0 https://github.com/scala/scala/blob/v2.12.0/src/compiler/scala/tools/nsc/Global.scala#L1581 (it seems in 2.12.0 there is no scala.tools.nsc.Global.reporter(), it appears in 2.12.5). In 2.12.14 the signature is different, the method returns FilteringReporter https://github.com/scala/scala/blob/v2.12.14/src/compiler/scala/tools/nsc/Global.scala#L1748 https://github.com/scala/scala/blob/v2.12.14/src/compiler/scala/tools/nsc/Global.scala#L90 So the issue seems to be in the combination of versions scalac-scoverage-plugin 1.3.0 + scala-compiler 2.12.14.

    Try to either upgrade scoverage-plugin or downgrade Scala version so that the signature of the method is the same (returning either Reporter both times or FilteringReporter both times). The signature changed in 2.12.12 -> 2.12.13 (Reporter -> FilteringReporter) https://github.com/scala/scala/blob/v2.12.12/src/compiler/scala/tools/nsc/Global.scala#L90 https://github.com/scala/scala/blob/v2.12.13/src/compiler/scala/tools/nsc/Global.scala#L90 If you're on scoverage-plugin 1.4.1 can you change Scala to 2.12.12?


    Try then to downgrade the scala version:

    <properties>
        <scala.version>2.12.12</scala.version>
        ...
    </properties>
    

    in combination with

    <dependency>
        <groupId>org.scoverage</groupId>
        <artifactId>scalac-scoverage-plugin_2.12</artifactId>
        <version>1.4.1</version>
    </dependency>