初始设置

用于 Ant 的 Jtest 插件以 JAR 文件形式随 Jtest 一起提供,位于以下位置:[INSTALL_DIR]/integration/ant。通过以下方式之一将 jtest-ant-plugin.jar 添加到构建 classpath:

  • 将文件部署到 Ant 的 libs 目录中: $HOME/.ant/lib
  • 将文件部署到 Ant 的系统安装 lib 目录中:$ANT_HOME/lib
    注意:如果您的 IDE、持续集成(CI)软件或使用的其他工具采用嵌入式 Ant 安装方式,则会忽略 $ANT_HOME/lib 目录,此时您需要使用 $HOME/.ant/lib 目录来添加该 JAR 文件。

  • 每次构建和分析项目时,在 Ant 命令行中添加以下参数:

    -lib $JTEST_HOME/integration/ant/jtest-ant-plugin.jar
  • 在根构建脚本中指定 taskdef 任务:

    <project name="project" default="sth" xmlns:jtest="antlib:com.parasoft.jtest.plugin.ant">
     
      <taskdef uri="antlib:com.parasoft.jtest.plugin.ant"
      
       resource="com/parasoft/jtest/plugin/ant/antlib.xml"
     
       classpath="PATH/TO/JTEST/integration/ant/jtest-ant-plugin.jar"/>
     
      <!-- ... -->
    
    </project>

配置 Jtest 执行

您可以在外部 XML 构建文件、常规 Ant XML 构建文件或命令行中配置 Jtest 执行——使用专用选项来指定相应设置即可。 

以下示例假设 jtest-ant-plugin.jar 已部署到 Ant 的 lib 目录之一,因此未使用命令行参数 -lib。

build.xml 文件中配置分析

您可以通过修改 Ant 的 build.xml 文件进行分析配置:

<project name="Analyzed Project" default="build"
    xmlns:jtest="antlib:com.parasoft.jtest.plugin.ant">
  <!-- ... -->
  <jtest:listener />
  <!-- ... -->
  <target name="jtest-analysis" depends="build">
    <jtest:jtest>
       <!-- jtest task configuration -->
    </jtest:jtest>
  </target>
  <!-- ... -->
  <target name="build">
    <!-- analyzed project build target -->
  </target>
</project>  

执行分析

如果在 build.xml 文件中进行分析配置,则使用以下命令执行分析:

ant jtest-analysis

在外部 XML 构建文件中进行分析配置

您可以在外部 jtest.xml 构建文件中进行分析配置。

<project name="Analyzed Project" default="build"
    xmlns:jtest="antlib:com.parasoft.jtest.plugin.ant">
  <property name="jtest.home" value="PATH/TO/JTEST" />
  <jtest:listener />
  <target name="jtest-analysis" depends="build">
    <jtest:jtest>
      <config>builtin://Recommended Rules</config>
      <home>PATH/TO/JTEST</home>
      <resource>Analyzed Project/src/**/*.java</resource>
      <report>report</report>
      <publish>true</publish>
    </jtest:jtest>
  </target>
  <!-- ... -->
  <target name="build">
    <!-- ant task makes subproject to inherit properties (including jtest.home) 
         which may be used by instrument and instrument-test tasks -->
    <ant target="build" dir="${basedir}" antfile="build.xml" />
  </target>
</project>

执行分析

如果在 jtest.xml 文件中进行分析配置,则必须使用 -file 选项添加到命令行中,否则会在分析时使用默认的 Ant build.xml 文件。使用以下命令执行分析:

ant jtest-analysis

层级结构

上面的示例展示了以优先级递减的顺序配置 home 参数的三种方式:

  1. 直接在 task 标签中配置。
  2. 使用项目属性(property 任务)。
  3. 使用 JVM 环境变量配置。您可以使用 -D 命令行选项配置 Jtest 选项,例如 -Djtest.home=value。
    注意:Ant 内置选项的优先级高于 Jtest 选项。如果使用 Ant 选项进行配置,将会覆盖 Jtest-D 传递的值。

在 XML 构建文件中进行高级配置

以下配置将加载 jtest-ant-plugin.jar 并通过构建文件启动监听器。因此:

  • Ant 命令行不需要任何额外选项即可执行 Jtest 分析
  • 不需要使用 -lib 选项传递 jtest-ant-plugin.jar,也不需要将该文件部署到 $ANT_HOME/lib 目录中
<project name="Analyzed Project" default="jtest-analysis"
        xmlns:jtest="antlib:com.parasoft.jtest.plugin.ant">
  <!-- ... -->
  <property name="jtest.home" value="PATH/TO/JTEST" />
  <taskdef uri="antlib:com.parasoft.jtest.plugin.ant"
    resource="com/parasoft/jtest/plugin/ant/antlib.xml"
    classpath="${jtest.home}/integration/ant/jtest-ant-plugin.jar"/>
  <!-- listener has to be specified after taskdef -->
  <jtest:listener />
  <!-- ... -->
  <target name="build">
    <!-- analyzed project build target -->
  </target>
  <!-- ... -->
  <target name="jtest-analysis" depends="build">
    <jtest:jtest/>
  </target>
</project>  

在命令行中配置分析

在命令行中使用 -Dproperty 选项(-Dproperty.KEY=VALUE)可以传递各项设置来进行分析配置。例如:

ant jtest-analysis -Dproperty.console.verbosity.level=high

手动自定义编译数据

在极少数情况下,高度自定义的 Ant 构建可能会导致 Ant 的 Jtest 插件自动检测到的编译数据中出现缺口或错误。Jtest 会在生成报告的设置问题部分以及控制台输出报告此类问题。如果经核实,检测到的编译数据中包含错误,则可以扩展或覆盖被分析项目的 build.xml 文件中的数据。

以下示例展示了一个被分析的 Ant 构建的自定义编译数据。该配置通过附加的二进制路径元素进行扩展。

默认情况下,Ant 没有编译 id。当项目多次触发 javac 任务时,您必须为 javac 定义一个 id 以匹配自定义编译数据。未指定 id 的自定义编译数据配置将应用于所有通过 javac 任务收集的、未指定 id 参数的编译数据。

<project name="Jtest Analysis" default="jtest-analysis"
    xmlns:jtest="antlib:com.parasoft.jtest.plugin.ant">
  <!-- properties etc. -->
  
  <target name="build">
    <javac id="main_compilation" srcdir="${src.dir}" 
      destdir="${target.dir}" source="1.5" />
    <!-- copying additional jar into target.dir which cannot be detected by plugin -->
  </target>
    
  <target name="jtest-analysis" depends="build">
    <jtest:jtest>
      <config>builtin://Recommended Rules</config>
      <home>${env.JTEST_HOME}</home>
      <compilation>
        <!-- will apply to every compilation data if ID not specified -->
        <id>main_compilation</id>
        <classpath>
            <!-- filesets can be used in classpath and bootpath -->
            <fileset dir="C:/some_directory/lib">
                <include name="**/*.jar"/>
            </fileset>
            <path>some_jar_file.jar</path>
        </classpath>
        <binarypath>
          <path>${additional.jar.file}</path>
          <path>${other.jar.file}</path>
        </binarypath>
      </compilation>
    </jtest:jtest>
  </target>  
</project>  

(info) 如需修改所有项目的编译数据,可以使用 -Djtest.dataUpdate 命令行选项。

有关 Jtest 插件自动检测编译数据自定义设置的更多信息,请参阅编译数据模型

  • No labels