Maven:如何处理生成的测试源(仅限)? [英] Maven: How to handle generated sources for test(only)?

查看:133
本文介绍了Maven:如何处理生成的测试源(仅限)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Usually generated sources should be created in the target dir. But how do I handle classes that are only used for test? I dont want that these classes get packaged in my jar. Is there a common way to deal with this situation?

推荐答案

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

Use maven build helper plugin's add-test-source goal to add your generated test source files to the build -> http://mojo.codehaus.org/build-helper-maven-plugin/add-test-source-mojo.html

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

It ensures that the directories added by this goal will be picked up automatically by the compiler plugin during test-compile phase of the build.

编辑

以下是如何使用cxf-codegen-plugin

Here is the example of how to generate code for testign with cxf-codegen-plugin

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

这篇关于Maven:如何处理生成的测试源(仅限)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆