2.3. 创建第一个 YAML 规则
本节介绍了创建和测试第一个基于 MTA YAML 的规则的过程。这假设您已安装了 MTA。有关安装说明 , 请参阅 CLI 指南中的 安装和运行 CLI。
在本例中,您将创建一个规则来发现应用程序定义了包含 < class-loading> 元素的
文件,并提供描述如何迁移代码的文档的链接。
jboss-
web.xml
2.3.1. 为规则创建 YAML 文件
- 为您的第一个规则创建一个 YAML 文件。
$ mkdir /home/<USER>/rule.yaml
2.3.2. 创建数据来测试规则
在目录中创建
jboss-web.xml
和pom.xml
文件:mkdir /home/<USER>/data/ touch /home/<USER>/data/jboss-web.xml touch /home/<USER>/data/pom.xml
在您创建的
jboss-web.xml
文件中,粘贴以下内容:<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 4.2//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd"> <jboss-web> <class-loading java2ClassLoadingCompliance="false"> <loader-repository> seam.jboss.org:loader=@projectName@ <loader-repository-config>java2ParentDelegation=false</loader-repository-config> </loader-repository> </class-loading> </jboss-web>
在您创建的
pom.xml
文件中,粘贴以下内容:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test</groupId> <artifactId>test</artifactId> <version>1.1.0-SNAPSHOT</version> <properties> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> </dependencies> </project>
2.3.3. 创建规则
基于 MTA YAML 的规则使用以下规则模式:
when(condition) perform(action)
流程
在您创建的
rule.yaml
文件中,粘贴以下内容:- ruleID: <UNIQUE_RULE_ID> 1 description: <DESCRIPTION> 2 when: <CONDITION(S)> 3 message: <MESSAGE> 4 labels: <LABELS> 5 effort: <EFFORT> 6 links: - <LINKS> 7
- 1
- 规则的唯一 ID。例如,
jboss5-web-class-loading
。 - 2
- 规则的文本描述。
- 3
- 完成
when
块指定一个或多个条件:-
使用
内置
提供程序的 XML 功能,因为此规则检查 XML 文件中的匹配项。 要在作为
jboss
元素上匹配,请使用 XPath 表达式-web
的子类加载jboss-web/web-loading
作为 XML 查询。在这种情况下,您只需要一个条件:when: builtin.xml: xpath: jboss-web/class-loading
-
使用
- 4
- 解释迁移问题的有用消息。当规则匹配时,报告中会生成该消息。例如:
message: The class-loading element is no longer valid in the jboss-web.xml file.
- 5
- 规则的字符串标签列表。
- 6
- 解决这个问题的预期故事数。
- 7
- 一个或多个超链接指向您找到的迁移问题的文档。
links: - url: https://access.redhat.com/documentation/zh-CN/JBoss_Enterprise_Application_Platform/6.4/html-single/Migration_Guide/index.html#Create_or_Modify_Files_That_Control_Class_Loading_in_JBoss_Enterprise_Application_Platform_6 title: Create or Modify Files That Control Class Loading in JBoss EAP 6
该规则现已完成,并类似如下:
- ruleID: jboss5-web-class-loading description: Find class loading element in JBoss XML file. when: builtin.xml: xpath: jboss-web/class-loading message: The class-loading element is no longer valid in the jboss-web.xml file. effort: 3 links: - url: https://access.redhat.com/documentation/zh-CN/JBoss_Enterprise_Application_Platform/6.4/html-single/Migration_Guide/index.html#Create_or_Modify_Files_That_Control_Class_Loading_in_JBoss_Enterprise_Application_Platform_6 title: Create or Modify Files That Control Class Loading in JBoss EAP 6
2.3.4. 安装规则
流程
将 CLI 指向您创建的规则文件:
–rules /home/<USER>/rules.yaml
2.3.5. 测试规则
流程
要测试规则,请将输入指向您创建的测试数据,并使用 MTA CLI 中的 rules 选项传递规则:
mta-cli analyze --input /home/<USER>/data/ --output /home/<USER>/output/ --rules /home/<USER>/rules.yaml
2.3.6. 查看报告
查看报告以确保它提供了预期的结果。
流程
分析完成后,命令会输出 HTML 报告的路径:
INFO[0066] Static report created. Access it at this URL: URL="file:/home/<USER>/output/static-report/index.html"
在 Web 浏览器中打开
/home/<USER_NAME>/output/static-report/index.html
。- 进入到左侧菜单中的 issues 选项卡。
验证规则是否已执行:
-
在 issues 表中,在搜索栏中输入
JBoss XML
。 -
验证表中是否存在 title
Find class loading 元素
的问题。
-
在 issues 表中,在搜索栏中输入
- 单击 jboss-web.xml 链接,以打开受影响的文件。