Properties file: use the key as a variable

I want to use the key defined in the properties file as a variable like this:

key1= value1
key2= value2
key3= key1

I'm trying to:

key3= {key1}

or

key3= ${key1}

But it does not work!

Any idea please?

+7
source share
4 answers

The built-in Properties Java class does not do what you are looking for.

But there are third-party libraries that do. Commons configuration is what I used with some success. The class does exactly what you are looking for. PropertiesConfiguration

Thus, you may have a file with a name my.propertiesthat looks like this:

key1=value1
key2=Something and ${key1}

The code that uses this file might look like this:

CompositeConfiguration config = new CompositeConfiguration();
config.addConfiguration(new SystemConfiguration());
config.addConfiguration(new PropertiesConfiguration("my.properties"));

String myValue = config.getString("key2");

myValuewill be "Something and value1".

+16
source

, . , key3= ${key1}, key3 "$ {key1}".

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Properties.html#load(java.io.InputStream)

csd, . Apache Ant (http://ant.apache.org/), - :

<property name="src.dir" value="src"/>
<property name="conf.dir" value="conf" />

, 'src.dir', :

<dirset dir="${src.dir}"/>

Apache Ant - .properties Ant. :

<loadproperties srcFile="file.properties"/>
0

.xml : Maven. maven. .properties :

key1 = ${src1.dir}
key2 = ${src1.dir}/${filename}
key3 = ${project.basedir}

maven pom.xml( ) - :

<resources>
    <resource>
        <filtering>true</filtering>
        <directory>just-path-to-the-properties-file-without-it</directory>
        <includes>
            <include>file-name-to-be-filtered</include>
        </includes>
    </resource>
</resources>

...

<properties>
    <src1.dir>/home/root</src1.dir>
    <filename>some-file-name</filename>
</properties>

, , , :

key1 = /home/root
key2 = /home/root/some-file-name
key3 = the-path-to-your-project

, , pom.xml: mvn clean install -DskipTests

0

csd ( Java). Commons Configuration, , csd.

, . PropertiesConfiguration Apache Commons ( 04/18)./2019).

( ) AbstractConfiguration, , , .

, , ( csd , , , csd.

0

All Articles