简体   繁体   English

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

[英]Generating Java classes using JAXB and xjc maven plugin

I am new to XJC Maven plugin. 我是XJC Maven插件的新手。 I am trying to generate a Java class from XSD. 我正在尝试从XSD生成Java类。 I have placed the XSD file in the location src/main/resources/static/xsd (Say my filename is example.xsd). 我已经将XSD文件放在src / main / resources / static / xsd位置(说我的文件名是example.xsd)。 I have also created a package src/main/java/com/example/message where I expect the sources of my generated Java classes. 我还创建了一个包src / main / java / com / example / message,在这里我希望生成的Java类的源。 My pom file looks like below: 我的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>

But when I build the project, no files are generated in com.example.message. 但是,当我构建项目时,com.example.message中没有文件生成。 Can anyone please suggest what wrong I am doing? 任何人都可以提出我在做什么错吗?

If you do not specify destination folder your generated classes will be placed in /target/generated-sources/jaxb . 如果不指定目标文件夹,则生成的类将放置在/target/generated-sources/jaxb I would not place those classes in any folder or package under src/main/java : I think that generated files should be dropped and recreated every time you launch the build using Maven. 我不会将这些类放在src/main/java下的任何文件夹或包中:我认为每次使用Maven启动构建时,都应删除并重新创建生成的文件。 In this way you are sure to have always the correctly generated file. 这样,您可以确保始终具有正确生成的文件。

If you use IntelliJ you should be able to use classes, if you are an Eclipse user then you must add them to source folders (right click on folder -> Build Path -> Use as Source Folder). 如果您使用IntelliJ,则应该能够使用类;如果您是Eclipse用户,则必须将它们添加到源文件夹中(右键单击文件夹->构建路径->用作源文件夹)。

You do not need to specify anything to include those classes when producing jar/war files, Maven will automatically include them. 在生成jar / war文件时,您无需指定任何内容即可包含那些类,Maven会自动包含它们。

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

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