初始设置
要将 Parasoft Jtest 与 Maven 集成,您需要在以下 settings.xml
文件之一修改 Maven 设置:
$M2_HOME/conf/settings.xml
- 位于 Maven 安装目录中的全局设置$HOME/.m2/settings.xml
- 位于用户主目录.m2
文件夹中的用户设置
如果两个文件都存在,用户指定的设置将覆盖全局设置。
您可以将 Maven 安装目录中的全局设置复制到 $HOME/.m2
目录,将其用作模板,并根据以下说明进行修改。
某些工具,如持续集成(CI)服务器和 IDE,可能自带嵌入式 Maven,因此只依赖用户设置,而忽略全局设置。在此类情况下,需确保用户设置包含 Jtest 集成所需的所有修改。
在您的 settings.xml
文件中配置以下设置:
<pluginRepository>
- 指定 Jtest 自带的本地 Maven 资源库的路径,位于 [INSTALL_DIR]\integration\maven<pluginGroups>
- 指定适当的 groupId、artifactId 和 goal 以简化通过命令行调用插件jtest.home
属性 - 指定 Jtest 安装目录的路径,以简化通过命令行调用插件
您的 settings.xml 文件可能与以下内容类似:
<settings> <!-- ... --> <pluginGroups> <!-- ... --> <pluginGroup>com.parasoft.jtest</pluginGroup> <pluginGroup>com.parasoft.jtest.tia</pluginGroup> </pluginGroups> <profiles> <!-- ... --> <profile> <id>jtest-settings-profile</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <jtest.home>PATH/TO/JTEST</jtest.home> </properties> <pluginRepositories> <pluginRepository> <id>jtest-local</id> <url>file://${jtest.home}/integration/maven</url> </pluginRepository> </pluginRepositories> </profile> </profiles> <mirrors> <!-- ... --> <!-- prevention against mirrors with wildcard (*) matching --> <mirror> <id>jtest-local-mirror</id> <mirrorOf>jtest-local</mirrorOf> <!-- mirrors tag does not support properties --> <url>file://PATH/TO/JTEST/integration/maven</url> <!-- properties are not resolved in this tag --> </mirror> </mirrors> </settings>
配置 Jtest 执行
有几种方法可以配置 Jtest 分析和测试代码的方式:
- 通过直接在项目的
pom.xml
文件中配置插件参数 - 在随
pom.xml
文件的<settings>
或<settingsList>
插件参数提供的.properties
文件中配置 - 使用 Maven 命令行选项
-Djtest
或-Dproperty
配置 - 在 Jtest 安装目录或主目录的
jtestcli.properties
文件中配置(请参阅配置概述)
我们建议直接在命令行或 POM 文件中配置 jtest
目标执行。
层级结构
如果使用 Maven 配置分析并将用户属性作为变量(${...}
)提供,将应用以下层次结构:
-Djtest.[Maven 属性名]
(例如,-Djtest.settings="my.general.properties"
)。pom.xml
文件-Dproperty.[属性名]
(例如,-Dproperty.
dtp.url=https://server1.mycompany.com:8443
)
如果属性是硬编码的(即在 pom.xml
中以实际值提供时),则具有优先级且无法被命令行设置覆盖。这将产生以下层次结构:
pom.xml
文件-Djtest.[Maven 属性名]
(例如,-Djtest.settings="my.general.properties"
)。-Dproperty.[属性名]
(例如,-Dproperty.
dtp.url=https://server1.mycompany.com:8443
)
例如,pom.xml
中作为用户属性 <config>${jtest.config}</config>
指定的测试配置可以被 -Djtest.config
覆盖。如果它被硬编码为 <config>builtin://Demo Configuration</config>
,则不能被覆盖。
jtestcli.properties
文件中提供的设置始终具有最低的优先级。
在 POM 文件中配置分析
您可以通过直接在项目的 pom.xml
文件中提供适当的插件参数来配置分析。有关配置参数列表的信息,请参阅 Maven 的 Jtest 目标参考。
pom.xml
文件中的插件配置可能与以下内容类似:
<project> <!-- ... --> <build> <!-- ... --> <plugins> <!-- ... --> <plugin> <groupId>com.parasoft.jtest</groupId> <artifactId>jtest-maven-plugin</artifactId> <version>2020.1.0</version> <configuration> <settings>settings.properties</settings> <config>builtin://Recommended Rules</config> <resources> <resource>**/my/package/**/*.java</resource> <resource>**/*.xml</resource> </resources> <exclude>**/test/**</exclude> <report>report_dir</report> <publish>true</publish> </configuration> </plugin> </plugins> </build> </project>
该示例中显示的所有属性都是可选的。
禁用继承
默认情况下,Jtest 在为根项目生成报告时对每个项目(模块)执行分析。因此,需确保在 POM 中包含 <inherited>false</inherited>
标签,以防止对每个嵌套模块进行冗余分析。
在命令行中配置分析
通过 -D
命令行选项传递适当的设置,可以直接在 Maven 命令行中配置分析:
-Djtest.[设置]
可用于配置执行 Maven 的 Jtest 插件专用的设置;请参阅 Maven 的 Jtest 目标参考。
示例:mvn jtest:jtest -Djtest.config="builtin://Critical Rules"
-Dproperty.[设置]
可用于配置以 KEY=VALUE 形式提供的任意 Jtest 设置(请参阅配置设置)。
示例:mvn jtest:jtest -Dproperty.console.vebosity.level=high
有关应用这些设置的结构顺序的信息,请参阅层次结构。
示例
示例 1. 以下命令行使用默认的 builtin://Recommended Rules
测试配置分析被分析构建中的每个模块:
mvn jtest:jtest
示例 2. 以下分析使用 jtest.settingsList
选项传递的两个 .properties 文件进行配置。测试配置和分析范围分别通过 jtest.config
和 jtest.resource
传递。Jtest 将分析“my.groupId:my-artifacId”项目中匹配“/src/main/java/my/package/**/*.java”模式的文件。
mvn jtest:jtest -Djtest.config="builtin://Critical Rules" -Djtest.resource="my.groupId:my-artifacId/src/main/java/my/package/**/*.java" -Djtest.settingsList="project.specific.properties","/etc/jtest/license.properties"
示例 3. 以下分析使用 jtest.settings
选项传递的 .properties 文件进行配置。测试配置使用 jtest.config
传递。property.console.verbosity
命令行选项覆盖了 .properties 文件中指定的 console.verbosity
选项。
mvn jtest:jtest -Djtest.config="builtin://Recommended Rules" -Djtest.settings="my.general.properties" -Dproperty.console.verbosity.level=high
手动自定义 POM 文件中的编译数据
在极少数情况下,大量执行编译的构建插件和高度自定义的 Maven 构建可能会导致 Maven 的 Jtest 插件自动检测到的编译数据中出现缺口或错误。Jtest 会在生成报告的设置问题部分报告此类问题。
如果核实检测到的编译数据中包含错误,则可以扩展或覆盖被分析模块的 POM 文件中的数据。
以下示例展示了一个被分析的 Maven 子模块的自定义编译数据。该配置通过一个额外的 classpath 元素进行扩展,并为模块指定一个新的源级别。由于未指定编译 id,因此将使用默认编译 id(default-compile 是执行 maven-compiler-plugin 的默认 id)。
<project> <!-- ... --> <build> <!-- ... --> <plugins> <!-- ... configuration with non default compiler plugin appending ${extra-classpath-element} and changing sourcelevel which cannot be detected automatically --> <plugin> <groupId>com.parasoft.jtest</groupId> <artifactId>jtest-maven-plugin</artifactId> <version>2020.1.0</version> <configuration> <compilation> <!-- no id specified so Maven default: "default-compile" id will be used --> <classpath> <path>${extra-classpath-element}</path> </classpath> <sourcelevel>1.6</sourcelevel> </compilation> </configuration> </plugin> </plugins> </build> </project>
如需修改所有项目的编译数据,可以使用
-Djtest.dataUpdate
命令行选项。
有关 Jtest 插件自动检测编译数据自定义设置的更多信息,请参阅编译数据模型。