Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 586
May12 Avatar asked Feb 14 '17 12:02

May12


People also ask

What is the use of 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.

How do I create a Java class using 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.


1 Answers

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>
like image 154
Xstian Avatar answered Sep 28 '22 09:09

Xstian