如何在Maven src / test中为XSD生成类,在src / main中引用XSD

时间:2016-09-01 09:48:48

标签: java maven xsd jaxb jaxb2-maven-plugin

以下maven设置:

src/main/resources/BaseTypes.xsd
src/test/resources/MyTypeUsingBaseTypes.xsd

现在我想将BaseTypes生成到target / classes中,而MyTypeUsingBaseTypes生成target / test-classes。

问题是,BaseTypes也会生成(=重复)到目标/测试类中。

我正在使用org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.12.3,有两个执行:

<plugin>
  <groupId>org.jvnet.jaxb2.maven2</groupId>
     <artifactId>maven-jaxb2-plugin</artifactId>
     <version>0.12.3</version>
     <executions>
    <execution>
      <id>gen-schemas</id>
      <goals>
        <goal>generate</goal>
      </goals>
    </execution>

    <execution>
      <id>gen-test-schemas</id>
      <phase>generate-test-sources</phase>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <generateDirectory>target/generated-test-sources/xjc</generateDirectory>
        <addTestCompileSourceRoot>true</addTestCompileSourceRoot>
        <schemaDirectory>src/test/resources</schemaDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

1 个答案:

答案 0 :(得分:3)

也许这样的事情可以完成这项工作(检查路径):

尝试在<configuration>添加以下选项:

<schemaLanguage>:告诉文件的类型是什么(wsdl,wadl,ear等)

<schemaIncludes>:这使您有机会选择特定文件

<generatePackage>:将生成的* .java文件放入所选生成目录

中的包中

通过这种方式,您可以根据需要编写尽可能多的执行数据,以获得不同的模式。

<executions>
    <execution>
        <id>xjc-schema2</id>
        <goals>
            <goal>generate</goal>
        </goals>
        <configuration>
            <schemaLanguage>wsdl</schemaLanguage>
            <schemaDirectory>src/test/resources</schemaDirectory>
            <schemaIncludes>
                <include>MyTypeUsingBaseTypes.xsd</include>
            </schemaIncludes>
            <generatePackage>com.name.of.package.MyTypeUsingBaseTypes</generatePackage>

            <generateDirectory>target/generated-test-sources/xjc</generateDirectory>
            <clearOutputDir>false</clearOutputDir>
        </configuration>
    </execution>
    <execution>
        <id>xjc-schema2v2</id>
        <goals>
            <goal>generate</goal>
        </goals>
        <configuration>
            <schemaLanguage>wsdl</schemaLanguage>
            <schemaDirectory>src/main/resources</schemaDirectory>
            <schemaIncludes>
                <include>BaseTypes.xsd</include>
            </schemaIncludes>
            <generatePackage>com.name.of.package.BaseTypes</generatePackage>

            <generateDirectory>target/generated-sources/xjc</generateDirectory>
            <clearOutputDir>false</clearOutputDir>
        </configuration>
    </execution>
</executions>