0

How to read the properties from .properties file by using maven provided plugin, for one of my current project, I always used to set the properties in the <properties> tag of pom.xml, But here my requirement is, I will set the all properties in some .properties file for ex: dev.properties. It is having the following content inside it.

spring.package=org.springframework
spring.artifact=spring-core
spring.version=3.0.5.RELEASE

So, now I want to set the above properties into the pom.xml like the following:

<dependencies>
        <dependency>
            <groupId>spring.package</groupId>
            <artifactId>spring.artifact</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

I gone through this link : How to read an external properties file in Maven

But it is giving the following error : Missing artifact org.springframework:spring-core:jar:spring.version

Here is the pom.xml

<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.ram</groupId>
    <artifactId>DynamicProperties</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <spring.version>spring.version</spring.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0.0</version>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <urls>
                                <url>file:///D:/Hadoop_Apps/DynamicProperties/src/main/resources/dev.properties</url>
                            </urls>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
</project>

But spring-core-3.0.5.RELEASE jar is avail in maven repository. Is there any other plugin in maven to do the same, or did I missed any additional config here.

Please correct if I am wrong.

Thanks.

Community
  • 1
  • 1
Ram Kowsu
  • 711
  • 2
  • 10
  • 30

2 Answers2

2

First of all, when Maven reads your pom.xml, it'll immediately replace placeholders such as ${spring.version} with the actual value you specify as

<properties>
  <spring.version>spring.version</spring.version>
</properties>

So changing the values later via a plugin is too late! Maven will not do this, however, if there's no value available at this time. Therefore you can remove these properties from pom.xml and let Maven plugin define them later in build lifecycle. This usually solves uses of properties overridden by plugins, but...

Second, there might be still another problem: Maven will likely resolve dependencies (including their versions) before even executing any plugin, which will prevent you from doing this whole thing. If that's the case, you can move your properties to a profile and activate a certain profile instead.

<properties>
  <!-- default goes here, when no profile is used -->
  <spring.version>3.0.4-RELEASE</spring.version>
</properties>

<profiles>
  <profile>
    <id>dev</id>
    <properties>
      <spring.version>3.0.5-RELEASE</spring.version>
    </properties>
  </profile>
  <profile>
    <id>prod</id>
    <properties>
      <spring.version>3.0.1-RELEASE</spring.version>
    </properties>
  </profile>
</profiles>
Anton Koscejev
  • 4,603
  • 1
  • 21
  • 26
  • You should never defined versions of dependencies via properties which will be changed by a profile. If you have different environments than you should use in every environment the same dependencies. – khmarbaise May 29 '16 at 11:08
  • Yes, although I don't know what the use case is exactly, it does sound dangerous to use different versions depending on environment. – Anton Koscejev May 29 '16 at 14:18
0

Please find below the corrected pom.

<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.ram</groupId>
<artifactId>DynamicProperties</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
    <spring-version>spring.version</spring-version>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <urls>
                            <url>file:///${basedir}\src\main\resources\config.properties</url>
                        </urls>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>

The problem was in -

<spring.version>spring.version</spring.version>

tag. It should have been

<spring-version>spring.version</spring-version>

inside properties tag.

BR

Jeet
  • 5,569
  • 8
  • 43
  • 75
  • Hi Jeetendra, It's a typo error, I did the same by that only It's giving the error – Ram Kowsu May 29 '16 at 05:23
  • Can you please post your pom file. So that we can analyse it better. – Jeet May 29 '16 at 05:25
  • I followed the same, got the following error :'dependencies.dependency.version' for org.springframework:spring-core:jar must be a valid version but is '${spring.version}'. @ line 44, column 13 – Ram Kowsu May 29 '16 at 05:59
  • I think it's not an issue too. we can use spring.version there, is nothing wrong in it – Ram Kowsu May 29 '16 at 06:17
  • Yes I tried to replicate the issue at my end and it got messed up a bit. Still trouble shooting it. – Jeet May 29 '16 at 06:22
  • check this - src/main/resources/config.properties – Jeet May 29 '16 at 06:31