You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenRewrite: AddCommentToMavenDependency Recipe匹配POM失败求助

修正OpenRewrite AddCommentToMavenDependency的XPath配置问题

问题描述

我尝试使用OpenRewrite的AddCommentToMavenDependency Recipe在依赖声明内添加注释行,但推测XPath配置不正确,请求帮忙修正。

当前配置

recipeList:
  - org.openrewrite.maven.AddCommentToMavenDependency:
      xPath: /project/dependencies/dependency
      groupId: com.myorg.exe
      artifactId: exe-starter-parent
      commentText: This is excluded due to CVE <X> and will be removed when we upgrade the next version is available.

对应的POM文件内容

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.myorg.exe</groupId>
        <artifactId>exe-starter-parent</artifactId>
        <version>5.0.24</version>
        <relativePath />
    </parent>
    <groupId>com.myorg.exe.samples</groupId>
    <artifactId>gooseberry-webclient-rest</artifactId>
    <version>${app_version}</version>
    <name>gooseberry-webclient-rest</name>
    <description>A sample project demonstrating the service kernel usage for a REST API Server</description>
    <properties>
        <snakeyaml.version>2.0</snakeyaml.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.zaxxer</groupId>
                    <artifactId>HikariCP</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</project>

问题分析

你配置的XPath /project/dependencies/dependency 用于匹配<dependencies>节点下的依赖项,但你指定的groupId和artifactId对应的是POM中的<parent>节点,两者匹配范围不对应,导致无法找到目标节点添加注释。

修正方案

将XPath修改为指向<parent>节点即可,修正后的配置如下:

recipeList:
  - org.openrewrite.maven.AddCommentToMavenDependency:
      xPath: /project/parent
      groupId: com.myorg.exe
      artifactId: exe-starter-parent
      commentText: This is excluded due to CVE &lt;X&gt; and will be removed when we upgrade the next version is available.

调整后OpenRewrite就能准确定位到<parent>节点,为其添加指定注释内容。

内容的提问来源于stack exchange,提问作者manish kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 17:01:25