Maven:如何处理生成的测试源(仅限)?

时间:2011-12-21 09:10:14

标签: java maven

通常应在目标目录中创建生成的源。但是我该如何处理仅用于测试的类?我不希望这些类在我的jar中打包。有没有一种常见的方法来处理这种情况?

1 个答案:

答案 0 :(得分:20)

使用maven build helper插件的add-test-source目标将生成的测试源文件添加到构建中 - > http://mojo.codehaus.org/build-helper-maven-plugin/add-test-source-mojo.html

它确保编译器插件在构建的test-compile阶段自动获取由此目标添加的目录。

修改

以下是如何使用cxf-codegen-plugin为testign生成代码的示例

<build>
  <plugins>
    ...
    <plugin>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-codegen-plugin</artifactId>
      <version>${cxf.version}</version>
      <executions>
        <execution>
          <id>generate-test-sources</id>
          <phase>generate-test-sources</phase>
          <configuration>
            <sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
            <wsdlOptions>
              <wsdlOption>
                <wsdl>${basedir}/src/main/wsdl/myService.wsdl</wsdl>
              </wsdlOption>
            </wsdlOptions>
          </configuration>
          <goals>
            <goal>wsdl2java</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <version>${build-helper-maven-plugin.version}</version>
      <executions>
        <execution>
          <id>add-test-sources</id>
          <phase>generate-test-sources</phase>
          <goals>
            <goal>add-test-source</goal>
          </goals>
          <configuration>
            <sources>
              <source>${project.build.directory}/generated/cxf</source>
            </sources>
          </configuration>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
</build>