Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use properties-maven-plugin?

Tags:

java

maven

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?

like image 678
zsom Avatar asked May 11 '15 09:05

zsom


People also ask

What is properties maven plugin?

The Properties Maven Plugin is here to make life a little easier when dealing with properties. It provides goals to read properties from files and URLs and write properties to files, and also to set system properties. It's main use-case is loading properties from files or URLs instead of declaring them in pom.

What is the use of properties in maven?

Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example: In your case you have defined properties as version of java.

How do I read properties file in Pom?

maven properties plugin is used to read the properties from an external file. In pom. xml, create a property under the properties tag for the property file location. On running the mvn validate command.

How do I use maven plugins?

In Maven, there are two kinds of plugins, build and reporting: Build plugins are executed during the build and configured in the <build/> element. Reporting plugins are executed during the site generation and configured in the <reporting/> element.


2 Answers

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

like image 197
Quaestor Lucem Avatar answered Sep 24 '22 22:09

Quaestor Lucem


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

like image 20
Rajesh Goel Avatar answered Sep 24 '22 22:09

Rajesh Goel