JAXB maven plugin not generating classes

2020-07-05 07:40发布

问题:

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.

<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.

回答1:

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.

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".

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.

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.