What is wrong with having a static method that returns the string from
a parallel properties file...

something like

public class MyFoo {
    private static String getProp(String name) {
        Properties props = new Properties();
        final String resourceName = MyFoo.class.getSimpleName() + ".properties";
        final InputStream is =
                MyFoo.class.getResourceAsStream(resourceName);
        try {
            try {
                props.load(is);
            } catch (IOException e) {
                throw new RuntimeException(
                        "Could not find associated properties file for "
                                + MyFoo.class.getName(), e);
            }
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                // ignore
            }
        }
        return props.getProperty(name);
    }

    public static final String MY_CONSTANT = getProp("myConstant");
}

This has the added benefit that all references to MY_CONSTANT will be
bound at class loading time and not at compile time...

N.B. The compile time binding of public static final constants can
bite you in the ass when you least expect it.

Especially if you change the class that has the constant and don't
recompile _every_ class that references it.

By passing the string through a function, you ensure that the constant
is bound when the class is loaded and not to when it is compiled.

-Stephen
On Thu, Jul 3, 2008 at 1:27 PM, Jean-Marie Pitre <[EMAIL PROTECTED]> wrote:
> Hi,
>
>
>
> I am facing a problem. I have a java class which contains final String.
> This string has to be final, so I can not set value by a file
> properties.
>
>
>
> This string is different according to each environment, so I would like
> to change it during the maven build by using maven profile.
>
> But I don't know how to change java source code with maven or maven
> plugin.
>
>
>
> Have you got any idea how to do this?
>
>
>
> Thanks,
>
> Regards.
>
>
>
>
> -------------------
> Email Disclaimer
> http://www.cofidis.be/emaildisclaimer.php
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to