2

I have one maven project. I want to externalise maven dependancy version to external property file. I tried with property file plugin I is not reading property file.

config.properties

springframework.version=4.2.5.RELEASE

POm.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.estuate.test</groupId>
<artifactId>testPom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0-alpha-1</version>
            <executions>
                <execution>
                    <phase>pre-clean</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>config.properties</file>
                        </files>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${springframework.version}</version>
    </dependency>
</dependencies>

still I am gettting error message as

build.plugins.plugin[org.codehaus.mojo:properties-maven-plugin].dependencies.dependency.version' for org.springframework:spring-core:jar must be a valid version but is '${springframework.version}'

Please help me out.

Santoshkumar Kalasa
  • 477
  • 1
  • 5
  • 15
  • Why not using the most recent version of the [properties-maven-plugin](http://www.mojohaus.org/properties-maven-plugin/) which 1.0.0 ? – khmarbaise Jan 10 '17 at 08:19
  • I didn't understand the solution. I have only one pom.xml file and I don't have any parent maven project or module module – Santoshkumar Kalasa Jan 11 '17 at 05:01

3 Answers3

2

When you add a plugin with a custom execution, like you do, then you must realise that it only executes in the phases you specify. You specify pre-clean phase, which is part of the clean lifecycle, and not part of the build lifecycle.

Maven is composed of lifecycles, which execute up to a given phase. A command of mvn compile actually means run the build lifecycle all phases up to and including the compile phase.

Two of the standard lifecycles are (not complete list):

clean :: pre-clean -> clean -> post-clean

build :: validate -> compile -> test -> package -> verify -> install -> deploy

The dependencies specified are probably used by the standard plugins for the compile phase, so the properties needs to be available at that time.

When you state the pre-clean phase, the properties are available when you execute mvn clean.

For the properties to be available at compile phase, you should probably bind to the validate phase instead.

Although very verbose, there is actually quite a few hints running in debug mode with mvn -X, but it may be too much to comprehend at first.

Some more information about maven lifecycles here : https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44
0

you can use maven properties plugin which is one of the suggested way to read property files in maven projects. below is the plugin details

  <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>Versions.properties</file>
                  </files>
                </configuration>
              </execution>
            </executions>
          </plugin>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${springframework.version}</version>
    </dependency>
</dependencies>

Version.properties file looks like

springframework.version=4.2.5.RELEASE
dadoonet
  • 14,109
  • 3
  • 42
  • 49
R Rajesh
  • 41
  • 5
-1

I think . You should assign a Spring version to the springframework.version statement. For example

     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-core</artifactId>
       <version>4.3.1-REALESE</version>
     </dependency>

then the other dependencies

<dependency>
    <groupId>...framework...</groupId>
    <artifactId>spring-...</artifactId>
    <version>${springframework.version}</version>
</dependency>