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: [HTML] View Plaincopy<?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, file structure directory see 3. Write the Com.tgb.lk.example.dist.ManifestClasspathWebappLoader.java class and package it as Jar[java] View Plaincopypackage 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 Manifestclasspathwebapploader () {         super (),     }    &NBSP;&NB Sp      Public Manifestclasspathwebapploader (ClassLoader parent) {         Super ( Parent);              @Override      protected void startinternal () throws lifecycleexception {         final Container Container = GetContainer ();      &N Bsp   if (container instanceof standardcontext) {                File Basef ile = new FIle (((Standardcontext) container). Getrealpath (""));                 if (basefile.exists () && basefile.canread ()) {  Readable                     if (basefile.isdirectory ()) {  &NB Sp Catalogue                         final File manifestfile = NE W File (Basefile, "meta-inf/manifest. MF ");                         if (manifestfile.exists () &AMP;&A mp Manifestfile.canread () && manifestfile.isfile ()) {   //manifest. MF file readable                             SYSTEM.OUT.P Rintln ("[DIST] found MANIFEST. MF "+ manifestfile);                             try {           &NBSp                 FileInputStream FileInputStream = new FileInputStream (manifestfile );                              Setclasspaths (base File, FileInputStream);                             catch (Filenotfou Ndexception e) {                             E . Printstacktrace ();                         }       &nbs P                         &NB Sp  } else if (Basefile.isfile ()) {//File (War)                   &NBSP ; }                }      &nbsp  }             super.startinternal ();             /**      * set classpath in MANIFEST.MF stream    & nbsp  *      * @param basefile      * @param inputstream     & nbsp;*/     private void Setclasspaths (File basefile, InputStream inputstream) {     &nbsp ;   String classpaths[] = null;          try {             final Manifest Manifest = new Mani Fest (InputStream);              final Attributes Attributes = Manifest.getmainattributes ();              String classpathvalue = Attributes.getvalue ("Class-path");              if (classpathvalue! = null) {         //can be non-null Description found Class-path      &nbsp           Classpathvalue = Classpathvalue.replaceall ("[\r\n]+$", "" "); Remove line breaks                  classpaths = Classpathvalue.split ("\\s+");    //split classpath string             }         } catch (Ioex Ception e) {             e.printstacktrace (),         } &nbs p;           if (classpaths! = null) { //If the classpath is found set classpath        &NB Sp     for (String classpath:classpaths) {                 Addrepos Itory (New File (Basefile.getparent (), classpath). Touri (). toString ());    //Convert relative path to actual path and convert to URI             }          &NBS P   System.out.println ("[DIST]" + basefile.getname () + "append" + classpaths.length + "Classpaths. ");            }        }     }  4. Put the shot Jar package to the%tomcat_hone%\lib. 5. Modify the Pom.xml of your War project, add the following configuration and run the MVN Package command: [HTML] View plaincopy<build>          < Plugins>              <plugin>                <artifactId>maven-war-plugin</artifactId>              & nbsp   <configuration>                      <archive> and nbsp;                        <manifest> &NB Sp                         &LT;ADDCLASSPATH&GT;TRUE&LT;/ADDCLASSP Ath>                            &LT;CLAsspathprefix&gt, .... /lib/</classpathprefix>                        &N Bsp   <useUniqueVersions>false</useUniqueVersions>                          </manifest>                      </archive>                  </configuration>              </plugin>          </plugins>      </build>   Observe the lk-example.war/meta-inf/manifest of the war pack. MF file, this file has a version of the jar that is exactly referenced by the war. Example: 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 the published project hasMultiple wars 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 packages when multiple projects share jars and specify jar versions 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.