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

jaxb2-maven-plugin生成XSD时如何关闭递归目录遍历

解决jaxb2-maven-plugin schemagen递归处理子包的问题

针对你遇到的schemagen目标递归遍历子目录并尝试处理未注解类的问题,有两种实用的解决方案,推荐优先使用第二种,更贴合Java包结构的处理逻辑:

方法1:通过includes/excludes过滤类文件

你可以在插件配置中添加<includes>和<excludes>规则,精准指定只处理目标目录下的直接类文件,排除所有子目录中的类。

修改后的配置示例:

<execution>
  <id>rest-api-execution-schemagen</id>
  <goals>
    <goal>schemagen</goal>
  </goals>
  <phase>generate-resources</phase>
  <configuration>
    <sources>
      <source>src/java/foo/rest/execution/model</source>
    </sources>
    <!-- 只包含当前目录下的.java文件 -->
    <includes>
      <include>*.java</include>
    </includes>
    <!-- 排除所有子目录下的.java文件 -->
    <excludes>
      <exclude>**/*.java</exclude>
    </excludes>
    <outputDirectory>${project.build.directory}/execution-api-xml-schema</outputDirectory>
  </configuration>
</execution>

这里的<include>*.java</include>会只匹配model目录下的直接Java类,而<exclude>**/*.java</exclude>会排除所有子目录中的类(**代表任意层级的子目录)。

方法2:指定packageName替代source目录(推荐)

相比基于文件目录的过滤,直接指定要处理的Java包名更精准,因为schemagen会严格只处理该包下的类,不会递归子包。

修改后的配置示例:

<execution>
  <id>rest-api-execution-schemagen</id>
  <goals>
    <goal>schemagen</goal>
  </goals>
  <phase>generate-resources</phase>
  <configuration>
    <!-- 直接指定目标包名,替代source目录配置 -->
    <packageName>foo.rest.execution.model</packageName>
    <outputDirectory>${project.build.directory}/execution-api-xml-schema</outputDirectory>
  </configuration>
</execution>

这种方式完全不需要担心子包的问题,插件会自动扫描指定包下的所有带JAXB注解的类,忽略子包中的内容,更符合Java项目的包组织习惯,也避免了文件目录路径可能带来的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:00:33