搜索

2.3. 创建第一个 YAML 规则

download PDF

本节介绍了创建和测试第一个基于 MTA YAML 的规则的过程。这假设您已安装了 MTA。有关安装说明 请参阅 CLI 指南中的 安装和运行 CLI。

在本例中,您将创建一个规则来发现应用程序定义了包含 < class-loading> 元素的 jboss- web.xml 文件,并提供描述如何迁移代码的文档的链接。

2.3.1. 为规则创建 YAML 文件

为您的第一个规则创建 YAML 文件。

$ mkdir /home/<USER>/rule.yaml

2.3.2. 创建数据来测试规则

  1. 在目录中创建 jboss-web.xmlpom.xml 文件:

    mkdir /home/<USER>/data/
    touch /home/<USER>/data/jboss-web.xml
    touch /home/<USER>/data/pom.xml
  2. 在您创建的 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>
  3. 在您创建的 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)

流程

  1. 在您创建的 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 块指定一个或多个条件:
    1. 使用 内置 提供程序的 XML 功能,因为此规则在 XML 文件中检查匹配项。
    2. 要在作为 jboss -web 的子类加载 元素上匹配,请使用 XPath 表达式 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. 安装规则

流程

  1. 将 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. 查看报告

查看报告以确保它提供了预期的结果。

流程

  1. 分析完成后,命令会输出 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

  2. 导航到左侧菜单中的 问题 选项卡。
  3. 验证规则是否已执行:

    1. issues 表中,在搜索栏中输入 JBoss XML
    2. 验证表中是否存在标题 Find class loading element in JBoss XML 文件中的 问题。
  4. jboss-web.xml 链接打开受影响的文件。
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.