Maven: How to handle generated sources for test(only)?

后端 未结 1 1737
失恋的感觉
失恋的感觉 2020-12-31 13:49

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

相关标签:
1条回答
  • 2020-12-31 14:00

    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

    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.

    EDIT

    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>
    
    0 讨论(0)
提交回复
热议问题