Tomcat package when multiple projects share jar and specify jar version precisely

Source: Internet
Author: User

If there are multiple wars in a TOMCAT when a product is packaged, the general way to deploy it is to deploy to the%tomcat_home%/webapps directory, which follows the Java EE specification and puts the referenced jar into the%tomcat_home%/webapps/ Xxxxx.war/web-inf/lib below. But more than one project could possibly refer to the same jar, how do you make multiple projects share this jar? When a project is published, it often causes a lot of problems due to jar conflicts, if the package is specified exactly the required jar version? How do I load a war in a specific location without putting it in the%tomcat_home%/webapps directory? This article will address several of these issues.

1. Download the TOMCAT Zip package and unzip it and create a lkexample.xml file under the%tomcat_home%/conf/catalina/localhost folder with the following contents:

<?xml version= "1.0" encoding= "UTF-8"? ><context docbase= ". /.. /apps/lk-example.war ">    <loader classname=" Com.tgb.lk.example.dist.ManifestClasspathWebappLoader "/> </Context>

2. Place the Lk-example.war you want to deploy to: /.. /apps/lk-example.war, and cut all jars under Lk-example.war/web-inf/lib to the following: /lib, see the file structure directory

3. Write the Com.tgb.lk.example.dist.ManifestClasspathWebappLoader.java class and package it as a jar
Package com.tgb.lk.example.dist; Import Org.apache.catalina.container;import Org.apache.catalina.lifecycleexception;import Org.apache.catalina.core.standardcontext;import Org.apache.catalina.loader.WebappLoader; Import Java.io.*;import Java.util.jar.attributes;import java.util.jar.Manifest; /*** gets the classpath from the MANIFEST.MF in the war and loads the additional */public class Manifestclasspathwebapploader extends Webapploader {public MANIFESTC    Lasspathwebapploader () {super ();    } public Manifestclasspathwebapploader (ClassLoader parent) {super (parent); } @Override protected void Startinternal () throws lifecycleexception {final Container Container = Getcontai        NER (); if (container instanceof standardcontext) {file Basefile = new File (((Standardcontext) container). Getrealpath             (""));                     if (basefile.exists () && basefile.canread ()) {//Is readable if (basefile.isdirectory ()) {//directory Final file Manifestfile = new file (bAsefile, "meta-inf/manifest.                     MF "); if (manifestfile.exists () && manifestfile.canread () && manifestfile.isfile ()) {//manifest. MF File readable System.out.println ("[DIST] found MANIFEST.                         MF "+ manifestfile);                            try {fileinputstream FileInputStream = new FileInputStream (manifestfile);                         Setclasspaths (Basefile, FileInputStream);                        } catch (FileNotFoundException e) {e.printstacktrace ();          }}} and Else if (Basefile.isfile ()) {//File (WAR)}}}    Super.startinternal (); }/** * Set the classpath in the MANIFEST.MF stream * * @param basefile * @param inputstream */private void Setclasspa        Ths (File basefile, InputStream inputstream) {String classpaths[] = null; try {final Manifest Manifest = new MAnifest (InputStream);            Final Attributes Attributes = Manifest.getmainattributes ();            String Classpathvalue = Attributes.getvalue ("Class-path"); if (classpathvalue! = null) {//Can not be null description found Class-path Classpathvalue = Classpathvalue.replaceall ( "[\r\n]+$", "" ");     Remove line break classpaths = Classpathvalue.split ("\\s+");        Split the taxonomy Path string}} catch (IOException e) {e.printstacktrace (); if (classpaths! = null) {//If a classpath is found, set the classpath for (String classpath:classpaths) {AddR    Epository (New File (Basefile.getparent (), classpath). Touri (). toString ()); Convert relative path to actual path and convert to URI} System.out.println ("[DIST]" + basefile.getname () + "append" + CLASSPATHS.L         Ength + "classpaths."); }     } }

4. Place the typed jar package under%tomcat_hone%\lib.
5. Modify the Pom.xml of your War project, add the following configuration and run the MVN Package command:
<build>        <plugins>            <plugin>                <artifactId>maven-war-plugin</artifactId>                <configuration>                    <archive>                        <manifest>                            <addclasspath>true</ Addclasspath>                            <classpathprefix> /lib/</classpathprefix>                            <useUniqueVersions>false</useUniqueVersions>                        </manifest >                    </archive>                </configuration>            </plugin>        </plugins>    </ Build>


Observe the lk-example.war/meta-inf/manifest of the war packet. MF file, this file has a version of the jar that is exactly referenced by the war. Such as:


6. Unzip the war package into the Apps directory file.
7. Run Tomcat to access the http://localhost:8080/lkexample.

Summary, modify%tomcat_home%/conf/catalina/localhost/ LKEXAMPLE.XML Specifies the app that tomcat loads and the WebApp ClassLoader that is used when the app is loaded, Manifestclasspathwebapploader loads the xxxx.war/meta-inf/with its own overridden ClassLoader. MANIFEST. The jar referenced by the exact jar path in MF. If you publish a project that has multiple wars, you can also specify a jar file that references the exact version under Lib so that the same jar can be used by multiple projects.


Tomcat package when multiple projects share jar and specify jar version precisely

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.