简体   繁体   English

如何从当前项目中引用Java Web项目库?

[英]how to refer java web project library from current project?

I have a web project Web1.war which have some jar file in its libraries (let's call it lib.jar ). 我有一个Web项目Web1.war ,它的库中有一些jar文件(我们称之为lib.jar )。 That is Web1.war --> lib.jar . 那就是Web1.war --> lib.jar

Then I did convert to jar file that web project: Web1.jar . 然后,我确实将该Web项目转换为jar文件: Web1.jar Afterwards add the Web1.jar file to another java web project Web2.war . 然后,将Web1.jar文件添加到另一个Java Web项目Web2.war

How to refer that library lib.jar from current project ( Web2.war ) through jar file ( Web2.war --> Web1.jar --> lib.jar ) ? 如何通过jar文件( Web2.war --> Web1.jar --> lib.jar )从当前项目( Web2.war )引用该库lib.jar

Java only supports JAR level dependencies. Java仅支持JAR级别的依赖关系。 You mention having converted you 'web project' to a JAR but don't give the details of how you did this. 您提到将“ Web项目”转换为JAR,但是没有提供有关操作方法的详细信息。

By default, Java uses the classpath to find dependencies. 默认情况下,Java使用类路径查找依赖项。 In a 'web project' (WAR) the classpath is the WEB-INF/classes directory and any JARs in the WEB-INF/lib . 在“ Web项目”(WAR)中,类路径是WEB-INF/classes目录以及WEB-INF/lib中的所有JAR。

If you have classes in one 'web project' (WAR) that you need to use in another 'web project' (WAR) then you have a couple of options, both of which will involve modifying your build: 如果您在一个“ Web项目”(WAR)中需要在另一个“ Web项目”(WAR)中使用的类,则有两个选择,这两个都将涉及修改构建:

  1. put shared classes are in their own jar, which you include in both 'web projects' (WARs). 将共享类放在自己的jar中,您将它们包含在两个“ Web项目”(WAR)中。

or 要么

  1. unpack the first 'web project' (WAR) adding the classes (and/or libraries) to the second 'web project' (WAR). 解压缩第一个“ Web项目”(WAR),将类(和/或库)添加到第二个“ Web项目”(WAR)。

Update 更新资料

You cannot covert a WAR to a JAR as you have suggested in your edits. 您不能像编辑中所建议的那样对JAR进行WAR转换。 While it's possible to create a custom archive format, a JAR cannot (and should not) contain other JARs. 尽管可以创建自定义存档格式,但JAR不能(也不应)包含其他JAR。

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

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