java静态代码检测-checkstyle

要想使用checkstyle,需要在代码仓库中做一些配置

 

1. 在maven项目的pom.xml文件中,加入checkstyle的依赖包:

Upgrading Checkstyle reference:  https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/upgrading-checkstyle.html

<project>
  ...
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-checkstyle-plugin</artifactId>
          <version>3.4.0</version>
          <dependencies>
            <dependency>
              <groupId>com.puppycrawl.tools</groupId>
              <artifactId>checkstyle</artifactId>
              <version>...choose your version...</version>
            </dependency>
          </dependencies>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  ...
</project>

2.通过命令执行,来生成report结果文件

reference url: https://checkstyle.sourceforge.io/cmdline.html

例如:

 java -Xss1m -Xms768m -Xmx768m -XX:NewSize=80m -XX:MaxNewSize=80m -XX:MetaspaceSize=160m -XX:MaxMetaspaceSize=160m -jar /home1/user/jenkins/ci_tools/ci/libs/checkstyle-7.8.2-all.jar -c my-checkstyle-rules.xml -f xml -o target/checkstyle-result.xml ./

官方文档: 

java -D<property>=<value>  \
     com.puppycrawl.tools.checkstyle.Main \
     -c <configurationFile> \
     [-f <format>] [-p <propertiesFile>] [-o <file>] \
     [-s <line:column>] [-g | --generate-xpath-suppression] [-w | --tabWidth <length>] \
     [-t | --tree] [-T | --treeWithComments] [-J | --treeWithJavadoc] [-j | --javadocTree] \
     [-V | --version] [-b | --branch-matching-xpath <xpathQuery>] [-h | --help] \
     [-e | --exclude <excludedPath>] [-E | --executeIgnoredModules] [-d | --debug] \
     [-x | --exclude-regexp <excludedPathPattern>] \
     file(s) or folder(s) ...
        

ps: my-checkstyle-rules.xml 可以是自定义的规则文件,也可以是官网有的2种规则文件:  sun_checks.xml 和 google_checks.xml

 

3.在jenkins中安装checkstyle 插件

4.在job中配置checkstyle 的执行命令(步骤2),并且勾选结果报告插件

 

posted @ 2024-07-25 18:04  苹果芒  阅读(53)  评论(0)    收藏  举报