Plugin extends functionality of artifact
<dependency>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
</dependency>
It processes all java.math.BigDecimal
fields in classes generated by XJC and initializes them using provided customizations.
-
Add this plugin to plugins section of maven-jaxb2-plugin in your pom.xml.
-
Enable it by providing
-Xinit-bigdecimal-fields
arg -
Add customizations for
xs:decimal
fields in your*.xsd
file- Add namespace used by plugin
xmlns:bd="http://init.bigdecimal"
- Enable processing of it's customizations
jaxb:extensionBindingPrefixes="bd"
Example of
*.xsd
header:<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:extensionBindingPrefixes="bd" jaxb:version="2.1" xmlns:bd="http://init.bigdecimal">
- Add init-field customizations.
Example:
<xs:element name="amount" type="xs:decimal"> <xs:annotation> <xs:appinfo> <bd:setStaticValue>ZERO</bd:setStaticValue> <bd:executeMethod> <bd:name>setScale</bd:name> <bd:param type="int">2</bd:param> <bd:param type="static" class="java.math.BigDecimal">ROUND_HALF_UP</bd:param> </bd:executeMethod> </xs:appinfo> </xs:annotation> </xs:element>
- Add namespace used by plugin
Customizations are defined in xmlns:bd="http://init.bigdecimal"
namespace and processed in the order, they are written in *.xsd
.
-
<bd:setStaticValue>
- initializes field with static value fromjava.math.BigDecimal
class.
For example, this customization:<xs:element name="amount" type="xs:decimal"> <xs:annotation> <xs:appinfo> <bd:setStaticValue>ZERO</bd:setStaticValue> </xs:appinfo> </xs:annotation> </xs:element>
will generate the following code:
BigDecimal amount = BigDecimal.ZERO;
-
<bd:executeMethod>
- executes method on previously initialized value, so it must be placed after<setStaticValue>
tag.<name>
- method name<param type="" class="">
- parameter that will be passed in method
Attributetype
accepts values:int
- param will be qualified as javaint
static
- param will be qualified as static field in class defined in additional attributeclass
Attributeclass
is required only withtype="static"
.
For example, this customization:
<xs:element name="amount" type="xs:decimal"> <xs:annotation> <xs:appinfo> <bd:setStaticValue>ZERO</bd:setStaticValue> <bd:executeMethod> <bd:name>setScale</bd:name> <bd:param type="int">2</bd:param> <bd:param type="static" class="java.math.BigDecimal">ROUND_HALF_UP</bd:param> </bd:executeMethod> </xs:appinfo> </xs:annotation> </xs:element>
will generate the following code:
BigDecimal amount = BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP);
Below configuration assumes:
*.xsd
source files placed in/src/main/resources/foo
- generated classes will be placed in
${project.build.directory}/generated-sources/xjc-foo
- classes will be placed in package
com.foo.bar.package
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaLanguage>AUTODETECT</schemaLanguage>
<episode>false</episode>
<generatePackage>com.foo.bar.package</generatePackage>
<generateDirectory>${project.build.directory}/generated-sources/xjc-foo</generateDirectory>
<schemaDirectory>${basedir}/src/main/resources/foo</schemaDirectory>
<schemaIncludes>
<include>**/*.xsd</include>
</schemaIncludes>
</configuration>
</execution>
</executions>
<configuration>
<args>
<arg>-Xinit-bigdecimal-fields</arg>
</args>
<plugins>
<plugin>
<groupId>jaxb2-init-bigdecimal-fields</groupId>
<artifactId>jaxb2-init-bigdecimal-fields</artifactId>
<version>1.0.2</version>
</plugin>
</plugins>
</configuration>
</plugin>