How to publish Scalastyle results in Jenkins Maven job?

1.6k Views Asked by At

We have a Scala project and use Maven and Jenkins for building it. Now we also want to use Scalastyle for static code analysis. It works fine in the IDE and if I use a Jenkins freestyle job. The output file is created and Jenkins shows a nice graph with the style warnings and errors.

However, if I use a Jenkins Maven job, the "Checkstyle trend" remains empty, although the xml file is there and has the right name (checkstyle-result.xml).

I found this. The discussion there is a bit confusing, but I understood that publishing the Scalastyle results should also work for a Maven job.

Did I get that wrong? Or is there in fact a way to make Jenkins publish the Scalastyle results for my Maven job? Or some kind of workaround? (Apart from using a freestyle job or SBT...)

1

There are 1 best solutions below

0
On

After trying out some things, I found the solution. All I needed was a Scalastyle entry in the root pom of my multi-module project that looks like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.scalastyle</groupId>
            <artifactId>scalastyle-maven-plugin</artifactId>
            <version>0.8.0</version>
            <configuration>
                <sourceDirectory>${project.basedir}/src/main/scala</sourceDirectory>
                <testSourceDirectory>${project.basedir}/src/test/scala</testSourceDirectory>
                <configLocation>scalastyle_config.xml</configLocation>
                <outputFile>${project.basedir}/checkstyle-result.xml</outputFile>
                <outputEncoding>UTF-8</outputEncoding>
            </configuration>
        </plugin>
    </plugins>
</build>

The important part here is ${project.basedir}. I tried it before without and it didn't work.

In the project configuration in Jenkins I give the scalastyle:check goal in the section Build -> Goals and options, e.g.

clean install scalastyle:check 

Under Build settings I ticked the checkbox Publish Checkstyle analysis results.

Now Jenkins publishes the Scalastyle results. One problem remains, however: Jenkins doesn't seem to pick up my Scalastyle config file (scalastyle_config.xml) but uses the scalastyle-maven-plugin's default rules. At least that's what I suspect, because for the same project I get more Scalastyle warnings in IntelliJ (which uses my custom config file) than in Jenkins...

UPDATE: I was wrong with that. Jenkins does indeed pick up my custom rules. Nevertheless it shows more Scalastyle warnings than IntelliJ does. I'll look into that later.