简体   繁体   中英

Maven checkstyle not running as part of build

I'm totally lost as to why not. Structure is like this:

using mvn checkstyle:check creates the reports (I have identical settings in reporting section) but build treats the plugin like it doesn't exist. If I change the checkstyle version, it doesn't download it, etc. Essentially it treats this config as invisible.

<build>
   <pluginManagement>
         <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-checkstyle-plugin</artifactId>
              <version>2.13</version>

              <configuration>
                <configLocation>checkstyle.xml</configLocation>
                <suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
                <maxAllowedViolations>2500</maxAllowedViolations>
                <consoleOutput>true</consoleOutput>
                <failsOnError>true</failsOnError>
                <failOnViolation>true</failOnViolation>
              </configuration>

              <executions>
                <execution>
                  <id>checkstyle</id>
                  <goals>
                    <goal>check</goal>
                  </goals>
                  <configuration>

                    <encoding>UTF-8</encoding>
                    <consoleOutput>true</consoleOutput>
                    <failsOnError>true</failsOnError>
                    <failOnViolation>true</failOnViolation>
                  </configuration>
                </execution>
              </executions>
            </plugin>
.....
</plugins>
</pluginManagement>
</build>

You can add the plugin to the pom as follows and it will execute as part of your build ( mvn clean install ):

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>3.0.0</version>
  <configuration>
    <configLocation>checkstyle.xml</configLocation>
    <suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
    <consoleOutput>true</consoleOutput>
    <maxAllowedViolations>2500</maxAllowedViolations>
    <failsOnError>true</failsOnError>
    <failOnViolation>true</failOnViolation>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>check</goal>
      </goals>
    </execution>
  </executions>
</plugin>

See also: Checkstyle not working

And actually, for most of those configuration parameters (all but failsOnError , which does not have a "User Property") you can alternatively put them in your pom.xml as properties:

  <properties>
    ...
    <checkstyle.config.location>checkstyle.xml</checkstyle.config.location>
    <checkstyle.suppressions.location>checkstyle-suppressions.xml</checkstyle.suppressions.location>
    <checkstyle.consoleOutput>true</checkstyle.consoleOutput>
    <checkstyle.maxAllowedViolations>2500</checkstyle.maxAllowedViolations>
    <checkstyle.failOnViolation>true</checkstyle.failOnViolation>
  </properties>

That way you can remove them from the plugin configuration section for convenience, though either approach works. See https://maven.apache.org/plugins/maven-checkstyle-plugin/check-mojo.html for "User property" values, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM