Intellij Idea Java - Maven JAXB-2插件具有不同配置的多个方案不会生成类

zd287kbt  于 2023-04-29  发布在  Java
关注(0)|答案(4)|浏览(321)

我们正在将现有的项目从Ant + Eclipse迁移到Maven + IntelliJ IDEA。我目前正在使用JAXBxsd文件生成类。我想继续当前的项目结构,所以我希望jaxb2-maven-plugin在特定位置生成类。我有多个方案,并希望在不同的位置生成类。我使用多个插件执行绑定,以便按照the JAXB-2 Maven plugin site中的说明执行。我的问题是,只有第一次执行。在第二次执行中不会生成任何类。这是我的POM。xml文件相关部分:

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <id>schema1</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                    <configuration>
                        <schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
                        <schemaFiles>myschema1.xsd</schemaFiles>
                        <packageName>xml</packageName>
                        <outputDirectory>${basedir}/src/main/java/com/example/dor/a</outputDirectory>
                        <arguments>-extension -Xcloneable -Xdefault-value -Xsetters -Xannotate</arguments>
                        <staleFile>${build.directory}/.jaxb-staleFlag-1</staleFile>
                        <clearOutputDir>false</clearOutputDir>
                    </configuration>
                </execution>
                <execution>
                    <id>schema2</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                    <configuration>
                        <schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
                        <schemaFiles>myschema2.xsd</schemaFiles>
                        <packageName>xml</packageName>
                        <outputDirectory>${basedir}/src/main/java/com/example/dor/b</outputDirectory>
                        <arguments>-extension -Xcloneable -Xdefault-value -Xsetters -Xannotate</arguments>
                        <staleFile>${build.directory}/.jaxb-staleFlag-1</staleFile>
                        <clearOutputDir>false</clearOutputDir>
                    </configuration>
                </execution>
            </executions>
</plugin>
ldxq2e6h

ldxq2e6h1#

使用版本2的更新答案。5.0的插件。这将是配置:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  <version>2.5.0</version>
  <executions>

    <execution>
      <id>schema1</id>
      <goals>
        <goal>xjc</goal>
      </goals>
      <configuration>
        <packageName>com.your.package</packageName>
        <sources>
          <source>${project.basedir}/src/main/resources/xsd/sample1/sample1.xsd</source>
        </sources>
        <clearOutputDir>false</clearOutputDir>
      </configuration>
    </execution>

    <execution>
      <id>schema2</id>
      <goals>
        <goal>xjc</goal>
      </goals>
      <configuration>
        <packageName>com.your.package</packageName>
        <sources>
          <source>${project.basedir}/src/main/resources/xsd/sample2/sample2.xsd</source>
        </sources>
        <clearOutputDir>false</clearOutputDir>
      </configuration>
    </execution>

  </executions>
</plugin>

希望对较新的配置有所帮助。

c9x0cxw0

c9x0cxw02#

我会升级到1。6,并且您必须将这两个模式放在不同的包中,以阻止生成的ObjectFactory中的冲突。下面为我工作:

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <id>schema1</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                        <configuration>
                            <schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
                            <schemaFiles>myschema1.xsd</schemaFiles>
                            <packageName>xml.a</packageName>
                            <outputDirectory>${basedir}/src/main/generated1</outputDirectory>
                            <clearOutputDir>true</clearOutputDir>
                        </configuration>
                    </execution>
                    <execution>
                        <id>schema2</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                        <configuration>
                            <schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
                            <schemaFiles>myschema2.xsd</schemaFiles>
                            <packageName>xml.b</packageName>
                            <outputDirectory>${basedir}/src/main/generated2</outputDirectory>
                            <clearOutputDir>true</clearOutputDir>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
ca1c2owp

ca1c2owp3#

有同样的问题。
启用maven的-X选项,查看为什么不执行第二次生成。
第二代未运行,因为staleFile相同。我必须在两次执行中添加具有不同值的参数staleFile:

<staleFile>${project.build.directory}/jaxb2/.xjcStaleFlag1</staleFile>

........

<staleFile>${project.build.directory}/jaxb2/.xjcStaleFlag2</staleFile>
1sbrub3j

1sbrub3j4#

对于那些使用jaxb 2-maven-plugin版本3的人。x可以添加

<clearOutputDir>false</clearOutputDir>

到每个执行块的配置部分。这可防止目录在每次执行后被清除。
参见:https://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v3.0.0/example_xjc_basic.html示例5

相关问题