问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/41031421/when-and-how-does-m2e-use-the-maven-test-scope-to-include-test-dependencies-in-e