how to make cxf-xjc-plugin generate sources in utf-8

孤者浪人 提交于 2019-12-19 05:56:23

问题


I try to generate java classes from xsd in a maven project using cxf-xjc-plugin.

It runs fine, but the generated source files get platform specific encoding (cp1251 on a windows pc) instead of utf-8. If any xsd types contain non-latin characters in schema annotations, then they become readable only in that specific encoding and the compiler later complains with [WARNING] /C:/.../SomeType.java:[17,4] unmappable character for encoding UTF-8.

Please help me force utf-8 for sources generation.

The source encoding is set with

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

The build plugin is set up like that:

<build>
    ...
    <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-xjc-plugin</artifactId>
        <version>2.7.0</version>
        <configuration>
            <extensions>
                <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:2.7.0</extension>
                <extension>net.java.dev.jaxb2-commons:jaxb-fluent-api:2.1.8</extension>
            </extensions>
        </configuration>
        <executions>
            <execution>
                <id>generate-sources</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>xsdtojava</goal>
                </goals>
                <configuration>
                    <sourceRoot>${basedir}/target/generated-sources</sourceRoot>
                    <xsdOptions>
                        <xsdOption>
                            <extension>true</extension>
                            <xsd>${basedir}/src/main/resources/schemas/Policy.xsd</xsd>
                            <bindingFile>${basedir}/src/main/resources/schemas/Policy.xjb</bindingFile>
                            <extensionArgs>
                                <extensionArg>-Xdv</extensionArg>
                                <extensionArg>-Xfluent-api</extensionArg>
                            </extensionArgs>
                        </xsdOption>
                    </xsdOptions>
                </configuration>
            </execution>
        </executions>
    </plugin>
...

after having read through some older issues with xjc, particularly CXF-4369 and JAXB-499 I tried to force encoding with maven project property <file.encoding>utf-8</file.encoding> and set a system property while running mvn -Dfile.encoding=utf-8 clean install, but got nowhere.


回答1:


I could make cxf-xjc-plugin generate sources in UTF-8 by adding the following entry to the xsdOption element:

<extensionArgs>
    <arg>-encoding</arg>
    <arg>UTF-8</arg>
</extensionArgs>


来源:https://stackoverflow.com/questions/22777386/how-to-make-cxf-xjc-plugin-generate-sources-in-utf-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!