Search code examples
javamavenmaven-plugin

How to read depedency version from properties file


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.


Solution

  • 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