Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Work around for m2e workspace resolution lacking test-classes in launcher classpath when test classifier set as dependency

Does anyone know how to workaround the problem where recent M2eclipse plugins fail to include dependent projects test-classes directory in junit launcher's classpath when the test's pom lists the test as a dependency?

Steps to Rep

  • Create to pom projects providerProj, userProj
  • In providerProj create TestUtils class and store with its tests
  • Set userProj to depend on provider's test classified artifact (provider-test.jar)
  • Add userProj test which invokes TestUtils class from providerProj

On commandline all works as expected. TestUtils class found via test classpath.

  • Import projects into eclipse
  • Run userProj test as junit

failure class not found.

When we check the classpath for the test don't see providerProj/test-classes folder. If we close providerProj and rerun, it works and we see provider-test.jar on class path.

useProject
<dependency>
   <groupId>workspacetest</groupId>
   <artifactId>providerProj</artifactId>
   <classifier>test</classifier>
</dependency>

We can manually edit the launchers and add in the necessary folders but that's not ideal. I might be able to convince m2eclipse to dump src and test output into target/classes which would mask the problem and open me up for entangled code. I can fork m2eclipse and change handle to assume that test classifier dep means include workspace resolution target/test-classes.

Do you know of a solution to the problems?

Thanks

Peter

like image 814
Peter Kahn Avatar asked Oct 02 '13 22:10

Peter Kahn


2 Answers

I wanted to do something similar not long ago. The issue is that Eclipse and Maven handle builds differently, and right now Eclipse doesn't deal with Maven's concept of classified artifacts very well. That's a paraphrase of notes I found in this Eclipse bug report.

I was able to find a workaround so both Eclipse and Maven function. I add this to the POMs that need items from a classified jar. (Note, I did this for test resources, not test source, so you may need to adjust if I don't have the config quite right below...)

<profile>
  <!-- Contents of this profile only needed to make this project work in Eclipse. -->
  <id>m2e</id>
  <activation>
    <property>
      <name>m2e.version</name>
    </property>
  </activation>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>include-test-source-eclipse</id>
            <phase>generate-test-sources</phase>
            <goals>
              <goal>add-test-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>../myOtherProj/src/test/java</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</profile>

Putting the build-helper plugin inside the profile ensures it only runs when in Eclipse, and not as part of a normal Maven build. It isn't pretty, but it works, at least in Eclipse Juno and m2e 1.3.1.20130219-1424.

The only issue I've had with the configuration is that sometimes Eclipse doesn't seem to find changes I made in my provider project. The fix is to run Maven --> Update Project on the provider project and any projects depending on it. I wasn't changing resources often enough for this to be a big deal for me.

like image 190
user944849 Avatar answered Oct 02 '22 13:10

user944849


You need to use <classifier>tests</classifier>.

BTW, http://maven.apache.org/guides/mini/guide-attached-tests.html discourages the use of the tests classifier, <type>test-jar</type> is preferred.

like image 44
Fred Bricon Avatar answered Oct 02 '22 15:10

Fred Bricon