Tomcat WAR,外部 JAR

Tomcat WAR, External JAR

我有一个高度模块化的应用程序,我正在尝试构建它,但在我的脑海中计划它给了我一些问题。申请如下。我想创建一系列包含我的应用程序业务逻辑的 JAR 文件,A.jarB.jarC.jar.

除了这些 JAR 文件之外,我还想以动态和基于 Jar 的方式向 Web 公开它们的每个功能。因此,例如,我可能在一台机器上有 A.jarB.jar,我希望能够将 A.warB.war 部署到 Tomcat 容器中同一台机器,其中 A.war 和 B.war 只需使用 Spring 之类的东西来发送 HTTP 请求并将此参数转发到 A.jarB.jar.

问题

任何帮助将不胜感激,我成为 Java 开发人员已有一段时间了,但直到最近才深入研究 class 路径成为问题的超级模块化代码。

how can I will I be able to build A.war and B.war and basically just promise them that the specific Jar files that they need will be there when they go to load classes from it?

对于 Maven 依赖项的 provided 范围,这实际上是一个合适的案例。来自 official documentation:

provided - this is much like compile, but indicates you expect the JDK or a container to provide it at runtime. It is only available on the compilation and test classpath, and is not transitive.

也就是说,如果您有一个 multi-module Maven project,您将从中构建 A.jar(例如 a-lib 模块),B.jar(例如 b-lib 模块), A.war(例如 a-war 模块)和 B.war(例如 b-war 模块)

- project
   |___a-lib
   |___b-lib
   |___a-war
   |___b-war

war 模块将依赖于库模块但在范围内 provided: 库将用于编译和测试,但不会作为 [=19= 的一部分打包] war 个文件的文件夹。然而,它们将需要在运行时出现,作为 Web 容器类路径的一部分。

Provided 通常与宿主容器提供的(即)开箱即用的库一起使用。一个典型的例子是 Servlet API 库:您的应用程序依赖于它,但它始终由 servlet 容器提供(即 tomcat)。


类似地,您可以查看 runtime 作用域,以防您真的不想将它用于编译(在 war 模块中没有直接引用库代码):

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.

运行时范围通常用于驱动程序(如 JDBC 驱动程序):您不会(不应该)直接在代码中引用的东西,但在运行时肯定需要。因此,它将成为 war lib 包装的一部分。