eclipse在多modules项目结构下避免模块间依赖引用的场景

时间:2022-04-25 06:07:59

这个在单一classLoader时,不会有问题。如果多classloader下会有问题。

假设工程有两个模块,module2 依赖module1

当我们执行mvc eclipse:eclipse后,然后查看module2下的.classpath文件,会发现 : <classpathentry kind="src" path="/module1"/>。

如果module1中pom.xml,我们声明了一个第三方依赖(假设为 servlet-api)scope:provided。 你会发现此时module2中的buildpath还是会有这个第三方依赖(servlet-api)。即使你在module2的pom.xml中在module1排除servlet-api实际还是传递存在着的。

通常使用单一appClassLoader情况下,你可能不需要关注这个问题。但如果依靠这些classpath分classLoader问题,就会出现问题。

解决方法:

对项目主pom中声明maven-eclipse-plugin并配置如下,阻止这种模块引用。

具体说明如下: https://maven.apache.org/plugins/maven-eclipse-plugin/examples/prevent-module-references.html

重新执行mvn eclipse:eclipse 后,再查看module2的.classpath文件,<classpathentry kind="var" path="M2_REPO/com/xxxx.../{version}/module1-{version}.jar"。此时再检查buildpath就干净了。

注:在intellj idea不存在这个问题。