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

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

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

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

免责声明:我是 maven-jaxb2-plugin.

的作者

检查documentation, it's right there. See 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 to rewrite schema URLs to 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 独有的。