使用JAXB和xjc maven插件生成Java类

时间:2018-05-16 13:32:32

标签: java maven spring-boot xsd

我是XJC Maven插件的新手。我正在尝试从XSD生成Java类。我已将XSD文件放在src / main / resources / static / xsd位置(假设我的文件名是example.xsd)。我还创建了一个包src / main / java / com / example / message,其中我期望生成的Java类的源代码。我的pom文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <version>1</version>
  <packaging>war</packaging>
  <name>demo</name>
  <description>demo</description>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.13.RELEASE</version>
    <relativePath />
    <!-- lookup parent from repository -->
  </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.7</java.version>
  </properties>
  <repositories>
    <repository>
      <id>jboss</id>
      <name>JBoss Repository</name>
      <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
    <repository>
      <id>java.net</id>
      <name>Java.net Repository</name>
      <url>http://download.java.net/maven/2/</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
    <repository>
      <id>codelds</id>
      <url>https://code.lds.org/nexus/content/groups/main-repo</url>
    </repository>
  </repositories>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
        <exclusion>
          <artifactId>tomcat-embed-el</artifactId>
          <groupId>org.apache.tomcat.embed</groupId>
        </exclusion>
        <exclusion>
          <artifactId>tomcat-embed-websocket</artifactId>
          <groupId>org.apache.tomcat.embed</groupId>
        </exclusion>
        <exclusion>
          <artifactId>tomcat-embed-core</artifactId>
          <groupId>org.apache.tomcat.embed</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>ojdbc6</artifactId>
      <version>11.2.0.3</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>demo</finalName>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>2.3.1</version>
        <executions>
          <execution>
            <id>xjc</id>
            <goals>
              <goal>xjc</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <packageName>com.example.message</packageName>
          <sources>
            <source>src/main/resources/static/xsd/example.xsd</source>
          </sources>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

但是当我构建项目时,com.example.message中没有生成任何文件。任何人都可以建议我做错了什么?

1 个答案:

答案 0 :(得分:1)

如果您未指定目标文件夹,则生成的类将放在/target/generated-sources/jaxb中。 我不会将这些类放在src/main/java下的任何文件夹或包中:我认为每次使用Maven启动构建时都应该删除并重新创建生成的文件。通过这种方式,您一定会始终拥有正确生成的文件。

如果您使用IntelliJ,您应该能够使用类,如果您是Eclipse用户,则必须将它们添加到源文件夹(右键单击文件夹 - &gt;构建路径 - &gt;用作源文件夹)。

生成jar / war文件时,不需要指定任何包含这些类的内容,Maven会自动包含它们。