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.
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.
The WEB-INF/classes and WEB-INF/lib directories contain Java class files and JAR libraries, respectively.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With