【问题标题】:Maven: How to handle generated sources for test(only)?Maven:如何处理生成的测试源(仅)?
【发布时间】:2013-08-30 20:05:33
【问题描述】:

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

【问题讨论】:

    标签: java maven


    【解决方案1】:

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

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

    编辑

    这里是如何使用 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>
    

    【讨论】:

    • 我应该提一下,cxf-codegen-plugin 将目录添加为普通的源文件夹。所以它仍然会被包装在罐子里,不是吗?
    • @mephi。实际上,从插件描述来看,它似乎只是从 WSDL 文档中吐出 Java 文件。将cxf-codegen-plugin添加到generate-test-sources阶段,然后添加build-helper-maven-plugin:add-test-source,编译后的类将仅可用于测试执行
    猜你喜欢
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 2012-04-11
    • 2022-10-19
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    相关资源
    最近更新 更多