JAXB maven 插件未生成 类

JAXB maven plugin not generating classes

我正在尝试从 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 will generate classes by default under target/generated-sources/jaxb 并且真的没有理由改变它。如果您使用的是 Eclipse,您只需要通过右键单击它并选择 "Build Path > Use a Source Folder".

将此文件夹添加到构建路径

当 运行ning Maven 时,您将 运行 它与 mvn clean install:它将清理 target 文件夹并从头开始重新生成所有内容:这会导致安全和可维护的构建。您会发现这将解决您的问题:由于生成的 类 在构建之前被删除,它们将在下一次构建期间正确地重新生成。

当然,如果这个生成过程比较长,不想每次都生成,可以运行Maven with just mvn install,配置插件不删除之前的通过将 clearOutputDir 设置为 false 生成 类。但请注意,虽然构建速度会稍快一些,但如果它们已更新,您可能无法在 XSD 中检测到后续错误。