0

I would like to know if there is a good way to build JAR files with Maven and after that copy them into %TOMCAT%/lib folder.

What I want to achieve is that every time when I deploy my WAR in the cloud (using Jenkins and Cloud Foundry), I get all JARs needed by Tomcat copied in its lib folder.

My use case is, my application uses Redis to store sessions (to be cloud ready), and I have added this configuration to %TOMCAT%/context.xml file

<!-- session persistence is handled by Redis Data Server -->
<Valve      className="com.gopivotal.manager.SessionFlushValve" />
<Manager    className="org.apache.catalina.session.PersistentManager"
            host="localhost" 
            port="6379"
            database="0" 
            maxInactiveInterval="60">

    <Store className="com.gopivotal.manager.redis.RedisStore" />

</Manager>

Sessions are stored like a charm when I copy manually redis.jar, commons-pool.jar and jedis.jar in %TOMCAT%/lib, but I want to do that automatically based on the best practice.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
  • 1
    Why?! A WAR should package its own libraries inside `WEB-INF/lib`. It is not recommended practice to put your own JAR inside Tomcat lib folder. – Tunaki Jun 26 '16 at 12:21
  • @Tunaki: these librairies are to be used by **TOMCAT** and not by the application itself, if i let the WAR package these libraries inside WEB-INF/lib so TOMCAT will not start and will throw `NoClassFoundException for com.gopivotal.manager.SessionFlushValve` – Halayem Anis Jun 26 '16 at 12:35
  • I see. You shouldn't modify your Tomcat `context.xml` for one project that uses `com.gopivotal.manager.SessionFlushValve`. Your WAR should contain the `context.xml` itself; for that you can use [`containerConfigXML`](http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html#containerConfigXML) of `maven-war-plugin` configuration. – Tunaki Jun 26 '16 at 12:43
  • @Tunaki i've found a solution to copy the necessary libraries in TOMCAT\lib on each deployment ( Servers on cloud foundry are volatile ). It is the best solution to satisfy TOMCAT dependencies ? Also, i've tried to create my own `context.xml` under `META-INF` but it seems that TOMCAT does not take care of it, the only way for me was to modify `TOMCAT\conf\context.xml` i have missed somthing i guess – Halayem Anis Jun 26 '16 at 12:49
  • No, it is not. As said before, you should almost never copy JARs into Tomcat's lib. Keep the changes contained to a single project: make the project's WAR contain all its dependencies inside `WEB-INF/lib` and make the project's WAR have a custom `context.xml`, specified with `containerConfigXML` (see above comments, if that didn't work you could create another question with this). Let Maven create the appropriate WAR that you deploy, without fiddling with the installation of your server. – Tunaki Jun 26 '16 at 12:51
  • @Tunaki http://stackoverflow.com/a/6138851/4098311 – Halayem Anis Jun 26 '16 at 21:22
  • I stand corrected then, did not know that. In that case, if the JARs that you need to copy are dependencies, you can use the [`maven-dependency-plugin:copy-dependencies`](http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html) to put them in a temp folder under `target` and then use `maven-antrun-plugin` to copy them into Tomcat. Make sure you set the scope to `provided` also. – Tunaki Jun 26 '16 at 21:29

0 Answers0