I have two applications deployed on tomcat. For each application I have defined their own context in $CATALINA_HOME/conf/Catalina/localhost where JDBC datasource is defined. When I undeploy one of the application using ant script context of this application is deleted, so on the next start of the tomcat application cannot be deployed because it doesnt have JDBC connection defined. Is there any solution to this problem?
I think this is a Tomcat bug. I filed a bug report but the fix is complicated.
Tomcat can deploy application in 3 ways,
Directory, like webapps/myapp.
WAR, like webapps/myapp.war.
Context fragment, which is what you are using.
If you use #3 but the app or war are in webapps, Tomcat will be confused with #1 or #2. When redeploying directory or war, it supposes to remove context fragment.
My workarounds are,
If you use directory, put it somewhere other than APPBASE (webapps). If you use WAR, put it somewhere else also and don't explode it.
We deploy our app using a script. In the script, the fragment is copied over every time after app is undeployed.
Here is a sample fragment for WAR deployment,
<Context docBase="/anywhere/but/webapps/myapp.war"
swallowOutput="true" unpackWAR="false" />
Please notice not every app runs in unpacked mode. You can't read any resources as files from the WAR in unpacked mode.
Related
Is there a way to start/stop .war file from inside another .war?
I'm running SymmetricDS server using Tomcat 8 (it deploys it's own .war) and I need to start it, when a button is pressed and Stop likewise. Can I do that?
When a war file is "dropped" into Tomcat's webapp folder, by default Tomcat automatically deploys it and starts it. When source war file is deleted from the webapp folder, Tomcat automatically stops and undeploys the webapp.
So basically all you need to do is copy and delete the war file to/from the webapp folder to start/stop a webapplication.
Also (or if you can't do this) Tomcat has a built-in manager webapplication which is capable to deploy new applications (from war-files), or to stop and undeploy running web applications.
See Manager App HOW-TO for more details on this.
If you want to do this from your code, check out the ManagerServlet class. You can call it with simple URLs and parameters. The javadoc of the class contains example URLs what you can do with it. Here are 2 important operations specifically to your needs:
/start?path=/xxx - Start the web application attached to context path /xxx for this virtual host.
/stop?path=/xxx - Stop the web application attached to context path /xxx for this virtual host.
I wrote the web application using Spring MVC. When I deploy the app to the server path is localhost:8080/projectName/. It is possible to remove projectName from path from eclipse? I found path in project properties but I cannot find the way how to change it.
I found that I have to change context root to : "/". I changed it but that has no effect.
Thank you for any help.
Regards,
Sebastian
I would break down your question into two parts :
A. Changes to the context root not taking any effect.
For changes to the context root to take effect, you must clean and republish you webapp on your server for the context root changes to get activated. To run "Clean" in context of the server from within eclipse,
Stop the Server
Window -> Show View -> Server -> Right click on your server configuration -> Clean. (Note: To "Clean" the server outside of eclipse, you need to go to the "webapps" directory of your server on your local filesystem and delete the .war file as well as the "project-name" folder which holds the exploded WAR file. )
Deploy your webapp to the server and restart the server.
B. Trying to run your webapp at the context root / - localhost:8080
From what it looks like, you are trying to run your web application at the "Root" of your application server. You haven't mentioned the application server that you are using, but let us for example assume that the server you are using is tomcat. For tomcat, to deploy an application which will run at localhost:8080/ you need to either deploy the exploded war under the "ROOT" directory at $CATALINA_HOME/webapps/ROOT , or name your war file to be root.war.
The $CATALINA_HOME/webapps/ROOT and $CATALINA_HOME/webapps/root.war are special keywords which tell tomcat to deploy the application at content root /.
If you are NOT using tomcat as your application server, then provide more details on the application server being used to see if I can help.
I have an application/war deployed in server. Now at runtime I want to add an xml document to the war/application. can I do that? if yes, what is the path of an war/application for it to be added.
You have to repackage the WAR, redeploy, and bounce the server. It's not that simple.
You can make that data available without the hassle if you put it in a database and have your application access it there.
in the webapps/WEB-INF folder you can find the xml files
It depends on the servlet container/application server.
If you are using Tomcat, wars are getting unpacked to the webapps directory inside the Tomcat directory.
Move webapps/app directory outside the webapps directory, what will cause an undeploy of app
Put your xml file into the app directory
Move app back to webapps what will cause a deploy
I have a web application deployed on tomcat 7. Its context file, named myAppName.xml is located in
$CATALINA_HOME/conf/Catalina/localhost folder.
The problem I'm facing is that on each overwrite or undeploy, my configuration file is deleted from $CATALINA_HOME/conf/Catalina/localhost folder and I have to copy/paste it from a backup location.
How can I prevent this behavior? I don't want to copy/paste context file everytime I change something in webapp.
Put it into web/META-INF in the source tree, then Tomcat will copy it out of there into conf/.... on each deploy. Tomcat 7 won't even do that by default, just use it where it is.
I want to be able to deploy code changes to Tomcat (near instantly), while I'm developing in Eclipse.
So far, I have my output from Eclipse placing the built classes in the WEB-INF/classes folder of my web application.
I also have a reloadable context, with the web.xml as a watched resource. Any edit / save to this file does reload my web app, taking just over one second - much quicker than building a new war file and deploying it in full.
However, what I'd like to do is trigger the redeploy when I edit any source file. As the .class files are being modified in Tomcat, it seems I just need to monitor any changes in the WEB-INF/classes folder and it's children.
I've read that I can add additional watched resources in Tomcat's context.xml but this doesn't seem to be quite what I need - unless it's possible to specify a directory that will be watched (including recursively monitoring sub folders and files)?
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/someother.file</WatchedResource>
<Manager pathname=""/>
</Context>
So essentially, my question is can I watch the entire classes folder (without including each WatchedResource explicitly) to trigger a redeploy in Tomcat?
If not, can I configure Eclipse to touch the web.xml file, whenever I save a source file in that project? I'm developing on a Windows system. :(
PS I'm not interested in the JRebel product. Any answer should be a free solution.
Update
According to the Tomcat documentation, the classes folder should be monitored by setting the context to reloadable:
Set to true if you want Catalina to
monitor classes in /WEB-INF/classes/
and /WEB-INF/lib for changes, and
automatically reload the web
application if a change is detected.
Only changes to the web.xml seem to trigger a reload. Is this a bug or is my setup incorrect?
Also, I've read about setting the docBase attribute for a given context:
docBase="webapps/someExample"
This appears to be close to what I need, as I could then republish in Eclipse quickly. My only problem is that I require several web apps / servlets to be running in Tomcat concurrently, on the same port etc.
For these cases I set the eclipse build output to WEB-INF/classes as you have done and create a context file with the docBase set to the webapp folder (parent of WEB-INF) in the project. This is manually placed in conf/Catalina/localhost (assuming default configs for some elements of server.xml). End result is tomcat ends up serving from the development directory. So, change a servlet or other class and it is updated to the classes folder. Save a jsp and it is immediately available.
If project structured like:
src
|-main
|-webapp
|-images
|-js
|-WEB-INF
|-jsp
|-classes
Then context would be like:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/path" reloadable="true"
docBase="<pathtoproject>/src/main/webapp" />
Maybe the Web Tools Project of Eclipse with auto-redeploy enabled will help you? Add a server, open properties and under Publishing you will see a radiobutton saying "Automatically publish when resources changes". This will result in a redeploy if classes changes otherwise just overwrites resources. You can install WTP via a built in update site (Eclipse only), so check out your software updates. It is free for most servers but it does not support certain Websphere features?
Try the Spring Loaded JVM agent I've described in the following answer:
https://stackoverflow.com/a/37064672/1034436
While that has worked for my Spring web application, this should work with vanilla Eclipse + WTP + Tomcat + Dynamic Web Applications since Spring Loaded works on the JVM/classloading level.
You will still need to use the "Automatically publish when resources changes" as mentioned by #toomasr in his answer. However, you must also disable "Module auto reload by default" option as well. If you already added/published modules from Eclipse to Tomcat, then disable "Auto Reload" for each web module (via the Tomcat config page's Modules tab). That should prevent Tomcat from reloading the application when a single class file is updated, which I suspect is what all that reload/wait time is.