Adding server-specific options to an Eclipse-managed Tomcat instance (in server.xml) - eclipse

How can I make sure that the server.xml which Eclipse produces for every separate project that relies on Tomcat, will always contain a specific option, which I added manually?
I'd like to add a bunch of <Context> mappings to the server.xml file. I tried doing that in the server.xml in the main tomcat dir - didn't work.
Then I saw that Eclipse builds a temporary folder for every project that uses Tomcat. This folder also containes this server.xml file. I edited it, and voila, it worked. However, soon after that the same file got updated by Eclipse, with the original data that it contained.
It is not very efficient to manually copy and paste the code every time before I run/restart tomcat. I hope there is a more permanent way.

Let's assume you have configured tomcat 5.5
If you have configured your tomcat server in eclipse using all default options, you want to edit the server.xml located at WORKSPACE_LOC\Servers\Tomcat v5.5 Server at localhost-config\server.xml

Related

Tomcat and Eclipse vs Intellij

I am working with Intellij but some of my co-workers don't. When I was writing install doc, I realized that Tomcat is not managed the same way on the two IDEs.
Which is a problem considering what happened next when I tried to set up our project on Eclipse.
Basically, on Intellij, you select a Tomcat on your computer and it will literally copy the war into the webapps folder and run the server with everything working fine.
I am not a user of Eclipse so I might have misunderstood something, but I found that when you create a Tomcat server, it will embed the one you gave it to it. Doing that is a bit of an issue when you are working with logback, because usually you set your logs location directly into the Tomcat folder. And in Eclipse you are working out of this folder.
So, I can't run my application because it can't find the location of the logs folder at the fine place.
Is there a way to use Tomcat in Eclipse like Intellij? Or did I just miss something because I am kind of new with Eclipse?
See the FAQ: (1) (2)
I found that when you create a Tomcat server, it will embed the one you gave it to it.
You have to be more specific with your description. How you do things and what do you see. What do you mean by "embed"? What is the actual failure that you are observing with your logging?
There are different ways to do things.
For me by default Eclipse does not embed Tomcat, but runs it as a proper java process. (org.apache.catalina.startup.Bootstrap) You should be able to see it with jconsole and similar tools.
It runs your web application expanded, i.e. without zipping it into a war file. It creates a separate configuration of Tomcat, i.e. runs it with separate CATALINA_HOME and CATALINA_BASE directories (as documented in RUNNING.txt file of Apache Tomcat). The CATALINA_HOME directory stays untouched and CATALINA_BASE directory is ${workspace}/.metadata/.plugins/org.eclipse.wst.server.core/tmp0 etc. A logs directory can be found there.
One known caveat is that java.util.logging is not configured by default (the system properties java.util.logging.manager and others are not set). See "How do I enable the JULI logging" item in the FAQ. -- In the same way you will set any other system properties that you may need.
The default configuration of java.util.logging (as provided by JRE) is to log everything to the console, without creating any files.

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.

Where do you put server.xml in a tomcat eclipse project when checking into version control?

When you create a tomcat project in eclipse, it puts the server.xml file under Servers, which appears as a separate "Project" in the project explorer. My question is, how best to check this into source code control, so that when another user attempts to check out the project, the server.xml file is in the correct place.
I currently use the Perforce version control system, but I've experienced the same issue in the past with other systems.
It's been my experience that other users will end up creating their own tomcat server in eclipse, and then they will have to copy in the custom server.xml.
The best I've been able to do is save the server.xml into the WEB-INF or other directory inside the project, and then update that to source code control. However, this introduces risk, because you have to remember to save your server.xml changes back to WEB-INF. Not to mention that you have to document the copying of the server.xml as a setup step for users creating your project.
I would create a folder under the web app project like etc/ where I'd put a big README and a hard link to the server.xml. So anyone checking out the project would be instructed to do the same (in the README) - and all changes to the server.xml would be versioned. I would not mess with WEB-INF - the server.xml has no place there.

Deploy a War file with Eclipse and Tomcat 6 without a build file

I know there are a bunch of posts on this topic, but I can't seem to figure it out. Here is my setup:
- Eclipse Indigo
- Tomcat 6
- Struts WAR file that I imported into Eclipse from http://www.manning.com/dbrown/SampleApplication.zip
I added the server to Eclipse, and added the project to the server. It runs fine, but I'd like to make some changes to the XML and java files (not the jsp files). I was under the impression that Eclipse takes care of the deployment after I save and restart the server, but I guess this isn't the case. I looked into creating an ant file to do a custom build, but I'm not really sure how to make it since I don't even know how to do a deployment "manually". How would I go about doing a manual deployment? Thanks.
A war file is usually created by exporting it from an IDE like eclipse, netbeans, etc.
While generating a war file there is 2 options,
You can include your java files with it
You can avoid java files from it.
In both the cases class files will be there which are created from java files. Class files will be able to take care of the actions of java class.
If the war file which you down loaded is not having those java files, you will not be able to use it with eclipse, where as it will work fine if you are putting it into tomcat directory for running.
For running a war file manually just put it into tomcat webapps folder and run tomcat.
Then you can access the pages using the url.
http://localhost:8080/yourprojectname/
If you want to check whether the war file contains any java files, after starting the tomcat server it will extract the war file into the same directry, where you can search for java files.
If you want to run the project from eclipse,
import it into eclipse, open index.jsp page
right click-> run as-> run on server
if you have not yet configured the server yet,select the server in the list, specify its installation path, then add projects into server in next step, finish it. then it will load your project.
Please provide little more details so that we can help you better.
manual: copy the war in the tomcat folder then start tomcat.
if you want to use eclipse you need to click redeploy war first by right clickling the deployed war in the servers tab under tomcat.

tomcat do not unpack war file

i stopped tomcat
paste myapp.war into webapps folder.
start tomcat
call in browser
http://localhost:8080/myapp/index.jsf
see 404
also tomcat does not generate log files under logs directory.
I am using eclipse to start and stop tomcat, and my tomcat 7 is an unzipped version.
to be sure that my war file runs on server, i remove same project from tomcat in eclipse servers view.
repeating question, but i think my situation is a little different, tomcat does not generate logs.
a note: i generate war file by right click in eclipse-> export war file.
thanks for any idea.
If you are launching tomcat from within Eclipse (using the webtools tomcat server adapter) you will have to make sure you have changed the settings to use the installation directory to launch instead of the default which uses a separate location for loading and deploying webapps. So if you add a war directly into the webapps folder, the server launched from Eclipse server adapter wont have that directly setup in the server.xml configuration.
So you should use the startup.bat/startup.sh in the tomcat/bin folder. If you do, you need to make sure that you don't just copy the .war file into the webapps, but rather unzip the war file into a directory folder called "myapp"