Ozsie / detekt-maven-plugin

Maven wrapper for detekt cli
Apache License 2.0
56 stars 18 forks source link

detekt:check not using config from pom.xml #70

Closed olxmute closed 4 years ago

olxmute commented 4 years ago

I have checkstyle plugin for Java in the project. In pom.xml I have such config that contains paths to checkstyle configuration and supressions files:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>${maven-checkstyle-plugin.version}</version>
    <configuration>
        <sourceDirectories>
            <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
            <sourceDirectory>${project.build.testSourceDirectory}</sourceDirectory>
        </sourceDirectories>
        <configLocation>${main.basedir}/config/checkstyle/checkstyle.xml</configLocation>
        <suppressionsLocation>${main.basedir}/config/checkstyle/checkstyle-suppressions.xml</suppressionsLocation>
        <suppressionsFileExpression>${main.basedir}/config/checkstyle/checkstyle.suppressions.file</suppressionsFileExpression>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

When I run mvn checkstyle:check maven excecutes plugin using configuration from pom.xml.

And I have similar config for detekt:

<plugin>
    <groupId>com.github.ozsie</groupId>
    <artifactId>detekt-maven-plugin</artifactId>
    <version>${detekt-maven-plugin.version}</version>
    <executions>
        <execution>
            <phase>verify</phase>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration>
                <config>config/detekt/detekt-config.yml</config>
                <baseline>config/detekt/baseline.xml</baseline>
                <report>
                    <report>html:target/reports/detekt.html</report>
                </report>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>io.gitlab.arturbosch.detekt</groupId>
            <artifactId>detekt-formatting</artifactId>
            <version>${detekt-maven-plugin.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</plugin>

But if I want to run mvn detekt:check maven will use default detekt configuration instead of the pom.xml one. Maven use that config only during mvn clean install. I think it would be great if it we could excecute mvn detekt:check with config from pom.xml like we doing with checkstyle plugin.

Ozsie commented 4 years ago

The reason for this is a difference in how the two plugins are configured. The check style plugin has it's configuration outside of the execution block, and the detekt plugin has it inside the execution block.

If you define a configuration block outside of the executions block it will work as you expect, like this

<plugin>
    <groupId>com.github.ozsie</groupId>
    <artifactId>detekt-maven-plugin</artifactId>
    <version>${detekt-maven-plugin.version}</version>
    <configuration>
        <config>config/detekt/detekt-config.yml</config>
        <baseline>config/detekt/baseline.xml</baseline>
        <report>
            <report>html:target/reports/detekt.html</report>
        </report>
    </configuration>
    <executions>
        <execution>
            <phase>verify</phase>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration>
                <config>config/detekt/detekt-config.yml</config>
                <baseline>config/detekt/baseline.xml</baseline>
                <report>
                    <report>html:target/reports/detekt.html</report>
                </report>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>io.gitlab.arturbosch.detekt</groupId>
            <artifactId>detekt-formatting</artifactId>
            <version>${detekt-maven-plugin.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</plugin>

To make it simpler, if you want the same configuration for mvn detekt:check as for mvn clean install, you could remove the configuration block from inside the execution block.

olxmute commented 4 years ago

Thanks, it helped! Now ot works as expected.

I've been configuring the plugin following your README and didn't know about this maven plugins configuration feature. I think it would be helpful for newcomers if you add this case to examples in README.

Ozsie commented 4 years ago

Yeah, I'll add it to the readme when I merge the latest detekt version.

lucasfarre commented 4 years ago

Hi @Ozsie ! Thanks a lot, this also helped me!

Just one more thing, I uploaded the configuration file to Amazon S3 bucket, but I can't make the plugin work by providing the public URL. Is this not supported yet?

Ozsie commented 4 years ago

I would say you have to check with detekt directly, the parameters are passed on to the detekt CLI as strings, so if it doesn't work it's something detekt does not support.