java JAXB Maven 插件不生成类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35242941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAXB maven plugin not generating classes
提问by krmanish007
I am trying to generate the java files from the XSD, but the below code doesn't generate. If I uncomment outputDirectory, it works but delete the folder first. adding clearOutputDir = false
is also not generating anything.
我正在尝试从 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>
I am getting the message:
我收到消息:
[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.
回答by Tunaki
The first thing is that you should never, ever,generate code inside src/main/java
. Generated code should not be version-controlled and can be deleted at any-time since it will be regenerated anyway.
首先,你永远不应该在src/main/java
. 生成的代码不应受版本控制,并且可以随时删除,因为无论如何它都会重新生成。
Generated code should always be located under target
, the build directory of Maven. The jaxb2-maven-plugin
will generate classes by default under target/generated-sources/jaxb
and there's really no reason to change that. If you're using Eclipse, you just need to add this folder to the buildpath by right-clicking it and selecting "Build Path > Use a Source Folder".
生成的代码应始终位于target
Maven 的构建目录下。该jaxb2-maven-plugin
会默认生成的类下target/generated-sources/jaxb
有真的没有理由改变这一现状。如果您使用的是 Eclipse,您只需要通过右键单击该文件夹并选择“构建路径 > 使用源文件夹”来将此文件夹添加到构建路径中。
When running Maven, you will run it with mvn clean install
: it will clean the target
folder and regenerate everything from scratch: this leads to a safe and maintainable build. You'll find that this will solve your problem: since the generated classes are deleted before the build, they will be correctly re-generated during the next build.
运行 Maven 时,您将运行它mvn clean install
:它将清理target
文件夹并从头开始重新生成所有内容:这会导致安全且可维护的构建。您会发现这将解决您的问题:由于生成的类在构建之前被删除,它们将在下一次构建期间正确地重新生成。
Of course, if this generation process is long and you don't want to do it each time, you can run Maven with just mvn install
and configure the plugin not to remove the previously generated classes by setting clearOutputDir
to false
. But do note that, while the build will be slightly faster, you may not detect subsequent errors in your XSD if they are updated.
当然,如果这个生成过程很长,不想每次都做,可以直接运行Maven,mvn install
配置插件不删除之前生成的类,设置clearOutputDir
为false
. 但请注意,虽然构建速度会稍快一些,但如果 XSD 更新,您可能无法检测到后续错误。