Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy JAR files to existing webapp subdirectory?

Tags:

java

maven

jar

Let's say I have a very ordinary web app deployed via mywebapp.war, generated by Maven:

webapps
  |
  |--mywebapp.war
  |--mywebapp
       |
       |--images
       |--js
       |--jsp
       |--jardeploy
       |--META-INF
       |--styles
       |--WEB-INF

Now I would like to deploy myjarfile1.jar and myjarfile2.jar (or more) in webapps/mywebapp/jardeploy. They are artifacts of another Maven project, not the one that makes the war. They are also not jars that mywebapp uses, which would be under WEB-INF/lib, but rather downloadable jars built separately and not part of the mywebapp source code.

Is there a way to package an archive bundling the two or more jars in Maven which, when dropped in webapps will get deployed not in default webapps/jardeploy but in webapps/mywebapp/jardeploy? It would be the same as though I took the jars and copied them in that directory but I would like to follow the standard app server deployment protocol by dropping a generated artifact in webapps rather than the hacky former approach. I actually want a formal artifact (just like the war) instead of a post deploy add-on. Is there a way to tell (perhaps in the manifest) that it needs to unpack the jar and put it in that directory, if that directory exist?

The container shouldn't matter but it is Tomcat 7.

like image 446
amphibient Avatar asked Apr 20 '17 20:04

amphibient


People also ask

Can JAR file be deployed?

Tomcat JAR deployment options There are three recommended options to make this happen: Package the JAR file in the WEB-INF\lib folder of the Java web application; Place the JAR file in the \lib subfolder of the Apache Tomcat installation; Configure a folder for shared JAR files by editing Tomcat's common.

In which directory on a Web application where we can put a JAR file?

The WEB-INF/classes and WEB-INF/lib directories contain Java class files and JAR libraries, respectively.

How do I put all JARs in a folder classpath?

In general, to include all of the JARs in a given directory, you can use the wildcard * (not *. jar ). The wildcard only matches JARs, not class files; to get all classes in a directory, just end the classpath entry at the directory name.

Where do I put JAR files in tomcat?

Classes and jars inside the ${catalina. base}/lib and ${catalina. home}/lib folders (which by default are equal to the lib folder of your Tomcat installation) are loaded by the common classloader.


2 Answers

This looks like a standard use-case for maven-war-plugin and extra resources, unless I'm missing something. For example here (See section Configuring web resources) tells you that you can include additional resources in your war build (and this is what your two jars are to me : additional resources)

If this doesn't work for you then maven-assembly-plugin allows you to specify a much more flexible format.

like image 72
Andrei Avatar answered Sep 25 '22 01:09

Andrei


You could create a webapp with a WebListener that copies the jar files to mywebapp/jardeploy directory. So when this application is deployed in your container it will copy the jars to the place you want.

I tested and it works! Supposing you have created this new webapp and have put the jars in src/main/webapps/jars directory, the WebListener look likes this:

@WebListener
public class Deployer implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        String realPath = sce.getServletContext().getRealPath("/jars");
        try {
            Files.copy(new File(realPath + "/myjarfile1.jar").toPath(), 
                new File(System.getProperty("catalina.base")+"/webapps/mywebapp/jardeploy/myjarfile1.jar").toPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("On shutdown web app");
    }
}

This code is just a proof of concept and can be enhanced, but it servers to its purpose. It only copies one jar (myjarfile1.jar) to /webapps/mywebapp/jardeploy. PS: I created this new webapp using maven archetypes.

like image 45
viniciusartur Avatar answered Sep 23 '22 01:09

viniciusartur