Generated sources by JAXB and problem with encoding

I recently had to solve the problem with JAXB to generate bad source from XSD. The problem was basically that the XSD source from which they were subsequently generated Java code contained in the tag Czech diacritics characters, and unfortunately, the output files were not generated in the UTF-8, but the native format of operating system of my laptop, which is MacCentralEurope.

What hapened when I then started building my code with Maven, I obviously – I had got exception that the generated files contain non-UTF characters. So I started investigation how to force the JAXB to generate source code in UTF-8 instead of using platform specific encoding.

A quick look into the source code JAXB again assured me that JAXB is not (politely spoken) the best written piece of software because there is something like this:

private final CharsetEncoder encoder = EncoderFactory.createEncoder(bw.getEncoding());

This piece of code always returns encoder by platform depend encoding and there is no way, how to force different encoding and only one way is to tell JVM wat encoding is the right one.

Fortunately, the solution is easy. Just set the system variable MAVEN_OPTS like this:

export MAVEN_OPTS="$MAVEN_OPTS -Dfile.encoding=UTF-8"

What surprised me that this issue is not documented, and even Google could not help me. So I hope these few lines will help to someone.

4 odpovědi na “Generated sources by JAXB and problem with encoding”

  1. I’re right – Project Micros was in many aspects a good school for all of us …

  2. Hi Milos, I am afraid you’re wrong. Tag sourceEncoding in maven doesn’t override system property file encoding so that can’t work.

    I’ve tried your approach but without success

Napsat komentář