How to share common properties among several maven projects?

David Rabinowitz picture David Rabinowitz · Aug 5, 2009 · Viewed 57.7k times · Source

I have several projects built by maven, and I want to share some common properties among them - spring version, mysql driver version, svn base url, etc. - so I can update them once and it will be reflected on all projects.

I thought of having a single super pom with all the properties, but if I change one of the problem I need to either increment its version (and to update all the poms inherit from it) or to delete it from all the developers' machines which I don't want to do.

Can specify these parameters externally to the pom? I still want to have the external location definition in a parent pom.

Answer

Romain Linsolas picture Romain Linsolas · Aug 5, 2009

What you can do is to use the Properties Maven plugin. This will let you define your properties in an external file, and the plugin will read this file.

With this configuration :

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0-alpha-1</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>my-file.properties</file>
                        </files>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

and if you have, in your properties file the following lines:

spring-version=1.0
mysql-version=4.0.0

then it's the same thing as if you wrote, in your pom.xml, the following lines:

<properties>
    <spring-version>1.0</spring-version>
    <mysql-version>4.0.0</mysql-version>
</properties>

Using this plugin, you will have several benefits:

  • Set easily a long list of properties
  • Modify the values of these properties without modifying the parent pom.xml.