JAXB maven插件不生成类

krm*_*007 11 java jaxb xjc maven

我试图从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>
Run Code Online (Sandbox Code Playgroud)

我收到的消息是:

[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.
Run Code Online (Sandbox Code Playgroud)

Tun*_*aki 13

首先,你永远不应该在里面生成代码src/main/java.生成的代码不应受版本控制,并且可以随时删除,因为它无论如何都会被重新生成.

生成的代码应始终位于targetMaven的构建目录下.该jaxb2-maven-plugin会默认生成的类下target/generated-sources/jaxb有真的没有理由改变这一现状.如果您正在使用Eclipse,则只需将此文件夹添加到构建路径,方法是右键单击它并选择"构建路径>使用源文件夹".

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

当然,如果这一代的过程是漫长的,你不希望每次都这样做,你可以只运行Maven mvn install和配置插件无法通过设置,移除先前生成的类clearOutputDirfalse.但请注意,虽然构建速度稍快,但如果更新后,您可能无法在XSD中检测到后续错误.

  • @ krmanish007不过请记住我的警告:在src / main / java中生成代码只会给您带来痛苦和折磨!:) (2认同)