Performing Code Analysis from the Command Line

You can perform code analysis from the command line using Apache Ant.

Properties for code analysis in the .cobolBuild script

Visual COBOL uses the information stored in an Apache Ant script, .cobolBuild, to build COBOL projects.

.cobolBuild is part of your project. It is stored in the project's directory alongside the .cobolProj file and, in Eclipse, you can access it if you explore the project in the Navigator view.

The script is automatically regenerated when when you change the project's properties or rebuild the project file, .cobolProj.

Enabling code analysis for a project adds the following elements to the .cobolBuild script:

  • An Ant task, analysis.

    The task requires the FailOnError attribute that specifies whether the build fails or continues when code analysis returns any results.

    This also requires the ruleList type with rule nested elements which specify the rules to execute. If you have enabled rules that are imported into the IDE from a non-default location, the details about these rules are added to the ruleList.custom type.

    The mffilelist type specifies the COBOL files to run code analysis on.

  • The analysisData and analysisDataDir parameters in the cobol Ant task.
  • The analyze and build.and.analyze Ant targets.

    You must specify one of these targets when performing code analysis at the command line:

    • analyze - specifies that only code analysis is to be carried out on the source files.
    • build.and.analyze - specifies that both an analysis and a build are to be carried out on the source files.
  • Code analysis searches for rules in the product installation director. The folder to look for is added in the rulesDir tag.

    If you have imported additional rules into the IDE from a different folder, that folder is stored in the rulesDir.custom tag.

You can explore these parameters if you create a native COBOL project, enable code analysis for it from the project's properties, and open the .cobolBuild file in the editor.

Syntax of the Ant command for code analysis

You can perform code analysis of your source files at the command line using Apache Ant and the .cobolBuild script. The following is an overview of the process:

  1. You must have Apache Ant installed on your machine. Download Ant from http://ant.apache.org/.
  2. In Eclipse, enable and configure code analysis from your project's properties.
  3. Start a Visual COBOL command prompt - click Start > All Programs > Micro Focus Visual COBOL > Tools > Visual COBOL Command Prompt.
  4. At the command prompt, navigate to the project's directory.
  5. Execute the following command to set the CLASSPATH environment variable:
    CLASSPATH=product-install-dir\bin\mfant.jar;%CLASSPATH%

    For example, if Visual COBOL is installed in the default location:

    CLASSPATH=%ProgramFiles(x86)%\Micro Focus\Visual COBOL\bin\mfant.jar;%CLASSPATH%
  6. Execute the following command to perform code analysis on the project using the .cobolBuild script and an Ant target for code analysis:
    Ant-install-dir\bin\ant -f .cobolBuild Ant-target -DanalysisFailOnError=value

    Where:

    • Ant-install-dir is the Ant installation directory. You can add Ant-dir\bin to the PATH environment variable to avoid having to specify the full path to the Ant executable.
    • Ant-target is one of the targets enabling code analysis adds to the project's .cobolBuild script:
      analyze
      Ant only analyzes the sources.
      build.and.analyze
      Ant builds the project, creates a build output, and then analyzes the source files.
    • FailOnError takes a value of either true or false:
      true
      Specifies that analyzing (or bulding and analyzing) the code should stop if running any of the analysis rules returns results.
      false
      Specifies that analyzing (or bulding and analyzing) the code should continue if running any of the analysis rules returns results.

    For example:

    c:\tools\antdir\bin\ant -f .cobolBuild build.and.analyze -DanalysisFailOnError=true

Customizing the .cobolBuild script

When you change the settings for code analysis in the project's properties, this changes the .cobolBuild script file. Alternatively, you can open the .cobolBuild script in the editor and make any changes to the code analysis settings manually (though, these changes are reverted if the IDE rebuilds the project).

You can use the .cobolBuild script file from outside of the workspace to perform code analysis, provided that the script and the source files are stored in the same location. This can be useful if you want to integrate your code analysis tasks into a continuous integration (CI) framework or when you store your applications outside of the workspace - for example, in a version control system such as Subversion.

In this case, you can manually modify the file to create more complex code analysis targets. You would not be able to edit the script if it is opened as part of a project as the IDE rebuilds the script each time it rebuilds the project.

See the section, Properties for Code Analysis in the .cobolBuild script, in this topic for the .cobolBuild script parameters that must be present. Ensure that the rulesDir and rulesDir.custom tags point to the locations of the files that contain the code analysis rules to execute.

Results from a code analysis job

The results of running code analysis from the command line are shown in the command prompt window alongside all the build messages.

The messages have the following format:

[Filename and full path] [LocationInFile (start line, column, end line, column)] : [ErrorMessage (error/warning + output code)] :  [RuleSetName] :  [Rule] :  [Code snippet the location refers to]

For example:

C:\Tutorials\Projects\MyProject\MyProgram.cbl (14,8,14,33): warning MFCA0001 : Within Entire Program : Dead Statements : display "Some Message"