17

I'd like to use properties-maven-plugin. I read the usage http://mojohaus.org/properties-maven-plugin/usage.html , but it's not working for me.

I created a very simple project to test it. Here is my 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>test</groupId>
  <artifactId>MavenTest</artifactId>
  <version>1.0.0</version>
  <name>MavenTest</name>

  <properties>
    <prop1>2.2</prop1>
  </properties>

  <dependencies>
    <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>
  </dependencies>

  <build>
    <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0-alpha-2</version>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>${basedir}/my.properties</file>
                            </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    <pluginManagement>
        <plugins>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>
                                        org.codehaus.mojo
                                    </groupId>
                                    <artifactId>
                                        properties-maven-plugin
                                    </artifactId>
                                    <versionRange>
                                        [1.0-alpha-2,)
                                    </versionRange>
                                    <goals>
                                        <goal>
                                            read-project-properties
                                        </goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
  </build>
</project>

If I run mvn compile, or mvn install the result is:

[ERROR] The build could not read 1 project -> [Help 1]
org.apache.maven.project.ProjectBuildingException: Some problems were encountered while processing the POMs:
[ERROR] 'dependencies.dependency.version' for org.apache.logging.log4j:log4j-api:jar must be a valid version but is   '${log4j.version}'. @ line 16, column 13

The "my.properties" file contains this:

 log4j.version=2.2    

As it described here: mojohaus

If I use the prop1 property, which is defined in the pom.xml, everything is working.

So what I'm doing wrong?

Lukas Eichler
  • 5,689
  • 1
  • 24
  • 43
zsom
  • 479
  • 1
  • 5
  • 19
  • You put the file in the correct directory? Means you do not get any errors or warnings when starting maven build? – Uwe Allner May 11 '15 at 10:37
  • Yes I did. Eclipse's effective pom view shows the correct (absolute) file path.And I only get that error, which is shown in the question – zsom May 11 '15 at 10:52
  • no resolution for this question? I am having the same problem.. – AlexGreg Mar 04 '16 at 09:17
  • 3
    I opened an issue on the Github project [here](https://github.com/mojohaus/properties-maven-plugin/issues/26) – AlexGreg Mar 04 '16 at 23:12
  • I have the same issue – Alessandro C Dec 19 '16 at 10:36
  • I had a similar issue, because i was trying to use the properties-plugin together with the wildfly-maven-plugin. I found this issue here https://issues.jboss.org/browse/WFMP-70 The creator has an other problem, but he says to call the initialize phase explicitly. This worked for me. Maybe you could try this, too. – SukiSoft Apr 05 '17 at 09:04

4 Answers4

7

Official answer from them is that using read-project-properties for setting version of dependencies is not supported: https://github.com/mojohaus/properties-maven-plugin/issues/26

This does not work nor is it the intention of properties maven plugin and furthermore what would be the advantage to read properties from a file to define versions of dependencies?

There is a very through explanation about this issue on this answer: https://stackoverflow.com/a/14727072/1707491

Quaestor Lucem
  • 607
  • 9
  • 21
1

Make sure that you have ${basedir}/my.properties file present with that specific property value pair: my.properties contents should be: prop1=2.3

Rajesh Goel
  • 3,277
  • 1
  • 17
  • 13
0

You have to define the version properties when you want to use them. The plugin just allows you to do so; but you will have to set the values for yourself. Like

<properties>
    <log4j.version>1.2.17</log4j.version>
</properties>

EDIT:

As you showed, you have done that. Now it seems, that as you defined the goal within an execution, it is only valid within that execution. Try to declare it as global:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <configuration>
        <files>
            <file>${basedir}/my.properties</file>
        </files>
    </configuration>
...

Uwe Allner
  • 3,399
  • 9
  • 35
  • 49
  • I forgot to show my property file, now the question has been edited. For your answer: if i define the properties in the pom.xml, everything is ok. But I want to store the properties in an external file. – zsom May 11 '15 at 09:45
  • I declared it as global (as in your answer), but I get the same error ;( – zsom May 11 '15 at 11:43
0

The maven way is to use the element in a top level POM that includes the projects with common dependencies. That is the simplest approach.

You can also import a POM used expressly for dependency managment. The import approach does not require the parent/child project relationships.

See the Maven documentation:

  1. Dependency Management
  2. Importing Dependencies
Charlie Reitzel
  • 809
  • 8
  • 13