Best way to change constants in Java build process

I have inherited a Java application (servlets) that runs under Tomcat. For historical reasons, the code has different "appearance" options, based on where the application will be deployed (mainly a branding issue).

There are several constants that control this branding process, which has different functions and should not be compacted into one constant (for example, BRAND, MULTI-LANGUAGE, as well as the location of CSS icons and stylesheets, etc.).

Currently, the development team must manually change the constants (they are at least localized in the same data class and well-documented), and then recompile the application using ANT.

What is the best way to automate this process, assuming at least Ant 1.8 and Java 6.x?

I know that there were no good solutions using compiler arguments (how can this be done in C or C ++), and I tend to the β€œbest way” to edit the source file containing the constants, or to put them in another file and their replacement using the Ant build process. I would like to get a result that will work using something like "ant build brand-x", where changing the brand will change the final assembly.

Thanks,

-Richard

+5
source share
6 answers

Use the replace task in Ant to change the values.

+2

, , "myapp.properties", path (. , ):

public class Constants
{
    private static final Properties props = new Properties();
    public static final String MY_CONSTANT;

    static
    {
        InputStream input = Constants.class.getResourceAsStream("/myapp.properties");
        if(input != null)
        {
           try
           {
              properties.load(input);
           }
           catch(IOException e)
           {
              // TODO log error
           }
        }

        // Initialize constants (dont' forget defaults)
        MY_CONSTANT = properties.getProperty("constant", "default");
        // .. other constants ...
    }
}

. ANT -D build.properties , ( ).

, , .

+9

"spring", , bean, , :

<bean id="propertyPlaceholder"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:configuration.properties" />
</bean>

ant -like ":

<bean id="connectionPool"  class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
    <property name="databaseName" value="mydb" />
    <property name="url" value="${db.url}" />
    ...

, , , . , ( , ).

public class Foo {
 public static final int SOME_CONSTANT=1;
..
}

public class Bar {
  ...
   int x=5+Foo.SOME_CONSTANT;
  ...
}

SOME_CONSTANT Foo 2, Bar, Bar 1 SOME_CONSTANT, ( , - ).

+1

ant expandproperties . . expandproperties ant .

<copy file="from" tofile="to">
  <filterchain>
    <expandproperties />
  </filterchain>
</copy>
+1

, . Ant "" :

<target name="one" description="constant substitution #1">
  <delete file="./tme3/MyConst.java" />
  <copy file="./save/MyConst.java" tofile="./tme3/MyConst.java" />
  <replace file="./tme3/MyConst.java" token="@BRANDING@" value="ONE_BRAND"/>
  <replace file="./tme3/MyConst.java" token="@STYLESHEET@"
           value="../stylesheet/onebrand.css"/>
  <replace file="./tme3/MyConst.java" token="@FAVICON@" value="../images/onebrand.ico"/>
  <replace file="./tme3/MyConst.java" token="@SHOW_LANGUAGES@" value="false"/>
</target>

, , - 3 , .

.

+1

Ant "-Dbrand = X".

0

All Articles