jaxb2-maven-plugin 生成 xsd 字符集问题

该博客主要讨论了在使用`jaxb2-maven-plugin`插件时遇到的XML Schema生成字符集不是UTF-8的问题。通过分析,作者指出需要修改插件源码`org.codehaus.mojo.jaxb2.schemageneration.XsdGeneratorHelper`中的`savePrettyPrintedDocument`方法,将文件写入部分改为指定UTF-8编码。为了解决此问题,作者建议克隆并修改`jaxb2-maven-plugin`的GitHub仓库源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<plugin>

                <groupId>org.codehaus.mojo</groupId>

                <artifactId>jaxb2-maven-plugin</artifactId>

                <version>2.5.1</version>

                <executions>

                    <!--

                    <execution>

                        <id>xjc-scores</id>

                        <goals>

                            <goal>xjc</goal>

                        </goals>

                        <configuration>

                            <packageName>com.generated.scores</packageName>

                            <schemaDirectory>src/main/resources/schemas/scores</schemaDirectory>

                        </configuration>

                    </execution>

                    <execution>

                        <id>xjc-videos-ramp</id>

                        <goals>

                            <goal>xjc</goal>

                        </goals>

                        <configuration>

                            <packageName>com.generated.ramp</packageName>

                            <schemaDirectory>src/main/resources/schemas/ramp</schemaDirectory>

                            <clearOutputDir>false</clearOutputDir>

                        </configuration>

                    </execution>

                    <execution>

                        <id>xjc-schedules</id>

                        <goals>

                            <goal>xjc</goal>

                        </goals>

                        <configuration>

                            <packageName>com.generated.schedules</packageName>

                            <schemaDirectory>src/main/resources/schemas/schedules</schemaDirectory>

                            <clearOutputDir>false</clearOutputDir>

                        </configuration>

                    </execution>

                    -->

                    <execution>

                        <id>schemagen</id>

                        <goals>

                            <goal>schemagen</goal>

                        </goals>

                        <phase>process-classes</phase>

                        <configuration>

                            <createJavaDocAnnotations>true</createJavaDocAnnotations>

                            <clearOutputDir>true</clearOutputDir>

                            <schemaSourceExcludeFilters>

                                <noJaxbIndex implementation="org.codehaus.mojo.jaxb2.shared.filters.pattern.PatternFileFilter">

                                    <patterns>

                                        <pattern>com.riversoft.weixin.*</pattern>

                                        <pattern>com.icss.regie.weixin.beans.commons.fault.*</pattern>

                                        <pattern>com.icss.regie.weixin.gateways.*</pattern>

                                        <pattern>com.icss.regie.weixin.impl.*</pattern>

                                        <pattern>com.icss.regie.weixin.services.*</pattern>

                                    </patterns>

                                </noJaxbIndex>

                            </schemaSourceExcludeFilters>

                            <outputDirectory>${basedir}/src/main/resources/wsdl</outputDirectory>

                        </configuration>

 

 

生成的代码 中 不是 utf-8 字符集

 

需要 clone 代码 https://github.com/mojohaus/jaxb2-maven-plugin 修改 

 

 

org.codehaus.mojo.jaxb2.schemageneration.XsdGeneratorHelper 中的 

 

savePrettyPrintedDocument 方法

 

out = new BufferedWriter(new FileWriter(targetFile));

改为

out = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (targetFile,true),"UTF-8"));

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值