3
votes

In my webapp; WEB-INF/lib is added in classpath by default which is fine. Now, I want to add spring jar files in my tomcat's classpath. If I put all the jar files inside WEB-INF/lib; it works fine. But if I want to add a directory WEB-INF/lib/spring and put all jar files inside spring directory ; it doesnt work. How can I include WEB-INF/lib/spring in classpath.

I would prefer to make changes in web.xml as that is very localised to my webapp. Surely I will not want to make changes in catalina.properties because there all the jar files are loaded in JVM ( not just added in classpath )

2
Isn't that what the %CATALINA_HOME%/lib directory is for? - Marcelo
The whole purpose is to segregate different jar files in different folders like lib/hibernate , lib/spring .. And by default only lib is part of classpath - Deepak Singhal
You cannot have subdirectories within WEB-INF/lib. As servlet specification states, only jar files directly under lib (/WEB-INF/lib/*.jar) will be loaded by classloader. - sperumal
you could create a link to each jar file, so you still have the jars grouped. but managing the links could be not so easy. - eeezyy

2 Answers

6
votes

You shouldn't care about how the jar files are segregated into the war file: it's only used by the container. Segregating the jar files could be useful in your source project. But then you just need to have a build process (using ant, gradle, whatever) that copies all the jar files from all the subdirectories into WEB-INF/lib. Using ant:

<copy todir="web/WEB-INF/lib" flatten="true">
    <fileset dir="lib">
        <include name="**/*.jar"/>
    </fileset>
</copy>
0
votes

Since things don't care about JAR naming, another alternative is to simply append prefixes to the the JARs (i.e. "spring-OLD JAR NAME.jar") to keep related JARs grouped without folders.