Deployment of Web Application to a Running Tomcat - deployment

I would like to collect some best-practices on deployment of a web-application to a running Tomcat. Not long ago I had to describe the deployment process of our web-application and the process appeared rather confusing.
Say, we have an application in a WAR file (foo.war) correctly configured and not requiring additional configuration. In this case, the deployment process is rather easy:
Copy the foo.war file to the $CATALINA_HOME/webapps directory. If the application starts correctly, the application will automatically deploy to $CATALINA_HOME/webapps/foo directory.
To undeploy the application:
Remove the foo.war file from the $CATALINA_HOME/webapps. If the application unloads correctly, it will be unloaded and the $CATALINA_HOME/webapps/foo will be removed.
Now I want to override some context parameters in my running application. Having read the docs, all I need to do:
Create a context.xml file called foo.xml
Copy the file to the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory.
Unfortunately, that did not work: the application would not restart. Empirically, we found out that the only working solution is when the war file is deployed to a location outside the $CATALINA_HOME/webapps.
Besides, the default values of the configurable context parameters in the WAR file should be specified in the web.xml, since context.xml in the WAR file is not read when there is a context.xml outside.
Here is an easy example of the foo.xml:
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="/path-to-deployment-directory/foo.war">
<Parameter name="myparam" value="newvalue" override="false"/>
</Context>
Be sure to specify override=false for the parameter if you want the 'newvalue' to override the value specified in the WAR's web.xml. This was not obvious for us.
Thus, to deploy an aplication to a running Tomcat:
Create a context.xml file called foo.xml
Copy the file to the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory.
Copy the foo.war to the location specified in the docBase of the foo.xml; the application will deploy automatically.
To apply new context parameters:
Add the parameter values to the foo.xml and save the file; the application will re-deploy automatically.
To undeploy the application:
Remove the foo.xml from the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory
Note that removing the foo.war will also work, but will remove the foo.xml as well.
By now, I have the following questions:
Is it a best-practice at all to deploy a web-application without stopping the tomcat? I heard an opinion that deployment to a running tomcat is never needed since people run each application in a separate tomcat.
Is it a good idea to copy WAR files to $CATALINA_HOME/webapps or they should better be kept in a separate location?
How can I configure an application deployed to $CATALINA_HOME/webapps
Why there is no INFO line in the catalina.out for deployment of an application and there is one for undeployment? Is it configurable?

On question (1), Tomcat works great for deploying servlets into a running server. There may be concerns w.r.t. security or possibly D.O.S. or provisioning reasons why you would have separate server instances.
You have the flexibility to do either way, but it is often more convenient to deploy to an already running server. This is a BUILT-IN feature in the servlet architecture. :)
For (2), again it is at your discretion where you you want to put WARs. It sounds like you already have it configured a non-standard (non-default I should say) way. Check your server.xml file for the settings in your server instance(s). Check for attributes like unpackWARs and autoDeploy.
For (3) and (4), plus your (1,2) questions, it might be a good idea to consult the Tomcat docs for your version of Tomcat on its deployment model. You should be able to use the same docs to figure out how your server has been configured.
See Tomcat Web Application Deployment in the Tomcat manual, adjusting for your version of Tomcat.

One solution would be to use the manager application. If you decide that is safe to use it, then you can easily deploy, start, stop and undeploy applications:
http://localhost:8080/manager/deploy?path=[context_path]
http://localhost:8080/manager/start?path=[context_path]
http://localhost:8080/manager/stop?path=[context_path]
http://localhost:8080/manager/undeploy?path=[context_path]
There are ant tasks that can help you with these.
I am guessing, but do not know for sure, that stopping and starting an application will make it reread the context.xml.
Regarding your second question, I believe it is better for maintenance reasons to keep the war files in the webapps directory.

Related

Make per-context JNDI variable available to Tomcat in Eclipse

I'm using Tomcat 8.5.6 inside Eclipse 4.6.1. I have my web-app project/context foo, which has a JAX-RS (using RESTEasy 3.1.0.CR3) endpoint of bar, so I can fire up Tomcat inside Eclipse and access:
http://localhost:8080/foo/bar
I have a variable named foobar which I want to access inside my JAX-RS implementation using JNDI:
final String foobar = (String) new InitialContext().lookup("java:comp/env/foobar");
I plan on deploying the produced WAR in production using Tomcat autodeploy. I want to configure the foobar variable for Tomcat externally to the WAR. How can I do that so that I can test it in Eclipse?
After a lot of reading, I found what I thought to be the $CATALINA_HOME of Eclipse: …\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\. So I created a context file for foo at …\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\foo.xml to correspond to my project/context, and put the following inside it:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Environment name="foobar" type="java.lang.String" value="123"/>
</Context>
Yes, I know that Eclipse erases this directory whenever I rebuild. But after building, I saved to file at least want to see if it works. It doesn't. I get an error:
javax.naming.NameNotFoundException: Name [foobar] is not bound in this Context. Unable to find [foobar].
I want to at least get it working so I can know how to do this in production, and worry later about the context file deletion thing in Eclipse. So what did I do wrong? Why can't Tomcat in Eclipse find this JNDI variable?
Note: I am not using a web.xml file and have no desire to do so; besides, this variable should be defined outside the WAR in the production deployment.
Update: The good news is that (on Windows 10 Professional Anniversary Edition 64-bit) using the same Tomcat but in standalone mode, I put the same foobar.xml file inside the standalone Tomcat's conf\Catalina\localhost\foo.xml, and my JAX-RS application picked it up just fine. So how can I define a JNDI variable in Tomcat inside Eclipse for testing?
It appears that in order to get Eclipse+Tomcat to recognize the per-module context files, you have to go into the server configuration (double-click on the server) and turn on the Publish module contexts to separate XML files. This way Tomcat will use the specific context XML file you created. Otherwise it apparently puts them in conf/server.xml and ignores the context-specific file you created.
There is still the problem that Eclipse regenerates this file each time you do a rebuild, destroying whatever JNDI variables you placed there. I'm trying to get the workaround in https://stackoverflow.com/a/22380248/421049 to work, but not yet succeeding. Anyone have any better ideas?
At least I'm able to reproduce a production environment now --- albeit temporarily, until the next rebuild.
Your link to Markus' answer on https://stackoverflow.com/a/22380248/1794485 allowed me to get this working, or at least as described in his workaround. But the remaining problem to solve was ordering.
As he said, you can workaround this by having a local copy of the META-INF/context.xml somewhere else, and adding this folder to the Deployment Assembly in the project properties of the Eclipse project.
This didn't pick up for me initially though. It looks like that while the Deployment Assembly in the properties shows as sorted by name, in fact it has an order like any other path. When I then removed the src/main/webapp entry (so the one containing the normal META-INF/context.xml) and added it back in, this effectively moved it down the pecking order. The next Tomcat deploy and startup in Eclipse finally put my preferred copy of META-INF/context.xml in .metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myapp\META-INF
If in doubt about the true sequence of that Deployment Assembly path, have a look under your Eclipse project on the file system - at .settings\org.eclipse.wst.common.component.

Can't get past Welcome to Jboss page Openshift

I'm trying to use Openshift to host my java webapps. The problem I am running into is every time I go to my application "http://omniticketmvc-leviliester.rhcloud.com/" it takes me to a "Welcome to your JBossEWS (Apache/Tomcat) application on OpenShift".
I thought maybe it was because my project had some sort of default .war that was being deployed instead of the one I wanted. To try to confirm that I followed this guide made by to deploy a pre-compiled War file. https://www.openshift.com/kb/kb-e1088-how-to-deploy-pre-compiled-java-applications-war-and-ear-files-onto-your-openshift-gear .
As you can imagine that did not work. The guide implies that I should be able to find my webapp running at app-domain.rhcloud.com/mywebsite with "mywebsite" being the name of the war file my project created. In this scenario my Application war file is name "OmniTicket". I can find that war file on the server using ssh but the directory hierarchy is confusing to me.
I also tried looking in logs on the server but I don't see any errors to indicate a malfunction in spring or database connections. Any help would be appreciated. Specifically when I deploy my application to the Jboss Server without any obvious errors, why can't I get to the application root?
I should also mention it is a SpringMVC restful service application that works locally.
Try following steps:
Rename your war name to ROOT.war
Delete the src and pom.xml. If pom.xml is present then OpenShift would try to build the maven project
Place the war in deployments folder under your application root folder
Commit the war and push changes to application Git repository.
Check the logs using rhc tail command

WAS related files getting packaged in ear

I have got an ear file to deploy on WAS. In the ear file, I see the war file, the jar files and other static stuff. But I also see files like variales.xml,security.xml and deployment.xml although the ant build script did not generate these files.
Where did these extra files come from? What purpose do they serve?
These files are part of a feature known as Enhanced EARs. See the WebSphere Application Server V7: Packaging Applications for Deployment redbook for more information. In short, application-specific configuration can be included in the application to minimize the per-server configuration that an administrator must perform when installing an application.

How to deploy war on websphere 8 without using Admin Console?

How do we a deploy a simple war file on the websphere server 8 without using the websphere administration console?
As a part of the manual deployment I will need to know,
where to put the war file manually on the server?
war uses jndi for db connection, since we do not want to use admin console,
how do we create jndi data sources?
we would also want to externalize the properties file. (like I used to keep
application.properties in JBOSS_HOME/server/default/conf folder in jboss instead of the app.war/web-inf/classes)
Please help
PS: Actually we will be having an ant build which will do these three things for us so that we don't have to go to the admin console. Just run the ant build and it will copy war, create jndi etc stuff.
The best way of doing what you want is writing a wsadmin script.
Using wsadmin you can deploy, add/delete/modify resources in WebSphere, pretty much anything.
I would suggest you read Getting started with wsadmin scripting
You can also use 'monitoredDeployableApps' folder under server profile. You can enable this feature from the admin console -> Applications -> Global deployment settings.
Simple and best way to do this is in two steps: (v 8+)
Enable 'monitoredDeployableApps' feature in Admin Console -> Applications -> Global Deployment Settings and restart the server. (once restart you will see a folder in your profile 'monitoredDeployableApps' (default name and can be changed while enabling this feature)
Drag and Drop your war file in this folder (no restart needed) and observer Systemout.log of the server. Verify in Admin Console for the deployed application.
NOTE: Make sure your context-root is populating to your deployable (war,ear,...) files.
-- Prakash Karri

How to run .ear file in JBoss 6?

I have created myProj.ear file and copied it into the deploy folder of the JBoss server.. How to run my project after starting the jBoss server?
I have been using war file and
deploying it in Tomcat till now to run
my project.... I am having a new
requirement to run the project in
JBoss. So, I converted my war file
into an ear file using the command Jar
-cvf myProj.ear ., Should I change anything in my project to run
the application in JBoss or just
copying my .ear file in to the jBoss
deploy folder is enough?
JBoss normally support hot deployment - meaning that if your application was deployed correctly (watch the console), it can be accessed via the browser (if you have a UI) or via web services, managed beans or any other interface you have provided.You can see the status of your application on the JBoss Admin Console. You can reach it by typing the URL of your JBoss installation. If you run your vanilla JBoss locally, you should be able to find the console under http://127.0.0.1:8080/admin-console
To reiterate: there is no explicit startup necessary, JBoss handles it for you.
Deploying an application in JBoss is pretty straightforward. You just have to copy the EAR file to the deploy directory in the 'server configuration' directory of your choice. Most people deploy it to the 'default' configuration, by copying the EAR file to the JBOSS_DIR/jboss-as/server/default/deploy directory.
Once copied, just run run.sh from bin, you can use the following params to bind it to an ip (-b) or binding it to anything other port (-Djboss.service.binding.set)
./run.sh -b 9.xxx.xxx.xxx -Djboss.service.binding.set=ports-01
Once you run it, Look at the console for error message. If everything goes fine you'd see "Started J2EE application" after couple of seconds.
If there are any errors or exceptions, make a note of the error message. Check that the EAR is complete and inspect the WAR file and the EJB jar files to make sure they contain all the necessary components (classes, descriptors,
jboss-deployment-structure.xml etc.).
You can safely redeploy the application if it is already deployed. To undeploy it you just have to remove the archive from the deploy directory. There’s no need to restart the server in either case. If everything seems to have gone OK, then point your browser at the application URL.
http://localhost:8080/xyz