Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - Reading a property from an external properties file

I have a property file with the following

junit.version=3.8.1
dbcp.version=5.5.27
oracle.jdbc.version=10.2.0.2.0

I try to read those properties from my pom file as shown below

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>${junit.version}</version>
  <scope>test</scope>
</dependency>


<dependency>
    <groupId>dbcp</groupId>
    <artifactId>dbcp</artifactId>
    <version>${dbcp.version}</version>
    <scope>provided</scope>
</dependency>
<dependency>
  <groupId>com.oracle</groupId>
  <artifactId>ojdbc14</artifactId>
  <version>${oracle.jdbc.version}</version>
  <scope>provided</scope>
</dependency>

and the plugin configuration

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <executions>
           <!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. -->
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>../live.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>

I find that when i run mvn clean install it does not find the properties, instead it comes up with the following errors:

'dependencies.dependency.version' for junit:junit:jar must be a valid version but is '${junit.version}'. @ line 23, column 16
'dependencies.dependency.version' for dbcp:dbcp:jar must be a valid version but is '${dbcp.version}'. @ line 31, column 12
'dependencies.dependency.version' for com.oracle:ojdbc14:jar must be a valid version but is '${oracle.jdbc.version}'. @ line 37, column 13

The above failures appear to be in situations where i refer to the property when i declare the dependency. I found that in some other situations the property is read from the file. For example it works if i use a property on the project version tag (not dependency version)

It seems that the property is not read from the file if it is referred to from the dependency declaration but is read if referred to from anywhere else. Any ideas?

like image 889
ziggy Avatar asked Mar 28 '12 17:03

ziggy


People also ask

How do I pass system properties in maven?

To provide System Properties to the tests from command line, you just need to configure maven surefire plugin and use -D{systemproperty}={propertyvalue} parameter in commandline. Run Single Test with Maven : $ mvn test -Dtest=MessageUtilTest#msg_add_test -Dmy_message="Hello, Developer!"

How do I read a properties file URL?

Steps for reading a properties file in Java Create an instance of Properties class. Create a FileInputStream by opening a connection to the properties file. Read property list (key and element pairs) from the input stream using load() method of the Properties class.


1 Answers

The initialize phase is not part of the clean lifecycle. You need to also bind your properties plugin to pre-clean phase.

However, the dependency resolution runs before resolving and executing other plugins, so your approach won't work.

The proper way to deal with that would be to move dependency versions into a parent pom.xml and use the same parent pom in both of your projects.

like image 136
Eugene Kuleshov Avatar answered Sep 18 '22 14:09

Eugene Kuleshov