简体   繁体   中英

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).

For example, let's say I have a generic project named "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.

My web project (lets call it webapp1) imports the source from "common" so that isn't a problem. 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.

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. 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.

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).

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).

When you are creating web project it's not necessary to add your libraries in WEB_INF folder.

Solutions:

  1. You wrote that you don't want to use maven "because my company only allows specific libs and versions of those libs"

You can install your libraries to your local maven repository, to your local machine. 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. 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 ); For example let's write example for Spring framework:

By default, spring boot plugin packages all dependencies of the project in the executable JAR with the following structure:

  • JAR
    • BOOT-INF
      • classes (contains java packages and classes)
      • lib (contains dependency jars)
    • META-INF (contains the MANIFEST.MF)
    • org (spring runtime support packages)

MANIFEST.MF is responsible to find JAR files. In Spring we have the following lunchers: JarLauncher , WarLauncher, and PropertiesLauncher. 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. But you can use PropertiesLauncher to specify jar files outside the project. So lets' do it! first off all you should use ZIP layout.

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 :

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. Finally we have many projects that uses same library jars that is outside the project.

You should not rely on Eclipse to build the deployment package (.war file). Use a build tool such as 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.
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.
Advantage: Project build is reproducible as batch build.

Gradle is a valid alternative to both Maven and Ant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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