Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When and how does m2e use the Maven test scope to include test dependencies in Eclipse Run Configurations?

It took me a while to make this observation, but I see that m2e "magically" manages the classpath.

Examples: If I Run as a Java application in the "src/java/main" source folder it excludes "test" scope. Note: The class was able to compile, but not able to run. But if I move that same class to the "src/java/test" folder then it is able to compile and run.

If I Run as JUnit Test then the "test" scope is always included (even if the class is in the "main" source folder).

The only way I made these discover is by dumping the java.class.path system property at runtime.

I found the following at http://www.eclipse.org/m2e/documentation/m2e-faq.html

Also note, that classpath used for JUnit and Java Application launch configurations for projects that have Maven support enabled is also calculated in a special way and excluded resources does not affect it either.

I cannot see any differences when I compare their Run Configurations, so how can I see what magic is being performed?
And is there a way to override the "normal" behavior via the Run Configuration or any other means?

like image 596
successhawk Avatar asked Oct 18 '22 21:10

successhawk


1 Answers

Quoting from https://bugs.eclipse.org/bugs/show_bug.cgi?id=410228:

Here is how m2e decides if java application launch should use test or runtime artifact scopes.

  • "run as junit test" and "run as testng test" uses test scope.
  • "run as java application" uses test scope if application main class is located under src/test/java, otherwise uses runtime scope.

This means you can control the behavior using the location of the main class.

like image 68
exo_cw Avatar answered Oct 20 '22 23:10

exo_cw