19

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?

ziggy
  • 15,677
  • 67
  • 194
  • 287

2 Answers2

14

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.

Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
  • Which phase should i use to ensure that the properties are loaded regarded of the mvn command? – ziggy Mar 28 '12 at 18:35
  • I already answered that. The mvn clean command is using differed lifecycle that has completely different phases from the default lifecycle (e.g. mvn package or mvn install). Please follow the link to documentation from my answer. – Eugene Kuleshov Mar 28 '12 at 18:40
  • Do you mean something like this? pre-clean,initialize – ziggy Mar 28 '12 at 18:54
  • I tried to add both pre-clean and intialize but it didnt work. – ziggy Mar 28 '12 at 18:56
  • 3
    Sorry, I should have read your question more carefully... You can't specify multiple phases in element. But you can't use properties loaded by properties-maven-plugin to specify versions of dependencies. I updated my answer with proper solution. – Eugene Kuleshov Mar 28 '12 at 19:15
  • I am trying to find the maven document where it states "dependency resolution runs before executing other plugins" so that I can get a more indepth insight into it. Do you have the link to such a document? – JackDev Nov 27 '12 at 04:34
  • JackDev no idea if such document exists (you may want to post a separate question for that). However, I am certain that how it is implemented in Mavens from version 1 to 3.x... If you think about it, that's the only way to guarantee reproducible builds. – Eugene Kuleshov Nov 27 '12 at 15:58
  • so is there any other way we can achieve same results ? I have got many environments and each needs different version of a jar. – nish1013 Feb 21 '14 at 10:13
  • "If you think about it, that's the only way to guarantee reproducible builds." Yea reproducible with duplicate entries for library versions, which yields a very high probability of conflict when your application has 2 dosen components with intertwined dependency graphs, how f-ing brilliant, the depth of thought is staggering – niken Oct 04 '17 at 14:10
2

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>
  • I have exactly same pom and packaging type is POM. Inside pom, I have following dependency junit junit ${testing.version} test I am running project with command 'mvn clean package -DskipTests'. But still it is not resolving dependency. – Tejas Sep 03 '18 at 11:59