Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB maven plugin not generating classes

Tags:

java

maven

jaxb

xjc

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.
like image 958
krmanish007 Avatar asked Feb 06 '16 15:02

krmanish007


People also ask

How do I generate Java classes from XSD using JAXB maven?

Just build the maven project using mvn clean install and you will see java classes generated in target/generated-sources/jaxb directory.

How do I run XJC?

Open a command prompt. Run the JAXB schema compiler, xjc command from the directory where the schema file is located. The xjc schema compiler tool is located in the app_server_root \bin\ directory. Use the generated JAXB objects within a Java application to manipulate XML content through the generated JAXB classes.

What is jaxb2 maven plugin?

This plugin uses the Java API for XML Binding (JAXB), version 2+, to generate Java classes from XML Schemas (and optionally binding files) and to create XML Schemas from annotated Java classes.


1 Answers

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.

like image 92
Tunaki Avatar answered Sep 21 '22 22:09

Tunaki