Willis V
Willis V

Reputation: 153

Extend properties file by including another properties file. Maven

Can I use 'include' option (or something like this) to include any properties file to another properties file?

So,I have two properties files: 1. "firstPropertiesFile" which contains the next string:

include = secondPropertiesFile #it's path to second property file

and

"secondPropertiesFile" which contains the next string:

key = value

Also I have resource file (file which will be filtered by resources:resources goal) contains:

${key}

When I invoke resources:resources goal, I expect the next steps:

  1. Resources plugin look into firstPropertiesFile file and see that it contains refer to another properties file.

  2. The plugin go to reference (path to second property file) and see necessary key and gets value (in our case- value).

But this way doesn't work in maven. Could you hint me how to realize this?

P.S. This option is supported in Apache Commons Configuration: http://commons.apache.org/proper/commons-configuration/userguide/howto_properties.html ("Includes" chapter).

Upvotes: 5

Views: 7218

Answers (2)

Boj
Boj

Reputation: 4053

In Maven, you can achieve something very similar Properties Maven Plugin.

I will not support the include semantics you are expecting, but it can compose a properties file from multiple sources. The example bellow will combine two properties files, which you can access from allprops.properties. This is especially useful when different sets of properties files must be used in different systems or environments.

<plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>properties-maven-plugin</artifactId>
      <version>${properties-maven-plugin.version}</version>
      <executions>
        <execution>
          <id>read-properties</id>
          <phase>generate-resources</phase>
          <goals>
            <goal>read-project-properties</goal>
          </goals>
          <configuration>
            <files>
              <file>firstPropertiesFile.properties</file>
              <file>secondPropertiesFile.properties</file>
            </files>
          </configuration>
        </execution>
        <execution>
          <id>write-all-properties</id>
          <phase>generate-resources</phase>
          <goals>
            <goal>write-project-properties</goal>
          </goals>
          <configuration>
            <outputFile>${project.build.directory}/allprops.properties</outputFile>
          </configuration>
        </execution>            
      </executions>
    </plugin>
  </plugins>

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533442

There is nothing built in to standard Java Properties to do this, you need to code it if you want this or use a library which does this already.

Upvotes: 1

Related Questions