我正在尝试从 XSD 生成 java 文件,但未生成以下代码。如果我取消注释 outputDirectory,它会起作用,但首先删除该文件夹。添加 clearOutputDir = false
也不会生成任何内容。
<build>
<plugins>
<!-- JAXB xjc plugin that invokes the xjc compiler to compile XML schema into Java classes.-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- The package in which the source files will be generated. -->
<packageName>uk.co.infogen.camel.message</packageName>
<!-- The schema directory or xsd files. -->
<sources>
<source>${basedir}/src/main/resources/xsd</source>
</sources>
<!-- The working directory to create the generated java source files. -->
<!--<outputDirectory>${basedir}/src/main/java</outputDirectory>
<clearOutputDir>false</clearOutputDir>-->
</configuration>
</plugin>
</plugins>
</build>
我收到消息:
[INFO] --- jaxb2-maven-plugin:2.2:xjc (default) @ service-business ---
[INFO] Ignored given or default xjbSources [/Users/aaa/development/workspace/infogen/infogen_example/service-business/src/main/xjb], since it is not an existent file or directory.
[INFO] No changes detected in schema or binding files - skipping JAXB generation.
最佳答案
首先,您应该永远、永远在src/main/java
中生成代码。生成的代码不应受版本控制,并且可以随时删除,因为无论如何都会重新生成。
生成的代码应始终位于 target
下,即 Maven 的构建目录。 jaxb2-maven-plugin
将默认在 target/generated-sources/jaxb
下生成类而且真的没有理由改变它。如果您使用的是 Eclipse,只需右键单击该文件夹并选择“Build Path > Use a Source Folder”,将其添加到构建路径即可。
运行 Maven 时,您将使用 mvn clean install
运行它:它将清理 target
文件夹并从头开始重新生成所有内容:这会导致安全且可维护的构建.您会发现这将解决您的问题:由于生成的类在构建之前被删除,因此它们将在下一次构建期间正确地重新生成。
当然,如果这个生成过程很长,你不想每次都做,你可以用mvn install
运行Maven,并配置插件不删除以前生成的类通过设置 clearOutputDir
为 false
。但请注意,虽然构建速度会稍快一些,但如果 XSD 已更新,您可能无法检测到后续错误。
关于java - JAXB maven 插件不生成类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35242941/