I am generating JAXB classes from xsd at runtime using XJC. But by default, the maxLength
constraint in xsd is not annotated.
I found a plugin to handle this, krasa-jaxb-tools . I have added dependencies to my POM, but I cannot add a plugin to the XJC process.
I am using jaxb-xjc tools version 2.2.11. Here are my dependencies:
<dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-xjc</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-jxc</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>com.github.krasa</groupId> <artifactId>krasa-jaxb-tools</artifactId> <version>1.3</version> </dependency>
I am trying to create a plugin instance by setting it up and passing it to the generateCode(Plugins[] extensions, ErrorListener errorListener)
in my S2JJAXBModel
, but it does not seem to have any effect. Here is my code:
SchemaCompiler schemaCompiler = XJC.createSchemaCompiler(); schemaCompiler.forcePackageName(packageRoot); // JAXB Plugin used to get the proper @Size annotations on all fields. JaxbValidationsPlugins jaxbValidationPlugin = new JaxbValidationsPlugins(); // Build up list of options for the plugin. // First option must be the name of the plugin itself. // Options must be prefixed with dashes String[] args = new String[] { "-" + JaxbValidationsPlugins.PLUGIN_OPTION_NAME }; try { // Activate plugin jaxbValidationPlugin.parseArgument(new Options(), args, 0); } catch (BadCommandLineException | IOException e1) { e1.printStackTrace(); } InputSource inputSource = new InputSource(schemaFile.toURI().toString()); schemaCompiler.parseSchema(inputSource); S2JJAXBModel model = schemaCompiler.bind(); // Passing the plugin to the method JCodeModel jCodeModel = model.generateCode(new Plugin[] {jaxbValidationPlugin}, null);
What am I doing wrong?
java xsd jaxb xjc
Sid Oct 26 '15 at 21:05 2015-10-26 21:05
source share