我使用XJC在運行時從xsd生成JAXB類。但默認情況下,xsd中的maxLength
限制不會被註釋。如何在運行時以編程方式添加xjc插件?
我發現了一個插件來處理這個問題,krasa-jaxb-tools。我已經添加了依賴到我的POM,但我似乎無法將該插件添加到XJC過程。
我使用的是jaxb-xjc工具的2.2.11版本。這裏是我的依賴關係:
<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>
我試圖實例化插件,配置它,並把它傳遞給generateCode(Plugins[] extensions, ErrorListener errorListener)
方法對我S2JJAXBModel
模型,但它似乎沒有任何效果。這裏是我的代碼:
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);
我在做什麼錯?