如何定义需要的模块,而不是 Apache Maven 中的依赖项

How to define a module being required, but not a dependency in Apache Maven

我有一个 Maven 多模块项目,有一个(或多个)名为 "plugin-xx" 的模块,在主 "runtime" 模块中,我正在动态加载一个 class一个插件模块。 为了避免 class 加载冲突,我正在创建一个新的 classloader,指向 "plugin-xx".

目标目录中的 jar

所以对于 "runtime" 模块,我想告诉 Maven,"plugin-xx" 需要生成一个 jar,但我不希望这个 jar 在 class "runtime" 的路径。

添加 "plugin-xx" 作为依赖项会将其包含在 class 路径中。

包括 plugin-xx 作为依赖项但使用 compile 以外的依赖项范围。我想你想要这个:

  • <scope>provided</scope>

来自docs:

provided

This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

我不是很清楚你的要求所以可能你想要这个:

  • <scope>runtime</scope>

来自docs:

runtime

This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.

您可以为您的依赖项使用 runtime 范围。定义如下所示:

<dependency>
    <groupId>plugin-xx</groupId>
    <artifactId>plugin-xx</artifactId>
    <version>1.0</version>
    <scope>runtime</scope>
</dependency>

在编译期间,您不会在主模块的类路径中看到该插件,但它会在运行时放置在类路径中,因此您可以使用 ClassLoader 动态加载它。