【问题标题】:Generating Java classes from XSD maven dependency in maven with JAXB (or others)使用 JAXB(或其他)在 maven 中从 XSD maven 依赖项生成 Java 类
【发布时间】:2015-10-01 12:58:13
【问题描述】:

我有一个数据模型作为 XSD 文件存储在 Maven 存储库中。我的目标是创建一个包含代表此模型的所有 Java 类的 jar。我想使用 maven 和 JAXB 来做到这一点。 我知道 maven-jaxb2-plugin (codehouse-mojo 和 java-net,尚不确定它们有何不同),但我没有看到使用 maven 依赖项中的 XSD 作为输入的方法。我是否必须编写自己的插件才能做到这一点?

如果有更好的工具,它不一定是 JAXB。

【问题讨论】:

    标签: java maven xsd jaxb


    【解决方案1】:

    免责声明:我是maven-jaxb2-plugin 的作者。

    检查documentation,它就在那里。见Specifying What To Compile - Specifying URLs, filesets and Maven artifact resources

    例子:

    <configuration>                                                                                         
        <schemas>                                                                                       
            <!--
                Compiles a schema which resides
                in another Maven artifact.
            -->
            <schema>                                                                                
                <dependencyResource>                                                            
                    <groupId>org.jvnet.jaxb2.maven2</groupId>                               
                    <artifactId>maven-jaxb2-plugin-tests-po</artifactId>                    
                    <!-- Can be defined in project dependencies or dependency management -->
                    <version>${project.version}</version>                                   
                    <resource>purchaseorder.xsd</resource>                                  
                </dependencyResource>                                                           
            </schema>                                                                                
        </schemas>                                                                                      
    </configuration>
    

    您还可以使用 catalogs 将架构 URL 重写为 Maven artifact resources

    例子:

    REWRITE_SYSTEM "http://schemas.opengis.net" "maven:org.jvnet.ogc:ogc-schemas:jar::!/ogc"
    

    这会将 URI http://schemas.opengis.net/ows/2.0/owsAll.xsd 重写为 maven:org.jvnet.ogc:ogc-schemas:jar::!/ogc/ows/2.0/owsAll.xsd。这将引用ogc-schemas JAR 工件中的ogc/ows/2.0/owsAll.xsd 资源。

    据我所知,这些功能是 maven-jaxb2-plugin 独有的。

    【讨论】:

    • 谢谢词典。它工作正常。我不得不改变我打包我的 XSD 文件的方式,因为我只是把它放在带有 XSD 打包的 repo 中,我无法访问 XSD 文件(未知协议:maven)。现在我有一个带有 jar 包装的标准 maven 项目,一切都按预期工作。
    猜你喜欢
    • 1970-01-01
    • 2014-07-01
    • 2015-06-08
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 2020-12-01
    • 1970-01-01
    相关资源
    最近更新 更多