如何使用jaxb2-maven-plugin将java类生成到源文件夹中?

2022-09-02 14:08:47

我使用jaxb2-maven-plugin来生成java类。有插件属性:

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

运行插件后,在目录中创建类。但是我需要在源文件夹(包)中有类:如何做到这一点?mvn clean compiletarget\classes\com\bcap\me\JaxBsrc\main\java\com\bcap\me\JaxB

更新我添加了属性,但我不确定这种方法的正确性:outputDirectory

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

更新

我解决了我的案件,就像:

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

感谢@ulab


答案 1

您可以使用以下 maven 插件

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

答案 2

推荐