现有几个xsd文件,需要将其转成java类,可以利用JAXB 的xjc工具来实现。
IEDA 中生成java类
打开xsd文件,然后Tools --> JAXB --> Generate Java code from xml...,弹出如下窗口,指定路径和包,点击OK即可
image.png
正常情况下,这就可以了,默认生成的是中文注释(Windows 下可能乱码),如果需要英文注释,可以使用 maven-jaxb2-plugin 插件生成。
Maven插件生成java类
处理单个xsd文件
在pom文件中添加如下插件配置,其中 locale 设置为en,即可生成英文注释。
<build>
    <plugins>
      <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.14.0</version>
        <configuration>
          <schemaDirectory>src/main/resources/xsd</schemaDirectory>
          <generateDirectory>src/main/java</generateDirectory>
          <schemaIncludes>
            <include>xsd1.xsd</include>
          </schemaIncludes>
       <generatePackage>com.test.common.domain.xsd1</generatePackage>
          <packageLevelAnnotations>false</packageLevelAnnotations>
          <noFileHeader>true</noFileHeader>
          <episode>false</episode>
          <locale>en</locale>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
此时执行jaxb2:generate,即可生成java类。如果我们想让不同的xsd生成在不同的包下,并且要处理多个xsd文件,则需要多次配置 schemaIncludes 和 generatePackage ,重复执行jaxb2:generate多次,这种方式不够友好。
image.png
处理多个xsd文件
将pom文件修改为如下,增加多个execution,并且指定不同的id。
<build>
    <plugins>
      <!--Generate java code by xsd file-->
      <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.14.0</version>
        <configuration>
          <schemaDirectory>src/main/resources/xsd</schemaDirectory>
          <generateDirectory>src/main/java</generateDirectory>
          <packageLevelAnnotations>false</packageLevelAnnotations>
          <noFileHeader>true</noFileHeader>
          <episode>false</episode>
          <locale>en</locale>
        </configuration>
        <executions>
          <execution>
            <id>xsd1-generate</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <schemaIncludes>
                <include>xsd1.xsd</include>
              </schemaIncludes>
              <generatePackage>com.test.common.domain.xsd1</generatePackage>
            </configuration>
          </execution>
          <execution>
            <id>xsd2-generate</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <schemaIncludes>
                <include>xsd2.xsd</include>
              </schemaIncludes>
              <generatePackage>com.test.common.domain.xsd2</generatePackage>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
此时执行mvn compile,即可将多个xsd文件生成java类。
image.png
如果想单独执行某一个或某几个execution:
// @后跟上execution id
// 执行单个execute
jaxb2:generate@xsd1-generate 
// 执行多个execute
jaxb2:generate@xsd1-generate jaxb2:generate@xsd2-generate 
IDEA下可以在Edit configurations中设置执行的命令:
image.png
Ref:
https://github.com/highsource/maven-jaxb2-plugin
https://stackoverflow.com/questions/31323475/jaxb-exception-messages-how-to-change-language
https://stackoverflow.com/questions/3166538/how-to-execute-maven-plugin-execution-directly-from-command-line
https://stackoverflow.com/questions/2857081/how-can-i-tell-jaxb-maven-to-generate-multiple-schema-packages












网友评论