views:

807

answers:

2

Reason: Our project is using Ant as commandline interface. After making a new assembly with maven's assembly plugin, I want to make initial tests to see if all has been properly assembled. Therefore I need to include the unit tests in the final assembly. After assembling, the initial tests would then be called sth like

> ant initTest

build.xml:

<target="initTest">
  <junit>
   <test class="MyTest" />
  </junit>
</target>

Problem is: I want to keep my Unit tests in src/test/java and not move them to src/main/java. Is there a way to tell the assembly plugin to include my unit tests? A simple include in the assembly descriptor does not do it ...

+1  A: 

There are two steps:

  1. Package the tests into a jar as well as the main code.
  2. Depend on that "-tests" jar in the module that makes the assembly.

To package up the tests, you need to bin the jar:test-jar goal. e.g.

<build>
  <plugins>
    <plugin>
      <artifactId>maven-jar-plugin</artifactId>
      <executions>
        <id>test-jar</id>
        <phase>package</phase>
        <goals>
          <goal>test-jar</goal>
        </goals>
      </executions>
    </plugin>
  </plugins>
</build>

Then in the assembly module, you can depend on the resulting artifact.

<dependencies>
  <dependency>
    <groupid>${project.groupId}</groupId>
    <artifactId>some-artifact</artifactId>
    <version>${project.version}</version>
    <classifier>tests</classifier>
  </dependency>
</dependencies>

The key bit is the "classifier".

Dominic Mitchell
the dependency should probably also have the scope "test"
Rich Seller
A: 

Sorry for asking again, don't want to pollute this questions here but:

When referencing the projects own unit tests in the projects own pom.xml I get a circular dependency error, since maven 2.0.9 wont recognize the classifier tag. Or did I misunderstand here something?

Thanks a lot for the first answer!

If the test types are in project A and A depends on project B, project B won't be able to reference A's test jar as this is a cycle. If this isn't the case can you post your POMs?
Rich Seller