Our maven pom.xml specifies to add an additional source and test-source folder if a certain profile (here "java8") is activated. The corresponding part of the pom looks like the following
<profile>
<id>java8</id>
....
<build>
<plugins>
....
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals><goal>add-test-source</goal></goals>
<configuration>
<sources>
<source>src/test/java8</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
According to http://mojo.codehaus.org/build-helper-maven-plugin/usage.html this appears to be the correct specification.
Running mvm install -P java8
I see that the additional tests are performed as expected.
However, running mvm eclipse:eclipse -P java8
the additional test source folder does not appear in eclipse .classpath
.
Question: How do I have to configure maven to add the test source folder to the eclipse configuration? Is the above behavior a bug or a misconfiguration?
The way I see it, the plugin is working as expected.
When you run
mvn install -P java8
, you are invoking the phaseinstall
. In effect, maven executes all the phase prior toinstall
, includinggenerate-test-sources
phase andtest
phase... before it really executesinstall
. Because your plugin's goal is bound togenerate-test-sources
phase, that's why in this case you see your tests added in the class-path and run.When you run
mvn eclipse:eclipse -P java8
, however, you are invoking a plugin's goal (in particular,eclipse
goal ofeclipse
plugin), not a build life-cycle (phase). According to the eclipse plugin's documentation, only thegenerate-resources
phase will be invoked. Be noted thatgenerate-resources
doesn't not "include"generate-test-sources
(see more here), so in this case, your build-helper plugin does not get called.If I guess correctly, you're trying to run your test in Eclipse with the profile
java8
enabled. In that case, one way to do it (without having to work around) is right click on your project, click Maven, in the Active Maven Profile input box type injava8
->OK. Now right click in your project and choose Run As -> JUnit Test (or whatever test framework you're using). Make sure you use the latest Eclipse version (Kepler 4.3.1 as of now) as it has a built-in m2e plugin which has improved a lot from the original m2e.rather than using eclipse:eclipse, you can use the http://www.eclipse.org/m2e/ plugin to open maven
pom.xml
projects.Once you install that plugin in maven you will be able to take advantage of the m2e maven plugin, which can map any maven phase into the eclipse build life cycle. in our example you will need to add something like this to your pom.xml:
Important
this will only work if you install the m2e plugin and use it to open maven projects.
Having spent some time experimenting with this, I can give a partial answer to my own question (hopefully saving some time of other developers):
If one uses
instead of
then the test source folder is added to the eclipse .classpath (and it is added as a test folder). I.e. I am executing "add-test-source" in a different phase now.
In other words the profile looks like this:
This looks like a "workaround". It still contradicts the specification on http://mojo.codehaus.org/build-helper-maven-plugin/usage.html
I'm experiencing the same issue as you Christian Fries and I came to the same conclusion as you about binding the
add-test-source
goal to thegenerate-sources
phase instead of thegenerate-test-sources
phase.The problem is that when we run
mvn eclipse:eclipse
, we are actually directly calling a plugin so only that plugin will run. The reason thegenerate-sources
phase is executed is explained in the plugin's doco (http://maven.apache.org/plugins/maven-eclipse-plugin/eclipse-mojo.html):What we want is to be able to tell the plugin to execute the
generate-test-sources
phase before itself and then run. Note that maven will run all the phases up to and including the one you specify (http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html) so we don't need to say rungenerate-sources
ANDgenerate-test-sources
because simply specifying the latter is enough. Anyway, we can test this situation by running (the order matters):...and for me, this did exactly as we expected. You can see from the output that the build-helper-maven-plugin is run to add the test sources and THEN the maven-eclipse-plugin runs and picks it up.
Now we have a new problem because AFAIK you can only bind a plugin to a phase (so it runs when the phase is run) and not the other way around.
The (sort of) solution is to bind the
build-helper-maven-plugin
and themaven-eclipse-plugin
(in that order, so define the plugins in your POM in that order) to thegenerate-test-sources
phase and then instead of runningmvn eclipse:eclipse
, run:So we have a POM that looks like:
I know it's not perfect because people will still run
mvn eclipse:eclipse
and cry when it doesn't work. Also, themaven-eclipse-plugin
will run as part of anything that runs thegenerate-test-sources
phase (i.e.mvn clean install
) which isn't so bad if it doesn't steamroll custom settings people have but if it is a problem you could move this stuff into a profile, bind it to a different phase that doesn't run as part of a build (likeclean
) or create a new lifecycle phase.