12

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?

lrxw
  • 3,576
  • 7
  • 32
  • 46

1 Answers1

25

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>
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
  • I should have mention, that the cxf-codegen-plugin add the dir as a normal source folder. So it will still get packaged in the jar, wont it? – lrxw Dec 21 '11 at 12:34
  • @mephi. Actually, from the plugin description, looks like it only spits out Java files from WSDL document. Add `cxf-codegen-plugin` to `generate-test-sources` phase, and add `build-helper-maven-plugin:add-test-source` right after that and the compiled classes will only be available for test execution – Alexander Pogrebnyak Dec 21 '11 at 15:58