简体   繁体   中英

Generating Java classes using JAXB and xjc maven plugin

I am new to XJC Maven plugin. I am trying to generate a Java class from XSD. I have placed the XSD file in the location src/main/resources/static/xsd (Say my filename is example.xsd). I have also created a package src/main/java/com/example/message where I expect the sources of my generated Java classes. My pom file looks like below:

<?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. 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 . 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. 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).

You do not need to specify anything to include those classes when producing jar/war files, Maven will automatically include them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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