------ Using Custom Developed Checkstyle Checks ------ 2008-02-04 ------ ~~ Licensed to the Apache Software Foundation (ASF) under one ~~ or more contributor license agreements. See the NOTICE file ~~ distributed with this work for additional information ~~ regarding copyright ownership. The ASF licenses this file ~~ to you under the Apache License, Version 2.0 (the ~~ "License"); you may not use this file except in compliance ~~ with the License. You may obtain a copy of the License at ~~ ~~ http://www.apache.org/licenses/LICENSE-2.0 ~~ ~~ Unless required by applicable law or agreed to in writing, ~~ software distributed under the License is distributed on an ~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ~~ KIND, either express or implied. See the License for the ~~ specific language governing permissions and limitations ~~ under the License. ~~ NOTE: For help with the syntax of this file, see: ~~ http://maven.apache.org/doxia/references/apt-format.html Using Custom Developed Checkstyle Checks We will guide you through creating a Maven project that produces a jar file of your custom checks. Then we'll include it in one of your projects by adding it as a dependency to the Checkstyle Plugin. The plugin also provides the ability to define the {{{http://checkstyle.sourceforge.net/config.html#Packages}package names XML document}} required to reference your custom check modules more easily. In this example we will be using the {{{http://checkstyle.sourceforge.net/writingchecks.html#Visitor%20in%20action}example check}} that can be found on the Checkstyle web site. * Structuring your project First we set up the directory structure for the custom ckecks project. It looks like this: +-----+ mycompany-checkstyle-checks |-- pom.xml `-- src `-- main `-- java `-- com `-- mycompany `-- checks |-- packagenames.xml `-- MethodLimitCheck.java +-----+ Now we'll go through each of the three files one at a time. ** <<>> Not much to say here, except that we add a dependency on Checkstyle. +-----+ 4.0.0 com.mycompany mycompany-checkstyle-checks MyCompany Checkstyle Checks 1.0 checkstyle checkstyle ${project.version} +-----+ ** <<>> This file lets you specify the names of the packages that you want to be able to use. Here we have added <<>> to the standard set of Checkstyle packages. That means that you can use your custom checks in the Checkstyle configuration file, without having to specify their package name. +-----+ +-----+ ** <<>> Here is the example check from the Checkstyle site. It checks that your source files don't have more that 30 methods. +-----+ package com.mycompany.checks; import com.puppycrawl.tools.checkstyle.api.*; public class MethodLimitCheck extends Check { private int max = 30; public int[] getDefaultTokens() { return new int[]{TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF}; } public void visitToken(DetailAST ast) { // find the OBJBLOCK node below the CLASS_DEF/INTERFACE_DEF DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK); // count the number of direct children of the OBJBLOCK // that are METHOD_DEFS int methodDefs = objBlock.getChildCount(TokenTypes.METHOD_DEF); // report error if limit is reached if (methodDefs > max) { log(ast.getLineNo(), "too many methods, only " + max + " are allowed"); } } } +-----+ * Building a JAR for your custom checks project To be able to use your custom checks in other projects, you need to package and install them. To do that, just run this on the command line: +-----+ mvn install +-----+ This produces a JAR file with the following contents and installs it into your local repository. ** <<>> +-----+ mycompany-checkstyle-checks-1.0.jar |-- pom.xml |-- META-INF | |-- MANIFEST.MF | `-- maven | `-- com.mycompany | `-- mycompany-checkstyle-checks | |-- pom.xml | `-- pom.properties `-- com `-- mycompany `-- checks |-- packagenames.xml `-- MethodLimitCheck.class +-----+ * Using your checks in another project Now you are ready to make use of your custom checks in another project. ** Create a Checkstyle configuration Create the file <<>> in the root of the project that wants to use your custom checks. In this file you tell Checkstyle which checks you want to use. <> We don't have to specify the fully qualified classname of our check here. That's because we used the <<>> file earlier. +-----+ +-----+ ** Configure the Checkstyle Plugin to use your custom checks Finally we need to tell the other project that you want it to use your custom Checkstyle checks. In the <<>> of that project, add the following configuration. <> You have to specify a plugin dependency on <<>> in the <<<\>>> element of your <<>>. It will not work inside the <<<\>>> element, because <<<\>>> does not support plugin dependencies. The rest of the configuration is done in the normal way in the <<<\>>> element. +-----+ ... org.apache.maven.plugins maven-checkstyle-plugin ${project.version} com.mycompany mycompany-checkstyle-checks 1.0 org.apache.maven.plugins maven-checkstyle-plugin ${project.version} checkstyle.xml ... +-----+