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

AWS SAM构建Spring Boot 3应用失败:checkstyle插件配置解析错误

解决AWS SAM Build中Maven Checkstyle/PMD插件的配置解析错误

问题现象

执行aws sam build构建基于JDK 21的Spring Boot 3应用时,抛出以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.3.1:check (default) on project xxxxx: Unable to parse configuration of mojo org.apache.maven.plugins:maven-checkstyle-plugin:3.3.1:check: Basic element 'includes' must not contain child elements -> [Help 1]

直接执行mvn clean package可正常完成构建,注释maven-pmd-plugin和maven-checkstyle-plugin的配置后,SAM Build也能运行,但需要保留代码检查功能。

问题原因

你使用的maven-checkstyle-plugin:3.3.1版本中,<includes>参数要求配置为逗号分隔的字符串,而非嵌套的<include>子元素列表。虽然直接mvn命令兼容这种写法,但AWS SAM Build的Maven执行环境对配置格式解析更严格,因此触发错误。

解决方案

方案1:修改includes配置为字符串形式

将两个插件中的<includes>块替换为单个字符串值:

修改后的PMD插件配置:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-pmd-plugin</artifactId>
  <version>3.22.0</version>
  <configuration>
    <targetJdk>21</targetJdk>
    <encoding>UTF-8</encoding>
    <consoleOutput>true</consoleOutput>
    <failOnViolation>false</failOnViolation>
    <printFailingErrors>true</printFailingErrors>
    <linkXRef>false</linkXRef>
    <includes>**/*.java</includes>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>check</goal>
        <goal>cpd-check</goal>
      </goals>
    </execution>
  </executions>
</plugin>

修改后的Checkstyle插件配置:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>3.3.1</version>
  <configuration>
    <configLocation>checkstyle.xml</configLocation>
    <encoding>UTF-8</encoding>
    <consoleOutput>true</consoleOutput>
    <failOnViolation>true</failOnViolation>
    <violationSeverity>warning</violationSeverity>
    <linkXRef>false</linkXRef>
    <includes>**/*.java</includes>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>check</goal>
      </goals>
    </execution>
  </executions>
</plugin>

方案2:升级Checkstyle插件版本

较新的maven-checkstyle-plugin版本(如3.4.0及以上)支持使用<include>子元素列表配置includes,升级后可保留原有嵌套格式:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>3.4.0</version>
  <configuration>
    <configLocation>checkstyle.xml</configLocation>
    <encoding>UTF-8</encoding>
    <consoleOutput>true</consoleOutput>
    <failOnViolation>true</failOnViolation>
    <violationSeverity>warning</violationSeverity>
    <linkXRef>false</linkXRef>
    <includes>
      <include>**/*.java</include>
    </includes>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>check</goal>
      </goals>
    </execution>
  </executions>
</plugin>

两种方案均可解决SAM Build的配置解析错误,同时保留PMD和Checkstyle的代码检查功能。

内容的提问来源于stack exchange,提问作者Anoop Varma

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 07:00:14