简体   繁体   English

使用 JAXB(或其他)从 maven 中的 XSD maven 依赖项生成 Java 类

[英]Generating Java classes from XSD maven dependency in maven with JAXB (or others)

I have a data model stored in the Maven repo as an XSD file.我有一个数据模型作为 XSD 文件存储在 Maven 存储库中。 My goal is to create a jar with all the Java classes representing this model.我的目标是创建一个包含代表此模型的所有 Java 类的 jar。 I want to use maven and JAXB to do this.我想使用 maven 和 JAXB 来做到这一点。 I know about the maven-jaxb2-plugin (codehouse-mojo and java-net, not sure yet how they differ) but I don't see a way to use XSD from the maven dependency as an input.我知道 maven-jaxb2-plugin(codehouse-mojo 和 java-net,还不确定它们有什么不同),但我没有看到使用 maven 依赖项中的 XSD 作为输入的方法。 Do I have to write my own plugin to do this?我是否必须编写自己的插件才能做到这一点?

It doesn't have to be JAXB if there is a better tool to it.如果有更好的工具,它不一定是 JAXB。

Disclaimer: I'm the author of the maven-jaxb2-plugin .免责声明:我是maven-jaxb2-plugin

Check the documentation , it's right there.检查文档,它就在那里。 See Specifying What To Compile - Specifying URLs, filesets and Maven artifact resources .请参阅指定要编译的内容 - 指定 URL、文件集和 Maven 工件资源

Example:例子:

<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>

You can also use catalogs to rewrite schema URLs to Maven artifact resources .您还可以使用目录将架构 URL 重写为Maven 工件资源

Example:例子:

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

This will rewrite an URI http://schemas.opengis.net/ows/2.0/owsAll.xsd to maven:org.jvnet.ogc:ogc-schemas:jar::!/ogc/ows/2.0/owsAll.xsd .这会将 URI http://schemas.opengis.net/ows/2.0/owsAll.xsd重写为maven:org.jvnet.ogc:ogc-schemas:jar::!/ogc/ows/2.0/owsAll.xsd This will reference the ogc/ows/2.0/owsAll.xsd resource in ogc-schemas JAR artifact.这将引用ogc-schemas JAR 工件中的ogc/ows/2.0/owsAll.xsd资源。

As far as I know, these features are unique to the maven-jaxb2-plugin .据我所知,这些特性是maven-jaxb2-plugin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM