Uploaded image for project: 'Maven Surefire'
  1. Maven Surefire
  2. SUREFIRE-443

Provide option to merge test classpath into one directory

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Minor
    • Resolution: Won't Fix
    • 2.4
    • None
    • classloading
    • None
    • Maven 2.0.8

    Description

      Please provide an option for the test classpath to be merged into one directory.

      Maven sets up the class files as follows:

      |-- target/test-classes
      |   `-- Unit Test classes
      |-- target/classes
          `-- Classes to be tested
      

      When running unit tests, the desired outcome is for any file in the "target/test-classes" tree to override those in the "target/classes". Specifically that any file in classes is ignored and overridden by the file in test-classes for the tests to work as expected.
      (In a sense a union on these two file trees in the order specified)

      The problem arises when using a container such as Embedded JBoss to test which treats each directory as a separate scope for classloading.
      Reading the ejb-3_0-fr-spec-persistence.pdf section "6.2.2 Persistence Unit Scope" spells it out quite clearly (a very short one page read) that if the "target/test-classes" and "target/classes" are each treated as a separate ejb-jar, then the runtime environment is not going to work due to these scoping rules.

      I have an ugly hack in place as follows which has the desired effect, but it would be more preferable if instead Maven Surefire provided an option to merge the directories together and have just one directory in the test classpath.

      <build>
              <plugins>
                  ...
                  <plugin>
                      <!-- This correctly merges the test-classes and classes directories for an EE container -->
                      <artifactId>maven-antrun-plugin</artifactId>
                      <executions>
                          <execution>
                              <id>setupTestClasspath</id>
                              <phase>test-compile</phase>
                              <configuration>
                                  <tasks>
                                      <echo message="Setting up a single merged test classpath directory" />
                                      <!-- move the originals -->
                                      <mkdir dir="${basedir}/target/tmp" />
                                      <move file="${basedir}/target/classes" todir="${basedir}/target/tmp" />
                                      <move file="${basedir}/target/test-classes" todir="${basedir}/target/tmp" />
                                      <!-- Merge into new directory -->
                                      <copy todir="${basedir}/target/test-classes" overwrite="true">
                                          <fileset dir="${basedir}/target/tmp/test-classes" />
                                      </copy>
                                      <copy todir="${basedir}/target/test-classes" overwrite="false">
                                          <fileset dir="${basedir}/target/tmp/classes" />
                                      </copy>
                                      <mkdir dir="${basedir}/target/classes" />
                                  </tasks>
                              </configuration>
                              <goals>
                                  <goal>run</goal>
                              </goals>
                          </execution>
                          <execution>
                              <id>restoreTestClasspath</id>
                              <phase>test</phase>
                              <configuration>
                                  <tasks>
                                      <echo message="Restoring original classes directories" />
                                      <delete dir="${basedir}/target/classes" />
                                      <move file="${basedir}/target/test-classes" tofile="${basedir}/target/test-classes-MERGED" />
                                      <move file="${basedir}/target/tmp/classes" todir="${basedir}/target" />
                                      <move file="${basedir}/target/tmp/test-classes" todir="${basedir}/target" />
                                  </tasks>
                              </configuration>
                              <goals>
                                  <goal>run</goal>
                              </goals>
                          </execution>
                      </executions>
                  </plugin>
                  ...
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            cosmic Cory Prowse
            Votes:
            9 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: