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

如何在Spring Boot Repackage插件阶段纳入JAXB生成的源码?

我来帮你解决这个问题——你之前的配置问题出在includes标签的用法上,它需要的是类路径的通配符模式,而不是单个类名。另外,得先确保JAXB生成的类被Maven正确编译,才能被打包进最终的Jar里。

下面是分步的解决方案:

1. 先确保JAXB生成的类被编译

首先要让Maven把target/generated-sources/xjc下的Java文件编译成.class文件,有两种方式:

方式一:配置maven-compiler-plugin添加编译源目录

如果你的项目没有用专门的JAXB插件自动管理生成目录,直接给maven-compiler-plugin加上这个目录:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.11.0</version>
    <configuration>
        <source>17</source> <!-- 替换成你的JDK版本 -->
        <target>17</target>
        <includes>
            <include>**/*.java</include>
            <include>${project.build.directory}/generated-sources/xjc/**/*.java</include>
        </includes>
    </configuration>
</plugin>

方式二:用maven-jaxb2-plugin自动管理生成目录

如果你用的是maven-jaxb2-plugin生成JAXB类,只需要开启addCompileSourceRoot,它会自动把生成目录加入Maven的编译源:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.15.3</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <schemaDirectory>src/main/resources/schemas</schemaDirectory> <!-- 你的XSD文件位置 -->
        <packageName>my.com.generated</packageName> <!-- 生成类的包路径 -->
        <generateDirectory>${project.build.directory}/generated-sources/xjc</generateDirectory>
        <addCompileSourceRoot>true</addCompileSourceRoot> <!-- 关键:自动加入编译源 -->
    </configuration>
</plugin>

2. 配置spring-boot-maven-plugin正确打包

现在修改你的spring-boot-maven-plugin配置,用通配符模式指定要包含的类,同时排除不需要的资源文件:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.artifactId}/exec</outputDirectory>
                <!-- 包含你的业务类和JAXB生成的类,用通配符匹配整个包 -->
                <includes>
                    <include>my/com/**/*.class</include> <!-- 你的业务类包 -->
                    <include>my/com/generated/**/*.class</include> <!-- JAXB生成类的包,如果和业务类同包可以省略 -->
                </includes>
                <!-- 排除不需要的资源文件 -->
                <excludes>
                    <exclude>**/*.properties</exclude>
                    <exclude>**/*.xml</exclude>
                    <exclude>**/*.yml</exclude>
                    <!-- 按需添加其他要排除的文件类型 -->
                </excludes>
            </configuration>
        </execution>
    </executions>
</plugin>

关键说明

  • 之前的<include>my.com.class</include>是错误的,Maven的路径匹配用斜杠/分隔包名,**表示任意子目录,*.class匹配所有类文件。
  • 必须确保JAXB生成的.java文件被编译成.class文件,否则打包时找不到这些类。
  • 通过excludes标签明确排除properties、yml等资源,就能保证最终Jar只包含你需要的类文件。

运行mvn clean package之后,你在${project.artifactId}/exec下得到的Jar就只会包含你的业务类和JAXB生成的类了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 07:31:27