How to generate java classes into source folder using jaxb2-maven-plugin?

May12 picture May12 · Feb 14, 2017 · Viewed 15.3k times · Source

I use jaxb2-maven-plugin to generate java classes. There is plugin properties:

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- The package of your generated sources -->
                <packageName>com.bcap.me.JaxB</packageName>
                <sources>
                    <source>src/main/resources/xsds/pos.xsd</source>
                </sources>
            </configuration>
        </plugin>

After running mvn clean compile the plugin creates classes in the target\classes\com\bcap\me\JaxB directory. But i need to have classes in the source folder (package): src\main\java\com\bcap\me\JaxB How to do this?

UPDATE I add outputDirectory property, but i am not sure about the correctness of this approach:

<!--<packageName>com.bcap.me.JaxB</packageName>-->
<outputDirectory>src/main/java/com/bcap/me/JaxB</outputDirectory>

UPDATE

I solved my case like:

  <execution>
                        <id>xjc_pos</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                        <configuration>
                            <!-- The package of your generated sources -->
                            <packageName>com.bcap.me.JaxB</packageName>
                            <outputDirectory>src/main/java</outputDirectory>
                            <sources>
                                <source>src/main/resources/xsds/pos.xsd</source>
                            </sources>
                            <generateEpisode>false</generateEpisode>
                            <clearOutputDir>false</clearOutputDir>
                        </configuration>
                    </execution>

Thanks to @ulab

Answer

Xstian picture Xstian · Feb 14, 2017

You could use following maven plugin

       <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>target/generated-sources/xjc</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>