0

I'm developing two separate Java web apps - A is website, B is backend service. Both are deployed on different machines running Tomcat.

A accesses B using REST calls.

I have separate machines for development, test and production. How can I manage deployment on development, test and production environment for these two apps? I constantly have to change URLs in both Java and JavaScript code to point to the correct server before I deploy the apps.

Options considered: 1) I've considered using system environment variables as described here. This would work for the Java side, but in my website A there's JavaScript code that invokes the REST API of backend server B. I don't know a way to access the URL defined on Java level from my JavaScript code. Is there one?

2) Use placeholders for the URL in the code that get replaced with the actual URL during a build (ant?). Again, not sure if this will work for the JavaScript code though.

This must be a common problem, but further googling didn't give me concrete hints on what people do to solve this. Appreciate any pointers.

toby88
  • 105
  • 2
  • 5

2 Answers2

2

Having a simple ant build with a Replace task to replace the API endpoint URL strings in your javascript files would do. You would maintain a separate configuration setting for dev,prod,etc, in your build configuration files and you can fire a build with that configuration as an option.

Take a look at this SO answer regarding filterchains to do a replacement when you copy your JS files to their final deployment destination.

In your JS file:

var APIURL = "@APIURL@";

Here's an example of the replace operation:

<!-- holds your API endpoints based on configuration parameter -->
<property file="${config}.properties" />
<!-- does the replacement -->
<copy src="/path_to_main_js_file" dest="/path_to_final_package_js_file"
   <filterchain>
     <filterreader classname="org.apache.tools.ant.filters.ReplaceTokens">
       <param type="token" name="APIURL" value="${apiurl}"/>
     </filterreader>
   </filterchain>
 </copy>

For example

ant -Dconfig=dev package

Or you can also use different targets for dev, prod, staging that internally load the correct configuration files.

ant package-dev

Your approach with environmental variables on the Java side seems like a good approach.

Community
  • 1
  • 1
ricosrealm
  • 1,616
  • 1
  • 16
  • 26
  • I'm developing in Eclipse. I suppose I can set the same environment variable inside Eclipse for the Java side. Any ideas how I can replicate in Eclipse whatever my Ant replace task does to my JavaScript? – toby88 Feb 23 '13 at 20:16
  • See my edit above re: Ant specifics. Re: Eclipse integration, this might help: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2FgettingStarted%2Fqs-84_run_ant.htm – ricosrealm Feb 23 '13 at 20:27
  • While the Ant based replacement certainly would work I did not like having to build separate WAR files for my environments. I decided to take a different route through having my website detect where it's currently running (JavaScript window.location.hostname) and issuing the backend server requests accordingly. Appreciate your pointers though. – toby88 Mar 04 '13 at 07:09
0

ZooKeeper is a good way to manage configuration, though likely overkill for your needs.

Like the solution you mentioned, generating URLs at build time with ant is another viable solution. I have used this (as well as ZooKeeper) firsthand; it is simple but not without its downsides. At the very least it means you need separate builds for dev, test, and prod.

If you're not using separate builds you will need some basic hook into what environment the code is running in. System environment variables work just fine for this.

The Twelve-Factor App's Config section isn't a direct answer to your question, but is definitely a worthwhile read.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • As you've pointed out ZooKeeper is a bit overkill for me. The Twelve-Factor App link was very informative. Thanks for sharing. – toby88 Mar 04 '13 at 07:10