Using Constants based on Environment

92 views
Skip to first unread message

harshyadav

unread,
Sep 30, 2016, 12:19:06 PM9/30/16
to GWT Users
Hi,

I have a client side configuration file Config.properties, in which I store some client properties for the application.

I use this file as:

Config.properties:
key = xyz


Config.java:
public interface Config
 
extends Constants {

 
String key();
}


Now, I want to have 2 versions of this, for dev and production environments.

I differentiate between the environments using the custom properties in different *.gwt.xml files:
<set-property name="env" value="dev"/>
<set-property name="env" value="prod"/>

How can I use 2 versions of the properties (Config_dev.properties and Config_prod.properties) file mapped to the same Java class (Config.java).

I tried doing it with localization (via setting locale property), but I get other localization stuff with it that I don't need.

Thanks,
Harsh

Jens

unread,
Sep 30, 2016, 6:20:58 PM9/30/16
to GWT Users
Ideally your build tool should put your environment into prod or dev mode and the GWT app shouldn't know/care about it. With Maven / Gradle / Ant you can use "resource filtering" to rewrite resources like property files while being copied to the output folder. 

In that case you would have

Config.properties:
key = ${key}

dev.properties:
key = devValue

prod.properties:
key = prodValue

and the build tool will replace ${key} with the value of one of the other two files before GWT will pick up config.properties.


Otherwise you might try (no idea if it works):

interface Config extends Constants {...}
interface DevConfig extends Config {...} -> create a file DevConfig.properties
interface ProdConfig extends Config {...} -> create a file ProdConfig.properties

and in Java code

Config config;
if ("dev".equals(System.getProperty("env")) {
  config = GWT.create(DevConfig.class);
} else if ("prod".equals(......)) {
  .....
}

Can't remember, but System.getProperty() might require GWT 2.8.


-- J.

harshyadav

unread,
Oct 1, 2016, 3:51:26 PM10/1/16
to GWT Users
Thanks for your response Jens.

For the maven configuration, I have a *.Dev.gwt.xml and a *.Prod.gwt.xml being referenced from the maven profiles. I'll see if I can directly filter resources there.


Since, I am using GWT 2.8, the second option you mentioned works great with my current setup.

As a follow up, is it possible to have the appropriate interface resource being picked from the *.gwt.xml instead.

For e.g., something like this wont work since here I only have interfaces as opposed to implementations below:

<replace-with
 
class="com.test.client.ClientFactoryImpl">
 
<when-type-is class="com.test.client.ClientFactory" />
</replace-with>


--Harsh
Reply all
Reply to author
Forward
0 new messages