java – 无法从XSD生成类
作者:互联网
这是我用来生成类的pom.xml.我试图通过提供目标作为包来通过maven构建生成.我也试过使用mvn clean install,我没有在控制台中看到任何错误,但是,“target / generated-sources / jaxb”中没有生成类
http://maven.apache.org/xsd/maven-4.0.0.xsd\”\u0026gt;
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>
这是我试图生成java类的示例XSD文件,如果需要对xsd文件进行任何更改,请告诉我.
<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>
解决方法:
这是创建的初始基本目录结构.
└───src
├───main
│ └───resources
└───test
proto.xsd放在资源下.
上述模式中有xs前缀,没有声明命名空间.所以改成如下:
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>
这是pom.xml文件:
<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>
如果您在上面注意到,schemaDirectory值会相应更改,因为xsd文件放在那里.
现在,运行以下maven命令:
mvn clean install
输出:
[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] ------------------------------------------------------------------------
您还可以看到目标目录的文件:
├───classes
│ └───com
│ └───login
│ └───app
├───generated-sources
│ ├───annotations
│ └───jaxb
│ └───com
│ └───login
│ └───app
├───jaxb2
├───maven-archiver
└───maven-status
└───maven-compiler-plugin
└───compile
└───default-compile
标签:java,maven,jaxb,xjc 来源: https://codeday.me/bug/20190627/1307851.html