DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Extending Tomcat WebappLoader to Share Library jars

Extending Tomcat WebappLoader to Share Library jars

Dapeng Liu user avatar by
Dapeng Liu
·
Mar. 14, 10 · Interview
Like (2)
Save
Tweet
Share
27.83K Views

Join the DZone community and get the full member experience.

Join For Free
Using Tomcat + Spring + Hibernate can quickly result in a 20M war. It took me 10mins to deploy such a war so to fix a typo in the HTML code. Wouldn't it be nice if we can somehow externalise the lib jars and different applications can share the same jars.

Reading Tomcat docs, I find out there is a shared lib folder for all the applications, provided each app is using exactly the same version. So this would not be very helpful either.

Finally I came across the Loader Component.

With a bit help from tomcat's src, I managed to extend org.apache.catalina.loader.WebappLoader that is able to load addtional lib by reading the external lib config settings from application's context.xml.

The idea is to intercept Tomcat's normal web app loading process and check if there are any tags in the context.xml. If so treat each tag as a folder that contains a set of jars, then make those jars available to the application. Then proceed with normal Tomcat web app loading.

There are 2 things to change

1. build the TomcatWebappLoader.jar (can be found in the attached zip) and put it to ${catalina.home}/lib, restart tomcat (this only needs to be done once). So the new WebappLoader is available to Tomcat.

2. change your context.xml

from
  <Context path="/testapp" />
to
  <Context path="/testapp">
    <Loader className='com.spiralcomms.tomcat.WebappLoader' />
    <lib>spring_2.5.6</lib>
    <lib>hibernate_3.3.0</lib>
  </Context>

and copy the required jars to ${catalina.home}/repo/spring_2.5.6 and ${catalina.home}/repo/hibernate_3.3.0 folders

DONE.

now the war size goes from 20M down to 300K, and the resulting war only takes a few secs to upload and deploy using Tomcat manager

I have included both the project src as well the compiled jar in the attached zip

Apache Tomcat JAR (file format) Library

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Secure APIs: Best Practices and Measures
  • DevOps vs Agile: Which Approach Will Win the Battle for Efficiency?
  • OWASP Kubernetes Top 10
  • HTTP vs Messaging for Microservices Communications

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: