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