views:

6252

answers:

5

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

+11  A: 

Try the Properties Plugin

Mike Pone
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<dependency> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.0-alpha-1</version></dependency>
Dougnukem
+2  A: 

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

<dependency>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-1</version>
</dependency> 
...
  <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>
Dougnukem
+2  A: 

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.

Rich Seller
A: 

Hi,

Am able to read the version from the property file and the corresponding jar is generated but a facing issue - at install phase the version number given in build file is not refered i.e the constant is refered.

value in build.properties : myVerNo=3.1.7076

pom.xml:-

jar Test ${myVerNo}

mvnbuild:-

[INFO] No tests to run. [INFO] [jar:jar] [INFO] Building jar: D:\jibx\Response\target\sample-3.1.7076.jar [INFO] [install:install] [INFO] Installing D:\jibx\Response\target\sample-3.1.7076.jar to D:\patch3.1.5\Patch_REPO\sampleJibx\sample\${myVerNo}\sa mple-${myVerNo}.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL

chakku
A: 

Hi,

I am able to write the properties to the external file using properties-maven-plugin but not able to read the properties from the files.

My properties file(app.properties) looks like:

hibernate-version=3.2.1.ga

spring-version=2.5.6

The plugin entry in the pom.xml is as follows:

          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0-alpha-2</version>
            <executions>
              <execution>
                <phase>initialize</phase>
                <goals>
                  <goal>read-project-properties</goal>
                </goals>
                <configuration>
                  <files>
                    <file>${basedir}/app.properties</file>
                  </files>
                </configuration>
              </execution>
            </executions>
      </plugin>

The file is present in the same directory as the pom.xml. But when i try to do a "mvn install", it does not take the value of ${spring-version}.

Any help would be highly appreciated.

Anshul