How to make generated classes contain Javadoc from XML Schema documentation

前端 未结 4 1424
太阳男子
太阳男子 2020-12-01 06:01

I\'m currently working with an XML Schema that has / on most types and elements. When I generate Jav

4条回答
  •  孤街浪徒
    2020-12-01 06:51

    Especially for that case I wrote XJC plugin xjc-documentation-annotation-plugin.

    What it does: -> Java class annotations

    Said we have this object described in XSD:

    
        
            Cadastral quarter
        
        
            
                
                    Cadastral number
                
            
    
    

    We run xjc like:

    xjc -npa -no-header -d src/main/generated-java/ -p xsd.generated scheme.xsd
    

    And got class like (getters, setters and any annotations omitted for simplicity):

    public class CadastralBlock {
        protected String number;
    }
    

    But in my case I want known how to class and fields was named in source file! So it what this plugin do!

    So you get:

    @XsdInfo(name = "Cadastral quarter", xsdElementPart = "\n  \n    \n      \n        \n      \n  ")
    public class CadastralBlock {
        @XsdInfo(name = "Cadastral number")
        protected String number;
    }
    

    How to use

    Manual call in commandline

    If you want run it manually ensure jar class with plugin in run classpath and just add option -XPluginDescriptionAnnotation. F.e.:

    xjc -npa -no-header -d src/main/generated-java/ -p xsd.generated -XPluginDescriptionAnnotation scheme.xsd
    

    Call from Java/Groovy

    Driver.run(
        [
            '-XPluginDescriptionAnnotation'
            ,'-d', generatedClassesDir.absolutePath
            ,'-p', 'info.hubbitus.generated.test'
            ,'CadastralBlock.xsd'
        ] as String[]
        ,new XJCListener() {...}
    )
    

    See test XJCPluginDescriptionAnnotationTest for example.

    Use from Gradle

    With gradle-xjc-plugin:

    plugins {
        id 'java'
        id 'org.unbroken-dome.xjc' version '1.4.1' // https://github.com/unbroken-dome/gradle-xjc-plugin
    }
    
    ...
    
    dependencies {
        xjcClasspath 'info.hubbitus:xjc-documentation-annotation-plugin:1.0'
    }
    
    // Results by default in `build/xjc/generated-sources`
    xjcGenerate {
        source = fileTree('src/main/resources') { include '*.xsd' }
        packageLevelAnnotations = false
        targetPackage = 'info.hubbitus.xjc.plugin.example'
        extraArgs = [ '-XPluginDescriptionAnnotation' ]
    }
    

    Complete gradle example in example-project-gradle directory of project.

提交回复
热议问题