Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

maven

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?

like image 833
lrxw Avatar asked Dec 21 '11 09:12

lrxw


People also ask

What does generate sources do in Maven?

The “maven-source” plugin is used to pack your source code and deploy along with your project. This is extremely useful, for developers who use your deployed project and also want to attach your source code for debugging.

Can Maven be used for testing?

Maven is the latest build testing tool. It has several new features as compare to Ant, like dependency, etc. Maven is a project build or project management tool. It is used to check the compilation issues between framework components whenever multiple test engineer integrates their files into the same framework.

Where in the Maven project we keep test classes?

If we use the default configuration, the Maven Surefire plugin runs all test methods found from the test classes that fulfill these two conditions: The test classes must be found from the src/test/java directory. The name of the test class must start or end with the string: Test.

Which Maven plugin can be used to run tests?

Configuring the Maven Surefire Plugin We can run our unit tests by using the Maven Surefire Plugin. Because we want to use its native JUnit 5 support, we have to use the version 2.22. 0 (or newer).


1 Answers

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>
like image 197
Alexander Pogrebnyak Avatar answered Sep 17 '22 12:09

Alexander Pogrebnyak