Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share common properties among several maven projects?

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.

like image 232
David Rabinowitz Avatar asked Aug 05 '09 06:08

David Rabinowitz


People also ask

How do I share resources between projects in Maven?

There are a few ways to share resources across multiple projects or modules: Cut and paste them. Use Assembly and Dependency plugins. Use the maven-remote-resources-plugin.

Can two Maven modules depend on each other?

Because modules within a multi-module build can depend on each other, it is important that the reactor sorts all the projects in a way that guarantees any project is built before it is required. The following relationships are honoured when sorting projects: a project dependency on another module in the build.

Does Maven support multi project build?

Maven's Multi-Module ProjectThe submodules are regular Maven projects, and they can be built separately or through the aggregator POM. By building the project through the aggregator POM, each project that has a packaging type different from pom will result in a built archive file.

How can I add an existing Maven project to another Maven project as a module?

Just do a regular "Import existing maven project into workspace" to get this done. Show activity on this post. If you use M2e with Eclipse you do not need to do that, because Eclipse resolves dependencies across the workspace. You just need to have the two projects open and your dependencies declared correctly.


2 Answers

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.
like image 83
Romain Linsolas Avatar answered Sep 25 '22 06:09

Romain Linsolas


Note that the original idea I have here is something that I am doing, but that I may have found a much better idea which I've also listed out below. I wanted to keep both ideas here for completeness in case the newer idea does not work.


I think you can solve this problem using the parent pom, but you need to have a maven repository and a CI build tool.

I've got several projects which all inherit base properties from a parent POM. We use Java 1.5, so that build property is setup there. Everything is UTF-8. All of the reports that I wish to run, Sonar setup, etc, is inside the parent POM.

Assuming your project is in version control and you've got a CI tool, when you check in, your CI tool can build to POM project and deploy the SNAPSHOT to the maven repos. If your projects point to the SNAPSHOT version of the parent POM, they'll check the repository to validate that they have the latest version...if not they download the latest version. So if you update the parent, all of the other projects will update.

The trick, I suppose is releasing with a SNAPSHOT. I'd say your releases are going to come much less frequently than your changes. So you perform a release of your POM, then update your POMs that inherit from them and check them into version control. Let the devs know that they need to do an update and go from there.

You could just trigger builds there forcing the new POMs into the repository and then have all of the devs pick up the changes automagically upon build.


I've removed the LATEST/RELEASE keywords idea because they do not work for parent POMs. They only work for dependencies or plugins. The problem area is in DefaultMavenProjectBuilder. Essentially it has trouble determining which repository to look for the parent to determine what the latest or release version is. Not sure why this is different for dependencies or plugins though.


It sounds like these would be less painful than having to update the POMs on every change to the parent POM.

like image 27
Mike Cornell Avatar answered Sep 22 '22 06:09

Mike Cornell