Can't generate classes from XSD
Here is my pom.xml that I used to create the classes. I am trying to build via maven build, providing the target as a package. I also tried using mvn clean install too, I didn't see any errors in the console, but the classes are not generated in "target / generated-sources / jaxb"
http://maven.apache.org/xsd/maven-4.0.0.xsd "> 4.0.0
<groupId>com.login.app</groupId>
<artifactId>Login</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Login</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>target/generated-sources/jaxb</outputDirectory>
<packageName>com.login.app</packageName>
<schemaDirectory>src/main</schemaDirectory>
<schemaFiles>proto.xsd</schemaFiles>
</configuration>
</plugin>
</plugins>
</build>
Here is my example XSD file I was trying to create java classes, can you tell me if there should be any changes in the xsd file.
<xs:element name="UserInfo">
<xs:complexType>
<xs:sequence>
<xs:element name="firsName" type="xs:string" />
<xs:element name="middleName" type="xs:string" />
<xs:element name="lastName" type="xs:string" />
<xs:element name="dob" type="xs:string" />
<xs:element name="ssn" type="xs:string" />
<xs:element name="emailId" type="xs:string" />
<xs:element name="userId" type="xs:string" />
<xs:element name="password1" type="xs:string" />
<xs:element name="password2" type="xs:string" />
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="addressline1" type="xs:string" />
<xs:element name="addressline2" type="xs:string" />
<xs:element name="zipCode" type="xs:int" />
<xs:element name="street" type="xs:string" />
<xs:element name="apt" type="xs:string" />
<xs:element name="city" type="xs:string" />
<xs:element name="state" type="xs:string" />
<xs:element name="country" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
source to share
Below is the original structure of the base directory.
└───src
├───main
│ └───resources
└───test
proto.xsd
is under resources.
The above schema is prefixed xs
with no namespace declaration. So changed as shown below:
proto.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="UserInfo" >
<xs:complexType>
<xs:sequence>
<xs:element name="firsName" type="xs:string" />
<xs:element name="middleName" type="xs:string" />
<xs:element name="lastName" type="xs:string" />
<xs:element name="dob" type="xs:string" />
<xs:element name="ssn" type="xs:string" />
<xs:element name="emailId" type="xs:string" />
<xs:element name="userId" type="xs:string" />
<xs:element name="password1" type="xs:string" />
<xs:element name="password2" type="xs:string" />
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="addressline1" type="xs:string" />
<xs:element name="addressline2" type="xs:string" />
<xs:element name="zipCode" type="xs:int" />
<xs:element name="street" type="xs:string" />
<xs:element name="apt" type="xs:string" />
<xs:element name="city" type="xs:string" />
<xs:element name="state" type="xs:string" />
<xs:element name="country" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Here is the pom.xml file :
<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.login.app</groupId>
<artifactId>Login</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Login</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>target/generated-sources/jaxb</outputDirectory>
<packageName>com.login.app</packageName>
<schemaDirectory>src/main/resources</schemaDirectory>
<schemaFiles>proto.xsd</schemaFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
If you noticed above, the value schemaDirectory
changes accordingly, since the xsd file is located there.
Now run the following maven command:
mvn clean install
Output:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Login 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ Login ---
[INFO]
[INFO] --- jaxb2-maven-plugin:1.5:xjc (xjc) @ Login ---
[INFO] Generating source...
[INFO] parsing a schema...
[INFO] compiling a schema...
[INFO] com\login\app\ObjectFactory.java
[INFO] com\login\app\UserInfo.java
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Login ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Login ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to D:\workspace\mavenexamples\maven-generate-classes\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Login ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\workspace\mavenexamples\maven-generate-classes\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ Login ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ Login ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ Login ---
[INFO] Building jar: D:\workspace\mavenexamples\maven-generate-classes\target\Login-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ Login ---
[INFO] Installing D:\workspace\mavenexamples\maven-generate-classes\target\Login-0.0.1-SNAPSHOT.jar to C:\Users\apps\.m2\repository\com\login\app\Login\0.0.1-SNAPSHOT\Login-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\workspace\mavenexamples\maven-generate-classes\pom.xml to C:\Users\apps\.m2\repository\com\login\app\Login\0.0.1-SNAPSHOT\Login-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.241 s
[INFO] Finished at: 2017-03-27T09:47:57+05:30
[INFO] Final Memory: 17M/137M
[INFO] ------------------------------------------------------------------------
You can also see the directory files target
:
├───classes
│ └───com
│ └───login
│ └───app
├───generated-sources
│ ├───annotations
│ └───jaxb
│ └───com
│ └───login
│ └───app
├───jaxb2
├───maven-archiver
└───maven-status
└───maven-compiler-plugin
└───compile
└───default-compile
source to share