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

2

14

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.

Heterolecithal answered 1/4, 2014 at 5:17 Comment(1)
UPDATE: directly extending MAVEN_OPTS=-Dfile.encoding=UTF-8 allowed generation of source files with proper UTF-8 encoding, but turned the output of unit tests to garbage (because windows console is not really utf-8). I'm sure there is a proper way to force the plugin respect source encoding without enforcing changing platform encoding for other components.Heterolecithal
M
13

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>
Murther answered 26/11, 2014 at 18:8 Comment(2)
did not help with xjc.getOptions().parseArgument( new String[] { "-encoding" , "UTF-8" } , 0 ) using jaxb-xjc-2.3.5.jarHorbal
For those who need to configure it within build.gradle the equivalent form in XJC task call is xjc(destdir: sourcesDir) {schema(...) arg(value: "-wsdl") arg(value: "-encoding") arg(value: "UTF-8") produces(...)}Croissant
H
0

using JVM param -Dfile.encoding=UTF-8 did the trick which is a bit heavy, but fine for us :-/

Horbal answered 13/5, 2022 at 20:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.