20

I would like to import properties from a project X pom file into my project Y pom such as library versions. I do not want to make the project X my project's parent.

I have tried to include project Xs pom in the dependency management section as an import.

<dependency>
                <groupId>abc</groupId>
                <artifactId>def</artifactId>
                <version>1.1</version>
                <type>pom</type>
                <scope>import</scope>
</dependency>

Please advise. Thanks.

user518066
  • 1,277
  • 3
  • 23
  • 35

3 Answers3

3

I think, today, the answer is NO. You can't read properties from external dependency without inheritance.

However a hack can be done with the codehaus Properties Maven Plugin. In fact, it can load maven properties from an external file. It can even use classpath: URLs to load files from. So you might try to load those from another dependency (which should have an appropriate scope since you probably do not want that dependency's JAR to hang around at runtime).

Katy
  • 1,023
  • 7
  • 19
1

The usual approach to share dependency versions without using parent POMs are BOMs.

These are separate projects that only contain a pom.xml which consists of <dependencyManagement>. This can then be imported in several other projects with <scope>import</scope>.

These other projects then import the dependencyManagement inside the BOM.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • 3
    true for dependencies, but we have to keep in mind that only dependencyManagement is imported from a BOM. It won't work for properties, pluginManagement, distributionManagement etc.. so it might not answer the question which is about properties. – Donatello Nov 16 '20 at 12:04
  • I downvoted this answer because, as Donatello noted, importing BOMs only brings in dependency management information, not properties, so in relation to the specific question this answer is wrong. – Garret Wilson Sep 06 '22 at 15:18
  • @GarretWilson That is true, but it is as near as we can get to what the OP wants ("such as library versions"). A plain "No, cannot be done" would be correct, but probably less helpful. – J Fabian Meier Sep 07 '22 at 09:53
  • "A plain 'No, cannot be done' would be correct …" Yes, that should be the correct answer. That is not your answer. Your answer is incorrect as to the question. If you want to say, "No, that cannot be done, but here is something related that might be helpful", that would still be correct. You are presenting the "something related that might be helpful" as the correct answer, which is incorrect. But I would say that even your alternative isn't even helpful in regard to the question. You don't say anything in regard to properties. You explain something else altogether. So no, the answer is wrong. – Garret Wilson Sep 07 '22 at 13:02
  • @GarretWilson You are right that I should have explained this more, and why probably answers the OPs underlying question. – J Fabian Meier Sep 07 '22 at 13:41
-3

Basically, you need to create a parent pom which is imported by both projects.

  1. The parent has a <dependencyManagement> section which lists groupId, artifactId and version
  2. The child pom's only need to list groupId and artifactId since the version is inherited from the parent's <dependencyManagement> section

eg:

root/pom.xml - Builds all modules, a simple pom with a `<modules>` section which includes parent, project1 and project2
root/parent/pom.xml - This has a `<dependencyManagement>` section
root/project1/pom.xml - parent=../parent/pom.xml
root/project2/pom.xml - parent=../parent/pom.xml

More info here

lance-java
  • 25,497
  • 4
  • 59
  • 101