I compiled a Java application into a WAR file, there is a configuration file inside the WAR file. The configuration file is required to change something after deployed to the production server, because it still contains the UAT server parameters.
However, I don't know where to edit the configuration file after deployed in Tomcat. Please help. Thanks.
It doesn't sound like a correct design. You should -
Load configuration file based on some System parameter (e.g. -Denvironment=UAT or PROD). This will be the decision factor for loading the right configuration file.
Do not package the file inside war itself, if possible externalize it to some other directory where amending is lot easier.
Related
According to what I know Java Web applications are deployed on a server by deploying the .war file of the web application. Is this correct?
I have a Web application that has some configurations to allow the server administrator to configure the application at the time of deployment. Ex. Set the path where log files are to be saved, Set the location to store uploaded documents etc..
I defined such parameters in a .properties file and hardcode the path to it. Thereafter I was able to get the web application to read the values in the .properties file without recompiling the source code (as the .properties file path did not change).
Now I want to know how to do this without hardcoding the path to the .properties file so I can deploy this on any server without worrying about its directory structure.
So, is it possible to provide a .properties file along with the .war file when the web application is being deployed so that the server administrator can edit the .properties configuration file, deploy the .war file (without recompiling the web application) on a server and let it read the values in the .properties file?
If so how can the web application know where the .properties file is?
Best practise is to put the properties file at a location where it can be found on the classpath. Your application loads the file with
this.getClass().getClassLoader().getResourceAsStream( "myConfig.properties");
If the file is located in a non standard classpath location you can finetune your applications classpath using tomcats configuration possibilites. See e.g. Understanding The Tomcat Classpath
My java application has more versions of external context.xml, one for each environment. The plan is to have one war file and using the right context.xml for production, staging, dev, etc.
I currently copy/symlink the context.xml into /usr/local/apache-tomcat-7.0.72/conf/Catalina/localhost/myapp.xml. Then I create war file from my sources and I copy it into /usr/local/apache-tomcat-7.0.72/webapps. The catalina is running and explodes the war file in webapps folder. In the same time it deletes my myapp.xml from conf folder. For that reason my app doesn't load the proper configuration and fails. If I copy again the myapp.xml and restart tomcat it works.
Is there a way to tell tomcat to not delete my conf file when extracting war file? Or is there a better place for external context.xml
I guess you have some params in this context.xml for production or stage etc. It's not a good idea to store them there as better approach is to start server with properties and read them in webapp so you never make mistake a.
Look here: https://eureka.ykyuen.info/2009/12/15/tomcat-setting-java-system-properties-for-webapps/
I have a spring application packed in war file. If i deploy in tomcat, i can go to file directory and find properties and xml files, and i can make changes, for example the name of the JNDI conexion, i restart the server, and everything is ok with new value. But now i have to deploy my app in weblogic 10.3, i can deploy and it runs, but if i want to change some properties of one properties file, i have to make a new war file and deploy again.
So my question is, how can i find this files without making a new war file? What is the best option to save this properties than can be changed in the future?
I want to make only one deployment, and after this, if changes properties are needed, any user can make this changes as easily as possible, for example changing a value of properties file.
Thanks!
You're probably looking for Weblogic deployment plans.
Read http://docs.oracle.com/cd/E11035_01/wls100/deployment/config.html#wp1065363 for details.
Question 1: May I ask what is the difference between deploying a java webapp with it's WAR file vs just copy/pasting the build folder into tomcat webapp folder?
Question 2: Somehow I am told to deploy my project just by renaming my /build/web folder to /build/, then copy and paste this folder into tomcat/webapp folder. Tomcat did serve the web app and I could access it via url. But the problem is that I suspect my System variables were not set. I start up a servlet and put this code in this init(ServletConfig config) method:
System.setProperty("LogPath","D:/Test/logs");
And doing this in my log4j.properties
log4j.appender.file.FILE=${LogPath}/wrapper.log
wrapper.log is not found in the designated directory but a stdout.log is found in tomcat/logs folder.
I am sure the init() method was fired because I have a quartz scheduler there. I am suspecting that my System.setProperty was not set. Any hint?
Update: With all the same source code, I have no problem if I am deploying with a WAR file. The ${LogPath} in log4j.properties work as expected.
Let me answer you the first question.
WAR file is a zip archive with different name. When you deploy this file to the Tomcat server, it unpacks this file to its folder as you would do it by copy-paste. If you are just developing your own project in your own environment and you don't want to distribute it, you don't need to create a war file. But if you want to distribute this project, I recommend you to create a war file. One file is easier to be sent.
Read more on Wikipedia http://en.wikipedia.org/wiki/WAR_%28file_format%29
Currently when I deploy a war file to Tomcat it can be downloaded from the URL via something like foo.com/myapp.war.
Most places recommend that you put an entry in a .htaccess file to prevent public access to any war files, or failing that an equivalent entry in your Apache config.
Unfortunately, my host does not provide access to the Apache config (although I can access Tomcat confs) and .htaccess files do not work for all Tomcat/Java related hosting environments. Pretty disappointing. They have been rather unhelpful in this respect.
Without resorting to something like "finding another host" (other than this issue they are fine - I'd rather stay here until my app grows too big), is there anything else I can do to prevent public users accessing my war files, yet still allow Tomcat to deploy the apps when it scans them?
For example, is it possible to specify one directory for Tomcat to scan for war files yet have it deploy the war into the public directories?
Thanks.
It is probably better to ask at https://serverfault.com/. It all boils down to how Tomcat is setup.
The vanilla setup will have a folder called webapps under CATALINE_HOME. You put your WAR archives there (they get auto-extracted and deployed). These folders will not be accessible from HTTP (you cannot download WAR archives from some URL like /webapps/my-test.war). These apps in webapps folder are deployed to some context roots. For example an application my-test.war will by default get deployed as yourhost.com/my-test/.
If you can download your WAR archives from foo.com/myapp.war maybe you can check out what does the CATALINA_HOME/webapp/ROOT app is doing. By default this is deployed under the foo.com. Ask from the host the Tomcat configuration files to figure what kind of custom configurations are they using.
You can place your .war files in any location Tomcat has access to. But you will have to tell Tomcat about it, so it picks them up. You can do this by placing a configuration XML file in
<CATALINA_HOME>/conf/Catalina/localhost/myWebapp.xml
There are samples on what to put into that file myWebapp.xml, e. g. here, step "4)". And of course, the official documentation.