Code Detection Toolbox--checkstyle, FindBugs, PMD-CPD

Source: Internet
Author: User
Tags documentation mkdir xsl checkstyle

It's written in front.

OK, I admit, I am lazy, wrote a day of documents, here to paste directly, I hope you can understand, if you need a complete format of the document, please go to my Baidu library download, address is:

Http://wenku.baidu.com/view/d2849ff04693daef5ef73d34.html

Below, start one. Objective:

Recently to track the code quality of a project to ensure the quality of the project. The individual always thinks that the quality assurance this thing should use the data to speak. Code walking is an aspect, but it's a good way to add some of the bugs by using the tools to do some of the standardized code. Combine a few open source tools that you've used and heard from before, and implement a simple tool that fits your current project.

two. Choice:

The first is the choice of tools, the use of multiple tools combined with the current level and status of project team members, to determine from several aspects of the inspection:

General Bugs

Coding specification

Duplicate code

Do not want to check too much, previously did not carry out this aspect of the work, whether the leadership support, the project members are good response is difficult to say, if we all like to improve more inspection is not too late.

In fact, a dependent detection, but now the project is using the SSH architecture, the existing dependency checking tool, the support for spring is not very good, and then give up.

For the choice of tools, tight is familiar with the choice of Checkstyle, FindBugs and PMD CPD tools.

three. Expected results:

Use the ant script to do all the work in one step and directly generate a check report for the project.

Four. Steps: 1. Preparation Tools

Ant, Checkstyle, findbugs, PMD downloads. Also download CVs and Eclipse, because the script needs to update the code with CVS, and Eclipse makes the configuration of the ant script easier to edit and environment.

2. Script Planning

Planning scripts, the entire script is divided into several sections, as follows:

<?xml version= "1.0" encoding= "Utf-8"?>

<project name= "MyProject" default= "Start" >

<!--variable definition-->

<!--Environment Initialization-->

<!--checkout-->

<!--Javac-->

<!--jar-->

<!--Checkstyle-->

<!--findbugs-->

<!--PMD CPD-->

<!--run-->

<target name= "Start" >

</target>

</project>

The entire script plan describes the process of executing the script, and each comment adds concrete content. The system performs the start task, which calls other tasks by relying on dependencies, and the process is defined by dependencies between tasks.

L first define variables in "variable definition" to facilitate reuse of multiple projects in the future.

I initialize the environment in "Environment initialization", where all environmental cleanup and environment creation is done.

L "Checkout" to complete CVS update work.

Because FindBugs needs to check the binary code, "Javac" completes the code compilation work.

L also for FindBugs needs, "jar" packs the code.

L "Checkstyle" completes the code spell check.

L "findbugs" complete bug check

L "PMD CPD" uses the PMD CPD feature to check for duplicate code.

3.checkout

The variable definition and the Environment Initialization section are added whenever needed, first in the checkout process, and the code is checked out of the CVS library. 3.1 Script Authoring

Checkout part of the script is as follows:

<target name= "Checkoutlib" depends= "MkDir" >

<cvs cvsroot= "${cvsroot}" package= "Workingarea/code/lib"/>

</target>

<target name= "checkoutsrc" depends= "Checkoutlib" >

<cvs cvsroot= "${cvsroot}" package= "Workingarea/code/project"/>

</target>

Checkoutlib Check out the common library, checkoutsrc check out the code, and define the Cvsroot variable in the variable definition section as follows:

<property name= "Cvsroot" value= ":p server:wangjianxuan:password@scm1.domain.com:/repository/project"/>

The text section is the same as the CVS we used to use, and we can refer to the part of the CVS view in Eclipse.

Note: The mkdir tasks that are dependent on the above script are initialized and are described in detail in order to establish the relevant directory.

3.2 Execution

It's good to execute ant in Eclipse, and what's missing is not the need to modify the environment variables, which is resolved within eclipse. There are several places to confirm before execution:

Right-click on script-"Run as-" Ant build ...

Note that there must be an ellipsis menu, this menu can configure the environment, open the following image of the interface:

Here targets can select the task to perform, and execution will be performed with the dependent task. Classpath defines the dependent class library, the JRE is used to define the version of the JRE, the environment tag is important to define variables that are not defined in some environment variables, and here I have not found the CVS path, but I can find CVS in the command line. So the PATH environment variable is defined here, as shown in the following figure:

After the script executes, the code is checked out to the "Workingarea/code/project" location under the engineering directory.

Note: Settings for content such as environment variables and classpath are lost after changing the ant script name and need to be reset.

3.3 Problem Handling

Ant calls CVs online article less, the problem is less, perhaps it is too easy, but I still encountered some problems.

First, if no CVS path is added to the path, or if you do not have CVS installed (Windows system needs to install cvsnt), the CVS path cannot be found and an error is detected. So ensure that any path down in the command line with the "CVS" command can be successful, if also reported wrong, you can add the "path" environment variable to eclipse.

In addition, according to Ant's CVS task instructions, the CVS password should use the Cvspass task to generate the Cvspass file, which is invoked using the Cvspass tag in the CVS task. But what I do in eclipse does not produce files, and CVS checkout reports that access is denied (clear, no password). The solution is to add the password to the Cvsroot attribute, and after the username, add ": Password" before the "@" sign.

4. Javac

The JAVAC task compiles the code that is just checked out, and the compiled code is placed in the bin directory. 4.1 Script

The javac part of the script is as follows:

<target name= "Javac" depends= "CHECKOUTSRC" >

<javac srcdir= "${srcdir}" destdir= "${builddir}" source= "1.6" >

<compilerarg value= "-xlint:unchecked"/>

</javac>

</target>

Where depends specifies that the script is just checked out so that the script performs the checkout before the compilation is performed. SOURCE Specifies the version of JDK to be used at compile time, where 1.6 is specified. Because of the need for code compilation, a compile parameter "-xlint:unchecked" is specified, which, depending on the actual situation, can be specified for multiple use and corresponding to the command-line compilation.

Two variables are defined in the Variable definition section: "Srcdir" specifies the source code path, and "BuildDir" specifies the compiled code path. The script is as follows:

<property name= "Projdir" value= "D:/work/quality_workspace/antscript"/>

<property name= "Srcdir" value= "${projdir}/workingarea/code/cginfo/src/main"/>

<property name= "BuildDir" value= "${projdir}/workingarea/code/cginfo/bin"/>

The Srcdir and BuildDir variables are based on the current project directory, and therefore a variable "projdir" declaration project directory is defined.

4.2 Execution

The simple execution of the Javac script may cause problems that require classpath to contain the classpath. Again, the script right-"Run as-" Ant build ... Open the window to select the Classpath page sign, as follows:

Here to note that

1. The button to add a jar package is valid only if the "User Entries" node is selected.

2. I do not know is not the problem that I use, add folders add a directory, you can not add all the jar packages to the classpath to the directory, you can only put all the jar package here.

After the compilation is packed, see the next section. 5.jar

As with Javac, the Javac compiled code is packaged with 5.1 scripts due to findbugs needs

<target name= "jar" depends= "Javac" >

<jar destfile= "${packagedir}/app.jar" basedir= "${builddir}"/>

</target>

The script destfile specifies the compiled jar package path and filename, basedir specifies the path to the original code.

Here the variable packagedir is defined in the variable definition section to specify the path to the jar package, as follows:

<property name= "Package" value= "Workingarea/code/commonmakemanage"/>

After execution, the jar pack is found in the DestFile location. 6.checkstyle

Finally get to the point, the preparation is completed, you can start checking, first of all, Checkstyle check and generate reports. 6.1 Script

Check the script as follows:

<target name= "Checkstyle" depends= "jar" >

<taskdef resource= "Checkstyletask.properties"/>

<checkstyle config= "Lib/check_rules.xml" >

<formatter type= "xml" tofile= "${checkstyledir}/checkstyle_report.xml"/>

<fileset dir= "${srcdir}" includes= "**/*.java"/>

</checkstyle>

<style in= "${checkstyledir}/checkstyle_report.xml" out= ${checkstyledir}/checkstyle_report.html "style=" lib/ Checkstyle-frames.xsl "/>

</target>

The script is a bit long, explained one by one:

First, taskdef defines the Checkstyle ant task and declares the property file, which requires a checkstyle jar package in the Ant's Classpath, where I use Checkstyle-5.4-all.jar.

Below, Checkstyle defines the various aspects of the check, "config" defines the check rules, which can be written in custom, and then explained.

The formatter child element defines the output format and the location of the output file, where the XML format is defined and the "checkstyledir" variable is defined, representing the Checkstyle report directory. The definition of the specific variable is described later.

The Fileset child element defines the source code location, dir specifies the source code directory, includes specifies the file type, or you can use excludes to exclude some files, as detailed in the Checkstyle documentation.

Generate XML report files after execution, use the style tag combined with the Checkstyle XSL files in the XML to generate HTML, easy to see, Checkstyle brought a lot of XSL files, try to see which is appropriate.

Note: This is a frame style, generated HTML, sometimes generated in the "${checkstyledir}" at the same level directory to generate the files directory, the directory contains part of the check results.

6.2 Variable Definitions

The script is as follows:

<property name= "Checkstyledir" value= "${projdir}/checkstyle"/>

6.3 Environment Initialization Script

Because the previous check result is deleted before the code is checked, the results are guaranteed to be up to date. At the same time to ensure that the file directory exists, otherwise write report times wrong. So there are the following scripts:

    <target name= "Deldir" >

        <delete dir= "${builddir}"/>

       <delete dir= "${" Checkstyledir} "/>

       <delete dir=" ${findbugsdir} "/>

       <delete dir= "${cpddir}"/>

    </target>

    <target name= "MkDir" depends= "Deldir" >

       <mkdir dir= "${builddir}"/>

       <mkdir dir= "${checkstyledir}"/>

       <mkdir dir= "${findbugsdir}"/>

       <mkdir dir= "${cpddir}"/>

    </target>

Here are all the scripts that need to be deleted and created, including the compiled directory BuildDir, the code check results directory checkstyledir;bug The result directory Findbugsdir and the code duplicate check directory Cpddir.

6.3 Execution

After the script is executed, the Checkstyle_report.xml and checkstyle_report.html file 6.4 Custom rules are established in the Checkstyledir directory

Checkstyle rules can be customized, detailed rules can refer to the Checkstyle document, here I only need to check a few rules, from the Checkstyle rule to remove unnecessary rules, to retain part of the need. The rules are as follows:

<?xml version= "1.0"?>

<! DOCTYPE Module Public

"-//puppy crawl//dtd Check Configuration 1.3//en"

"Http://www.puppycrawl.com/dtds/configuration_1_3.dtd" >

<!--

Checkstyle configuration that checks the sun coding conventions from:

-The Java Language specification at

Http://java.sun.com/docs/books/jls/second_edition/html/index.html

-The Sun Code conventions at http://java.sun.com/docs/codeconv/

-The Javadoc guidelines at

Http://java.sun.com/j2se/javadoc/writingdoccomments/index.html

-The JDK Api documentation http://java.sun.com/j2se/docs/api/index.html

-Some best practices

Checkstyle is very configurable. Being sure to read the documentation at

Http://checkstyle.sf.net (or in your downloaded distribution).

Most Checks are configurable, is sure to consult the documentation.

To completely disable a check, just comment it out or deletes it from the file.

Finally, it is worth reading the documentation.

-->

<module name= "Checker" >

<!--

If you set the Basedir property below, then all reported file

Names is relative to the specified directory. The

Http://checkstyle.sourceforge.net/5.x/config.html#Checker

<property name= "Basedir" value= "${basedir}"/>

-->

<!--Checks that property files contain the same keys. -->

<!--http://checkstyle.sf.net/config_misc.html#Translation-->

<module name= "translation"/>

<module name= "Treewalker" >

<!--Checks for Javadoc comments. -->

<!--http://checkstyle.sf.net/config_javadoc.html-->

<module name= "Javadocmethod"/>

<module name= "Javadoctype"/>

<module name= "Javadocvariable"/>

<!--Checks for naming conventions. -->

<!--http://checkstyle.sf.net/config_naming.html-->

<module name= "Constantname"/>

<module name= "Localfinalvariablename"/>

<module name= "LocalVariableName"/>

<module name= "MemberName"/>

<module name= "methodname"/>

<module name= "PackageName"/>

<module name= "ParameterName"/>

<module name= "Staticvariablename"/>

<module name= "TypeName"/>

<!--Checks for Headers-->

<!--http://checkstyle.sf.net/config_header.html-->

<!--<module name= "Header" >-->

<!--The Follow property value demonstrates the ability-->

<!--to have access to ANT properties. In the case it uses-->

<!--the ${basedir} allow Checkstyle to is run-->

<!--from any directory within a project. -->

<!--expansion,-->

<!--http://checkstyle.sf.net/config.html#properties-->

<!--<property-->

            <!--     name= Headerfile "                          &nbs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.