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?

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.


u can define like this:

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0-alpha-2</version>
            <executions>
                <execution>
                <id>pre-clean-config</id>
                    <phase>pre-clean</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>config.properties</file>
                        </files>
                    </configuration>
                </execution>
                <execution>
                <id>initialize-config</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>config.properties</file>
                        </files>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Need Your Help

bento start command fails with NoClassDefFoundError DNSCheckingTool

cassandra startup bento

I was trying out the tutorial in https://github.com/kijiproject/kiji-schema/blob/master/kiji-schema-cassandra/cassandra_tutorial.md

AdMob interstitial Ad is shown but can't click on it

android admob

I've got an app that displays an Interstitial Ad when certain activity is closed. I use a different activity to show the Ad. So far it shows the Ad correctly but nothing happens when I click on the...