简体   繁体   English

如何让 cxf-xjc-plugin 在 utf-8 中生成源代码

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

I try to generate java classes from xsd in a maven project using cxf-xjc-plugin.我尝试使用 cxf-xjc-plugin 在 maven 项目中从 xsd 生成 java 类。

It runs fine, but the generated source files get platform specific encoding (cp1251 on a windows pc) instead of utf-8.它运行良好,但生成的源文件获得了特定于平台的编码(Windows pc 上的 cp1251)而不是 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 .如果任何 xsd 类型在模式注释中包含非拉丁字符,则它们仅在该特定编码中变得可读,并且编译器稍后会抱怨[WARNING] /C:/.../SomeType.java:[17,4] unmappable character for encoding UTF-8

Please help me force utf-8 for sources generation.请帮我强制使用utf-8来生成源代码。

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.在阅读了 xjc 的一些旧问题,特别是CXF-4369JAXB-499之后,我尝试使用 maven 项目属性<file.encoding>utf-8</file.encoding>强制编码并在运行mvn -Dfile.encoding=utf-8 clean install ,但一无所获。

I could make cxf-xjc-plugin generate sources in UTF-8 by adding the following entry to the xsdOption element:通过将以下条目添加到 xsdOption 元素,我可以使 cxf-xjc-plugin 以 UTF-8 生成源代码:

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

使用 JVM 参数-Dfile.encoding=UTF-8做了一个有点重的技巧,但对我们来说很好:-/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM