Ozsie/detekt-maven-plugin

Generate classpath for type resolution

pcaravelli-sr opened this issue · 1 comments

A bit ago, I was trying to use this plugin with Type Resolution, but ran into some issues getting the classpath specified correctly as required. For mostly other unrelated reasons, I decided to use detekt-cli via antrun plugin, but figured I'd put in this issue in case it helps others, or perhaps helps prioritize a built-in fix.

The problem

Enabling Type Resolution requires providing a classpath with all the JARs that should be considered by Detekt during code analysis. I took a look at #60, but the recommended solutions for providing the classpath there didn't scale; as far as I could tell, the recommendation was to manually stitch together paths to all the JARs you want considered in Detekt's check. For a project with any real number of dependencies, this isn't really workable.

Workaround

The best workaround I found was to use the maven-dependency-plugin to generate the classpath string, and put it into an output property, like so:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
        <execution>
            <id>generate-classpath-var</id>
            <phase>package</phase>
            <goals><goal>build-classpath</goal></goals>
            <configuration>
                <outputProperty>generated.classpath</outputProperty>
                <silent>true</silent>
            </configuration>
        </execution>
    </executions>
</plugin>

Once configured as such, the classpath can be specified in the detekt plugin as such:

<classPath>${generated.classpath}</classPath>

However, the main downside was that the maven-dependency-plugin needed to run before the detekt plugin. This was fine if I was running mvn verify, but broke usage of mvn detekt:check.

Having moved back to using detekt-cli, I am able to solve this problem via maven-antrun-plugin's classpath support.

All of this said, the workaround I stitched together above, and the classpath support from the maven-antrun-plugin indicate that determining the classpath dynamically is possible. If there is enough need for it, it would be a really nice quality-of-life improvement for this plugin!

Ozsie commented

Hi, thanks for the info and a second, in many ways nicer, workaround.

Just so I understand it correctly. If you activate Type Resolution, would you need all dependencies in the classpath? If so it could be done, otherwise you would have to specify manually.