Why can't I pack certain JARs outside WEB-INF

2019-08-10 01:59发布

问题:

I have read in several blogs that it is not advised (or not possible)to pack jars outside the WEB-INF folder. I am curious as to know why this is so..

For instatnce,

mainStuff.war
|
|-->WEB-INF
|   |
|   |-->lib     (having many jars, used by the contents of specificStuff.jar
|   |-->classes
|   |-->web.xml
|
|-->META-INF        (having the .MF file and signed files .SF and .DSA)
|
|
|-->index.jsp       (consists of a jnlp xml, referring to the jars in lib)
|
|
|-->specificStuff.jar   (this is a separate jar module, which is mentioned in the POM of the war)

In the jnlp configuration in index.jsp, i am referring to the jars in lib like:

  <resources>     
    <java version="1.6+"/>
    <jar href="specificStuff.jar"/> 
    <jar href="lib/someJarthatIneed.jar"/>
  </resources>     

Now, when i launch my JNLP, I get the error that someJarthatIneed failed to load. However, when i manually moved the lib outside WEB-INF, this error did not occur.

So, what I need to know is that, is there any way to pack the lib folder and all jars outside WEB-INF?? OR Why is it that my jnlp (which is outside WEB-INF) not able to load the jar from WEB-INF/lib. (I tried changing the path of jar as

<jar href="WEB-INF/lib/someJarthatIneed.jar"/>

but it did not work either..)

Any help on this will be apprciated. :)

回答1:

Thanks for all the suggestions... But, I found a way to do it..

I used the maven-dependency-plugin (copy-dependencies) to copy all the dependency jars to a folder lib to a specified location.

<plugin>      
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/src/main/webapp/lib</outputDirectory>
                <includeScope>compile</includeScope>
            </configuration>
        </execution>
    </executions>
</plugin> 

Then use

  <packagingExcludes>WEB-INF/lib/</packagingExcludes>

in the pom, where you are using maven-war-plugin to make the war, to remove the lib folder from WEB-INF..

So, now the index.jsp is able to fetch all the jars from the lib.



回答2:

What is in WEB-INF is not available from http requests, and what is not in WEB-INF will be considered as resources and thus will not be loaded by the container.

If the jars you put in the WEB-INF/lib is not required by your application, simply moves them outside the WEB-INF folder, and they will be treated as resources.

You can also think about building an "uberjar" for you JNLP jar, packing all dependencies into a single jar.