Search code examples
mavengroovycobertura

cobertura-maven-plugin cannot find my groovy source code


I am trying to use apache-aven to produce a code-coverage report for my Java/Groovy project. Attached is the pom file:

<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>com.hal_con</groupId>
  <artifactId>scheduler</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all -->
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.4.8</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.gmaven</groupId>
        <artifactId>gmaven-plugin</artifactId>
        <version>1.5</version>
        <configuration>
          <providerSelection>1.8</providerSelection>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <plugins>
      <plugin>
        <!-- https://mvnrepository.com/artifact/org.codehaus.mojo/cobertura-maven-plugin -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.7</version>
      </plugin>
    </plugins>
  </reporting>
</project>

I've tried both the following:

In both cases the results were exactly the same:

Unable to locate com/hal_con/scheduler/FileParser.groovy. Have you specified the source directory?

I figure that the maven-cobertura-plugin needs to be told where to find my groovy sources, but I cannot find an example.


Solution

  • The Cobertura Maven Plugin doesn't provide a way to customize the location of the sources. By default, it then looks into the Maven standard folder, which is src/main/java. Since your Groovy classes are located inside src/main/groovy, they are not found.

    There are 2 solutions depending on your project:

    • Add those sources to the project with the help of the build-helper-maven-plugin:add-source Mojo:

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/main/groovy</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
      

      This is helpful if the project is a mixed Java / Groovy project, because you can keep the Maven defaults, and add the Groovy specific folders.

    • Override the source directory of Maven with

      <build>
        <sourceDirectory>src/main/groovy</sourceDirectory>
        <!-- rest of build configuration -->
      </build>
      

      This would be convenient if the project is a pure Groovy project, without any source Java files.

    With any of those two changes, running mvn clean site will generate a Cobertura report where the Groovy sources will be correctly found.