Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate classpath for type resolution #144

Closed
pcaravelli-sr opened this issue Apr 11, 2022 · 2 comments
Closed

Generate classpath for type resolution #144

pcaravelli-sr opened this issue Apr 11, 2022 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@pcaravelli-sr
Copy link

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
Copy link
Owner

Ozsie commented Apr 15, 2022

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.

@Ozsie Ozsie self-assigned this Apr 15, 2022
@Ozsie Ozsie added the enhancement New feature or request label Apr 15, 2022
sesigl pushed a commit to sesigl/detekt-maven-plugin that referenced this issue Jul 23, 2022
@Ozsie Ozsie closed this as completed in 4470f5e Aug 11, 2022
@jakub-bochenski
Copy link

This was fine if I was running mvn verify, but broke usage of mvn detekt:check.

FTR you could work around it by using a profile that executes both, then running it like mvn -Pdetekt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants