Using an Inline Checkstyle Checker Configuration
Since version 2.12, an inlined Checker configuration can be defined directly in the configuration
section of the plugin itself inside your pom.xml
.
This is especially useful when the Checkstyle checks should be executed with every compile and you do not like to create an own project for your Checkstyle rules and neither like to have a separate parent POM structure.
To define a custom Checkstyle checker configuration inside your pom.xml
, use the checkstyleRules
parameter.
<project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.5.0</version> <executions> <execution> <id>verify-style</id> <phase>process-classes</phase> <goals> <goal>check</goal> </goals> </execution> </executions> <configuration> <logViolationsToConsole>true</logViolationsToConsole> <checkstyleRules> <module name="Checker"> <!-- Checks for Size Violations. --> <!-- See http://checkstyle.sf.net/config_sizes.html --> <module name="FileLength"> <property name="max" value="3500" /> <property name="fileExtensions" value="java"/> </module> <!-- Checks for whitespace --> <!-- See http://checkstyle.sf.net/config_whitespace.html --> <module name="FileTabCharacter"/> <module name="TreeWalker"> <module name="StaticVariableName"/> <module name="TypeName"> <property name="format" value="^_?[A-Z][a-zA-Z0-9]*$"/> </module> </module> </module> </checkstyleRules> </configuration> </plugin> </plugins> </build> ... </project>
Using a different header
The inline configuration works by writing the configuration to a file and passing that file to checkstyle. This configuration file must contain a specific header.
We include one by default that is compatible with the included checkstyle version.
When you use a different checkstyle version than the the one that's included it might be necessary for you to specify a different header:
<project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.5.0</version> <executions> <execution> <id>verify-style</id> <phase>process-classes</phase> <goals> <goal>check</goal> </goals> </execution> </executions> <configuration> <logViolationsToConsole>true</logViolationsToConsole> <checkstyleRules> <module name="Checker"> <!-- Checks for Size Violations. --> <!-- See http://checkstyle.sf.net/config_sizes.html --> <module name="FileLength"> <property name="max" value="3500" /> <property name="fileExtensions" value="java"/> </module> <!-- Checks for whitespace --> <!-- See http://checkstyle.sf.net/config_whitespace.html --> <module name="FileTabCharacter"/> <module name="TreeWalker"> <module name="StaticVariableName"/> <module name="TypeName"> <property name="format" value="^_?[A-Z][a-zA-Z0-9]*$"/> </module> </module> </module> </checkstyleRules> <checkstyleRulesHeader> <![CDATA[ <!DOCTYPE module PUBLIC "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN" "https://checkstyle.org/dtds/configuration_1_3.dtd"> ]]> </checkstyleRulesHeader> </configuration> <dependencies> <dependency> <groupId>com.puppycrawl.tools</groupId> <artifactId>checkstyle</artifactId> <version>8.15</version> </dependency> </dependencies> </plugin> </plugins> </build> ... </project>