一年半之前,为了做Java代码的圈复杂度度量,研究了一下JavaNCSS,修改了它的源码,把覆盖率数据存入数据库,结合代码覆盖率数据,给出最迫切需要改进的代码列表(覆盖率低,且圈复杂度最高的代码)。时间有点久远,稍微记录一下。内容很简单,没有什么技术含量,仅仅是为了记录一下,供有需要的同学参考。 

    JavaNCSS官网:http://www.kclee.de/clemens/java/javancss/
     一般而言,常规使用(命令行或者ant任务)更为合理。 如果你想将它作为一个组件库来使用,那么需要直接修改它的源码。
    搭建工程的过程挺简单的,从官网下载源码以及相关依赖的jar包。新建Java工程,把源码拷贝到src目录下,然后,新建lib文件夹,把相关的依赖jar包拷贝到lib文件夹下,最后设置好Java依赖即可。
    为了方便在本地调测JavaNCSS,且不依赖运行环境配置,我是直接偷懒了,直接修改JavaNCSS的Main.java的源码。

原来的代码:
public static void main(String[] asArgs) throws IOException {
                Locale.setDefault( Locale.US );

                Javancss pJavancss = new Javancss(asArgs);

                 if (pJavancss.getLastErrorMessage() != null) {
                        System.exit(1);
                }

                System.exit(0);
        }

修改后的代码:
public static void main(String[] asArgs) throws IOException {
          asArgs = new String[] { "-recursive", "D:\\tcc3\\3.0\\trunk\\test-javancss\\test", /*"-out", "result.txt",*/ "-all"};
          
                Locale.setDefault( Locale.US );

                Javancss pJavancss = new Javancss(asArgs);

                 if (pJavancss.getLastErrorMessage() != null) {
                        System.exit(1);
                }

                System.exit(0);
        }

这样,在文件夹 D:\\tcc3\\3.0\\trunk\\test-javancss\\test 下放置测试文件,然后右键运行该程序即可得到JavaNCSS的结果文件。

怎么知道有哪些命令行参数啊?最直接的方法是查看官网。我这里也拷贝一下(请以官网为准):
Usage

Make sure javancss-30.51/lib/javancss.jar, javancss-30.51/lib/ccl.jar, and javancss30.51/lib/ jhbasic.jar are added to your CLASSPATH.
Then to start JavaNCSS type: java javancss.Main. As an alternative you can edit and use either the JAVANCSS.BAT or javancss file. Just change the JAVA_HOME and CLASSPATH variables according to your system. Make sure you did include the swingall.jar archive in the classpath.

Now for the first run type in (beeing in the javancss-30.51 directory itself):

./bin/javancss -gui test/*.java

or

./bin/javancss -gui -recursive test

For regression test of JavaNCSS use:

java javancss.test.JavancssTest

If no parameter is provided for JavaNCSS, standard input (stdin) is used as the input stream. Multiple java source files can be specified in the command line. If a '@' char is put in front of a file name, then not this file will be measured but its content will be interpreted as a list of Java source files that shall be counted. The '@' functionality can be used recursively inside this file as well. Wild cards are not supported yet. (If the operating system processes the command line for the program, then you are lucky. Windows doesn't do that.) Instead use something like cat *.java | javancss or type *.java | javancss. Of course, this can lead to ambiguities when mixing source files that belong to a package with files that doesn't.

If no option is given, JavaNCSS only calculates the total non commenting source statements (NCSS) of the given input.

JavaNCSS can also be used conveniently via Ant. For details have a look at the JavaNCSS Ant Task page, especially at the example at the bottom of that page. You can also have a look at the build.xml file that gets distributed with JavaNCSS itself (see "javancss" target).

Synopsis

javancss [-option] stdin | [@]source_file*
Options

-ncss
This is the default which counts total non commenting source statements and nothing else.
-package
Collects the metrics data for each package. This is the most top level view javancss offers for your projects. Take a look here what javancss prints out for the Sun JDK 1.1.5 java.* source tree.
-object
Collects the metrics data for each class/interface. For an example program output, look here.
-function
Collects the metrics data for each function. For an example program output, look here.
-all
The same as '-package -object -function'.
-gui
Opens a gui to presents the '-all' output in tabbed panels.
-xml
Output in xml and not in ascii format. Additional option '-all' is recommended.
-out file
Output goes normally to standard output, with this option an output file can be specified.
-recursive
Java file in sub directories will be parsed as well. Be careful not to get caught in an endless loops because some Unix links.
-check
Trigger JavaNCSS self test suite.
-version
Prints out the version of JavaNCSS.
-help
Prints out some basic information.