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

lrx*_*rxw 10 java maven

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

Ale*_*yak 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>
Run Code Online (Sandbox Code Playgroud)