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

人走茶凉 提交于 2019-12-12 11:01:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!