This mecanism allow user to start webapp in tomcat 8 without packaging (like WAR).
This is interesting in development, to run application as fast as possible.
In Tomcat 6x, 7x this is called Virtual Web App, in Tomcat 8x there is a new way to do this : Resources
Documentation about Tomcat Resources is here : https://tomcat.apache.org/tomcat-8.0-doc/config/resources.html
Write the configuration
To use this, it’s necessary to write webapp configuration in tomcat server.xml file.
This is an example for an application with some jars, java classes and web parts (jsp and web.xml).
The structure of application in the repository is :
-
testWebapp
-
src/main/java
-
some java files
-
-
src/main/webapp
-
somes jsp
-
WEB-INF/web.xml
-
-
pom.xml with jar dependencies (hibernate for example)
-
Classic way will packaging a WAR file with this structure :
-
META-INF
-
WEB-INF
-
classes (contains .class files)
-
lib (contains .jar files)
-
web.xml
-
With tomcat resources, we package nothing, and just write into server.xml file :
<Context docBase="/tmp/testWebapp/src/main/webapp" path="/testWebapp" reloadable="false"> <Resources> <PostResources base="/tmp/testWebapp/target/classes" className="org.apache.catalina.webresources.DirResourceSet" webAppMount="/WEB-INF/classes"/> <PostResources base="/Work/mavenRepository/org/hibernate/hibernate-core/4.3.7.Final/hibernate-core-4.3.7.Final.jar" className="org.apache.catalina.webresources.JarResourceSet" webAppMount="/WEB-INF/classes"/> </Resources> </Context>
The order of each Resources declaration is important for the tomcat classloader.
It’s surprising to mount jar in WEB-INF/classes instead of WEB-INF/lib, but this is the only way I found to make it work correctly with JarResourceSet. The next configuration is dangerous, because it’s not link the right jar, and cause wrong loading in application, I suggest to not using this :
<Context docBase="/tmp/testWebapp/src/main/webapp" path="/testWebapp" reloadable="false"> <Resources> <PostResources base="/tmp/testWebapp/target/classes" className="org.apache.catalina.webresources.DirResourceSet" webAppMount="/WEB-INF/classes"/> <PostResources base="/Work/mavenRepository/org/hibernate/hibernate-core/4.3.7.Final" className="org.apache.catalina.webresources.DirResourceSet" webAppMount="/WEB-INF/lib"/> </Resources> </Context>