138

Does anyone know how to read a x.properties file in Maven. I know there are ways to use resource filtering to read a properties file and set values from that, but I want a way in my pom.xml like:

<properties file="x.properties"> 

</properties>

There was some discussion about this: Maven External Properties

Hamedz
  • 726
  • 15
  • 27
Dougnukem
  • 14,709
  • 24
  • 89
  • 130
  • If you have only a few values, and different users will need different values, consider instead [putting the value in your `settings.xml`](https://maven.apache.org/examples/injecting-properties-via-settings.html). – Raedwald Mar 27 '19 at 16:33

3 Answers3

101

Try the Properties Maven Plugin

kapex
  • 28,903
  • 6
  • 107
  • 121
Mike Pone
  • 18,705
  • 13
  • 53
  • 68
  • 1
    I think that's what I'm looking for I couldn't find the 1.0-SNAPSHOT in the maven repositories but there is a release: http://mvnrepository.com/artifact/org.codehaus.mojo/properties-maven-plugin org.codehaus.mojo properties-maven-plugin 1.0-alpha-1 – Dougnukem May 11 '09 at 19:32
  • 3
    Current link: http://mojo.codehaus.org/properties-maven-plugin/read-project-properties-mojo.html – Jesse Glick Jul 07 '11 at 16:01
  • Current version: org.codehaus.mojo properties-maven-plugin 1.0-alpha-2-SNAPSHOT from http://snapshots.repository.codehaus.org/ – Huluvu424242 Jul 28 '11 at 20:19
  • 2
    The link in the Answer has been updated to the new link from @JesseGlick – Jon Adams Mar 14 '12 at 20:57
  • 1
    I had problems with this plugin on Windows. If someone has problems too, try out [kuali](http://site.kuali.org/maven/plugins/properties-maven-plugin/1.1.9/index.html) instead. – fnst May 10 '13 at 11:16
  • The [Kuali](http://site.kuali.org/maven/plugins/properties-maven-plugin/1.1.9/index.html) plugin has the added benefit of being able to read a property file from a jar (classpath) which was necessary in my case. – Morten Haraldsen Sep 22 '14 at 13:19
  • The link is now dead. – Olivier Grégoire Jul 28 '15 at 11:34
64

Using the suggested Maven properties plugin I was able to read in a buildNumber.properties file that I use to version my builds.

  <build>    
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-1</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>${basedir}/../project-parent/buildNumber.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
   </plugins>
The Alchemist
  • 3,397
  • 21
  • 22
Dougnukem
  • 14,709
  • 24
  • 89
  • 130
  • 9
    could you show the inside of the buildNumber.properties file? thank you! – victorio Nov 12 '13 at 09:10
  • Thanks for a working example. However, why I got an error of `Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:properties-maven-plugin:1.0-alpha-1:read-project-properties (execution: default, phase: initialize)` – WesternGun Feb 05 '18 at 14:44
  • When I enter this section under after and before the regular maven plugins, I am getting this error: `Plugin 'execution' not covered by lifecycle configuration: org.codehaus.mojo:properties-maven-plugin:1.0-alpha-1:read-project-properties (execution: default, phase: initialize)` – PraNuta Feb 28 '19 at 15:42
  • @BorisBrodski could you show the inside of the buildNumber.properties file? It seems you deleted some details. – Moustafa Mahmoud Sep 04 '19 at 16:05
  • @MoustafaMahmoud why me? That's not my answer :) But I may speculate, that it could look like 'my.great.product.version=1.0.0'. – Boris Brodski Sep 04 '19 at 16:21
  • As I saw you was discussing something inthe comments. I thought you added some more information. My problem seems when I tried to read the value from the properties file and run the install I didn't get the properties value. So, I wounder if anyone can help to add full example for @Dougnukem's answer – Moustafa Mahmoud Sep 04 '19 at 16:26
  • As I recall the lifecycle configuration issue is a problem specific to Eclipse that requires a mapping in m2e. – maw Jan 23 '20 at 22:22
  • Depending on the used packaging you might need to use the validate phase instead of initialize phase. – anre Apr 16 '21 at 14:58
  • Or even better use ${maven.multiModuleProjectDirectory}/buildNumber.properties with current Maven version (but in that case the parent module should have ".mvn" directory present). – anre Apr 19 '21 at 08:19
  • What about to retrieve the file data inside a Java Class??? – testing_22 May 23 '22 at 15:52
6

This answer to a similar question describes how to extend the properties plugin so it can use a remote descriptor for the properties file. The descriptor is basically a jar artifact containing a properties file (the properties file is included under src/main/resources).

The descriptor is added as a dependency to the extended properties plugin so it is on the plugin's classpath. The plugin will search the classpath for the properties file, read the file''s contents into a Properties instance, and apply those properties to the project's configuration so they can be used elsewhere.

Community
  • 1
  • 1
Rich Seller
  • 83,208
  • 23
  • 172
  • 177