简体   繁体   English

Tomcat WAR,外部JAR

[英]Tomcat WAR, External JAR

I have a highly modular application that I am trying to build and planning it out in my head is giving me some issues. 我有一个高度模块化的应用程序,我正在尝试构建和计划它,这给了我一些问题。 The application is as follows. 应用如下。 I want to create a series of JAR files that contain the business logic for my application, A.jar , B.jar and C.jar . 我想创建一系列JAR文件,其中包含我的应用程序A.jarB.jarC.jar的业务逻辑。

In addition to these JAR files, I would like to expose each of their functionalities to the Web on a dynamic and per Jar bases. 除了这些JAR文件之外,我还要在动态的和每个Jar的基础上向Web公开它们的每个功能。 So for instance, I may have A.jar and B.jar on a machine, and I would like the ability to deploy A.war and B.war into a Tomcat container on the same machine, where A.war and B.war simply use something like Spring to field the HTTP Request and forward this params onto A.jar and B.jar . 因此,例如,我可能在一台计算机上安装了A.jarB.jar ,并且我希望能够将A.warB.war部署到同一台计算机上的Tomcat容器中,其中A.war和B.war只需使用诸如Spring之类的字段来发送HTTP请求并将此参数转发到A.jarB.jar

Questions : 问题

  • how do I build A.war and B.war without it failing due to the fact that A.jar and B.jar may not be there at the time? 我如何构建A.warB.war而不会因A.jarB.jar可能不在而失败?
  • I know I can add A.jar and B.jar to the Classpath of Tomcat, but 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? 我知道我可以将A.jarB.jar添加到Tomcat的Classpath中,但是我将如何构建A.warB.war并基本上只是向他们保证,他们需要的特定Jar文件将是他们何时从那里加载课程呢?

Any help would be much appreciated, I have been a Java developer for awhile now, but have not delved into super modular code where class paths became an issue until recently. 非常感谢任何帮助,我已经有一段时间成为Java开发人员了,但是直到最近,我还没有深入研究超级模块化代码,在这些代码中类路径成为一个问题。

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? 我将如何构建A.war和B.war,并且基本上只是向他们保证,当他们从中加载类时,它们所需的特定Jar文件将在那里?

This is actually a suitable case for the provided scope for a Maven dependency. 对于provided的Maven依赖范围,这实际上是一个合适的情况。 From official documentation : 根据官方文件

provided - this is much like compile , but indicates you expect the JDK or a container to provide it at runtime. provided -这很像compile ,但是表明您希望JDK或容器在运行时提供它。 It is only available on the compilation and test classpath, and is not transitive. 它仅在编译和测试类路径上可用,并且不可传递。

That is, if you had a multi-module Maven project from which you would build A.jar (eg the a-lib module), B.jar (eg the b-lib module), the A.war (eg the a-war module) and B.war (eg the b-war module) 也就是说,如果您有一个多模块Maven项目 ,可以从中构建A.jar (例如a-lib模块), B.jar (例如b-lib模块), A.war (例如a-战争模块)和B.war (例如b-war模块)

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

the war modules would have dependencies on the library modules but in scope provided : libraries will be used for compilation and testing, but will not be packed as part of the lib folder of the war files. war模块将依赖于库模块,但在provided范围内:库将用于编译和测试,但不会打包为war文件的lib文件夹的一部分。 They will however need to be present at runtime, as part of the web container classpath. 但是,它们将需要在运行时作为Web容器类路径的一部分出现。

Provided is normally used with libraries which are provided (that is) out-of-the-box by the host container. 通常,provider与主机容器提供的(即开箱即用的)库一起使用。 A classic example is the Servlet API library: your application depends on it, but it is always provided by the servlet container (ie tomcat). 一个经典的例子是Servlet API库:您的应用程序依赖于它,但是它总是由Servlet容器(即tomcat)提供。


Similarly, you may look at the runtime scope in case you actually really don't want to use it for compilation (there is no direct reference to libraries code in war modules): 同样,如果您实际上确实不想将其用于编译runtime ,则可以查看runtime范围(在war模块中没有直接引用库代码的情况):

runtime - this scope indicates that the dependency is not required for compilation, but is for execution. runtime -此作用域指示依赖关系不是编译所必需的,而是执行所必需的。 It is in the runtime and test classpaths, but not the compile classpath. 它在运行时和测试类路径中,但不在编译类路径中。

Runtime scope is normally used for a driver (like a JDBC driver): something you would not (you shouldn't) refer directly in your code, but you definitely need at runtime. 运行时范围通常用于驱动程序(如JDBC驱动程序):您不会(您不应该)直接在代码中引用某些东西,但是在运行时确实需要。 As such, it will be part of the war lib packaging. 因此,它将成为war lib包装的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM