Including a directory inside web-inf/lib in tomcat

2019-04-15 01:35发布

问题:

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 )

回答1:

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>


回答2:

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.