How to build a Maven project with Eclipse with a pom.xml that imports an external properties file?

436 Views Asked by At

I have looked around from various questions on stackoverflow, but I have not found the answer that solve my purpose.

I want to import a properties file in a pom.xml; my purpose is to replace the <properties> section with the properties loaded from the external file. Each property refers to the version of a particular maven dependency.

I have tried the properties-maven-plugin, but the properties are not solved and the project is not built.

I'm looking for a way that preserve the standard build of Eclipse, and also the mvn install goal of Maven.

As an example, I want that this section:

<properties>
    <dependency1.version>1.0.0</dependency1.version>
</properties>

will be replaced with the property declared in a dependency.properties file, like this one:

dependency1.version=1.0.0

How can I implement my pom.xml in order to obtain this behaviour?

Thanks in advance.

EDIT

This is what I have tried:

<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>
                        <files>
                            <file>${basedir}/dependencies.properties</file>
                        </files>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>my version: ${dependency1.version}</echo>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Well, if I do a simple mvn install, in maven console it prints:

[INFO] Executing tasks
     [echo] my version: 1.0.0
[INFO] Executed tasks

So, the plugin solve the property, but when I try to import the dependency with this:

<dependencies>
    <dependency>
        <groupId>dependency1</groupId>
        <artifactId>dependency1</artifactId>
        <version>${dependency1.version}</version>
    </dependency>
</dependencies> 

I obtain this error:

[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[ERROR] 'dependencies.dependency.version' for dependency1:dependency1:jar must be a valid version but is '${dependency1.version}'. @ line 21, column 13
 @  

The property is solved in the maven-antrun-plugin, but if it is used in the dependencies section, it doesn't work.

0

There are 0 best solutions below