简体   繁体   English

如何在Eclipse动态Web应用程序中使用Java jar库

[英]How to use java jar Libraries in an Eclipse dynamic web application

I am trying to set up a directory structure (and project files) for projects that will need to share a common set of Jars (and in some cases code that is just imported into the working project). 我正在尝试为需要共享一组通用Jars的项目(以及在某些情况下只是导入到工作项目中的代码)建立目录结构(和项目文件)。

For example, let's say I have a generic project named "common". 例如,假设我有一个名为“ common”的通用项目。 This project contains common code and jars that might be used by a stand-alone java application or, in this case, a dynamic web application. 该项目包含可被独立的Java应用程序(在这种情况下为动态Web应用程序)使用的通用代码和jar。

My web project (lets call it webapp1) imports the source from "common" so that isn't a problem. 我的Web项目(我们称其为webapp1)从“ common”导入源代码,所以这不是问题。 The reason I'm doing this is because, at this point, it really doesn't by me anything to create a "common" project and compile it into a jar then reference that from webapp1 because webapp1 would not pick up the vendor jars since they must all be under WEB-INF/lib. 我这样做的原因是因为在这一点上,我真的什么也没创建一个“通用”项目并将其编译到一个jar中,然后从webapp1引用它,因为webapp1不会拾取供应商的jar它们必须都在WEB-INF / lib下。

So how can I do this? 那我该怎么做呢? I've thought about just making WEB-INF be a symbolic link into another directory with my jars in it, but that wouldn't work for Windows environments. 我曾经考虑过将WEB-INF变成一个到我目录中的另一个目录的符号链接,但这不适用于Windows环境。 But that's basically the kinda thing I'm going for. 但这基本上就是我想要的东西。 Here is a depiction of this: 这是对此的描述:

common \
  src \
    some.source.packages
  lib \
    HibernateLibs \
      hibernate.jar
      hibernate2.jar
    ApacheCommonLibs \
      common-string.jar \
      etc.

webapp1
  src \
    local.source.packages
    reference to common.source.packages above
  WebContent \
    WEB-INF \
      lib \
        This is where I'd like to place references to the libs in common
        (only those I need for this web app)

If I just have webapp1 reference common, this sort of works, but then the instant I try to run my webapp1 from the IDE, those libs aren't actually in the right place. 如果我只是拥有通用的webapp1参考,那么这种工作方式就可以了,但是当我尝试从IDE运行我的webapp1时,这些库实际上不在正确的位置。

Note that build systems like Maven, etc. are more or less out because my company only allows specific libs and versions of those libs (it is a pain, but that's what I have to work with). 请注意,诸如Maven之类的构建系统已或多或少地出现了,因为我的公司仅允许特定的库和这些库的版本(这很痛苦,但这就是我必须使用的库)。

Thanks for an insight on this. 感谢您对此的见识。 I'm not stuck on the project structure, but I do want to avoid having multiple projects with the same JARs all over them and have to keep those in sync (not to mention it makes checking out the code really slow). 我并不拘泥于项目结构,但我想避免在整个项目中使用同一个JAR的多个项目,并且必须使它们保持同步(更不用说它使签出代码的速度非常慢)。

When you are creating web project it's not necessary to add your libraries in WEB_INF folder. 创建Web项目时,无需在WEB_INF文件夹中添加库。

Solutions: 解决方案:

  1. You wrote that you don't want to use maven "because my company only allows specific libs and versions of those libs" 您写道,您不想使用Maven, “因为我的公司只允许特定的库和这些库的版本”

You can install your libraries to your local maven repository, to your local machine. 您可以将库安装到本地Maven存储库,也可以安装到本地计算机。 So the dependencies will be retrieved from your local computer. 因此,依赖项将从您的本地计算机中检索。 It's very simple: 很简单:

 mvn install:install-file -Dfile=[file_path] -DgroupId=[your group id] -DartifactId=[artifact id] -Dversion=[version]   -Dpackaging=jar

Also you can yous Nexus or Jfrog artifactory server to retrieve jars. 可以你要跟的NexusJfrog artifactory的服务器来获取罐。 This servers will be only for your company. 该服务器适用于您的公司。 So the jars will be accessible only for your company. 因此, 只有您的公司才能使用罐子。

  1. Also you can extract dependency from your your JAR/EAR/WAR too (but it depends what technology you use - Spring, EJB or just Servlets ); 您也可以从您的JAR / EAR / WAR中提取依赖项(但这取决于您使用的技术-Spring,EJB还是Servlets ); For example let's write example for Spring framework: 例如,让我们为Spring框架编写示例:

By default, spring boot plugin packages all dependencies of the project in the executable JAR with the following structure: 默认情况下,spring boot插件将项目的所有依赖项打包为具有以下结构的可执行JAR:

  • JAR
    • BOOT-INF 引导启动
      • classes (contains java packages and classes) 类(包含java包和类)
      • lib (contains dependency jars) lib(包含依赖罐)
    • META-INF (contains the MANIFEST.MF) META-INF(包含MANIFEST.MF)
    • org (spring runtime support packages) org(Spring运行时支持包)

MANIFEST.MF is responsible to find JAR files. MANIFEST.MF负责查找JAR文件。 In Spring we have the following lunchers: JarLauncher , WarLauncher, and PropertiesLauncher. 在春季,我们有以下午餐者: JarLauncherWarLauncherPropertiesLauncher。 Their purpose is to load resources. 它们的目的是加载资源。 The JarLauncher is only able to locate and load classes inside BOOT-INF and JARs inside the lib directory. JarLauncher 只能在lib目录内的BOOT-INF和JAR内部定位和加载类。 But you can use PropertiesLauncher to specify jar files outside the project. 但是您可以使用PropertiesLauncher在项目外部指定jar文件。 So lets' do it! 因此,让我们开始吧! first off all you should use ZIP layout. 首先,您应该使用ZIP布局。

Maven: Maven:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <layout>ZIP</layout>
    </configuration>
</plugin>

Gradle: 摇篮:

springBoot{
    layout = "ZIP"
}

Then everything is just simple. 然后,一切都很简单。 Now run the web application : 现在运行Web应用程序

java -Dloader.path=lib,external-jar.jar -jar starter.jar

Now we have web application without WEB_INF folder and we have all the dependence jar files outside the application. 现在,我们有了没有 WEB_INF文件夹的Web应用程序并且在应用程序之外还有所有依赖项jar文件。 Finally we have many projects that uses same library jars that is outside the project. 最后,我们有许多项目使用与项目外部相同的库jar。

You should not rely on Eclipse to build the deployment package (.war file). 您不应该依赖Eclipse构建部署包(.war文件)。 Use a build tool such as Ant, Maven, Gradle, ... 使用构建工具,例如Ant,Maven,Gradle等。

If the company has a standard set of authorized libraries, the company should create an internal Maven Repository containing those libraries (eg using Artifactory ) , and you should use Maven to get the libraries needed for your particular project from that repository, and to build deployment package. 如果公司具有一组标准的授权库,则公司应创建一个包含这些库的内部Maven存储库(例如,使用Artifactory ,并且您应使用Maven从存储库中获取特定项目所需的库,并进行部署包。
Advantage: Libraries are all in a common location, and not in a version control system. 优点:库都位于公共位置,而不是版本控制系统中。

If you can't get company to create a repository, but have a "common" project with all the libraries, then use Ant to build your project and create the deployment package. 如果您不能让公司创建存储库,而是拥有一个包含所有库的“公共”项目,则可以使用Ant来构建您的项目并创建部署包。
Advantage: Project build is reproducible as batch build. 优点:项目构建可作为批处理构建进行复制。

Gradle is a valid alternative to both Maven and Ant. Gradle是Maven和Ant的有效替代方案。

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

相关问题 Eclipse动态Web应用程序将jar文件放在WEB-INF / lib和Java Build Path中 - Eclipse Dynamic Web Application putting jar files in WEB-INF/lib and Java Build Path Java:尝试使用外部.jar库时,Eclipse中的NoClassDefFoundError - Java: NoClassDefFoundError in Eclipse while trying to use external .jar libraries 文件路径如何在Java和Eclipse中的动态Web应用程序中工作? - How do file paths work in a Dynamic Web Application in Java and Eclipse? 将Java项目导出为JAR,并尝试在ECLIPSE的动态Web项目中使用它后,找不到资源文件 - Cannot find a resource file, after exporting java project as JAR, and trying to use it in a Dynamic Web Project in ECLIPSE 如何在Eclipse Java Dynamic Web Project中使用.properties文件? - How to use a .properties file in Eclipse Java Dynamic Web Project? 如何在Java Build Path中放置eclipse jdt jar库? - How to put eclipse jdt jar libraries in Java Build Path? 关于eclipse(java)中如何使用外部库 - Regarding how to use external libraries in eclipse (java) 在Tomcat上部署后如何在Eclipse中配置Java Web应用程序以反映后端jar中的更改 - how to configure java web application in eclipse to reflect changes in backend jar after deploy on tomcat 如何在Eclipse中将Jar文件用作Java项目 - How to use Jar file As a Java project in Eclipse jar库和Eclipse中单独的.java文件 - jar libraries and separate .java files in Eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM