Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access maven property using dependency management

I am using maven's dependency-management to import the POM into my project Y as below:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.abc</groupId>
            <artifactId>X</artifactId>
            <version>1.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

And my artifact X has following properties which I want to access in POM of project Y:

<properties>
    <property1>value1</property1>
    <property2>value2</property2>
</properties>

I am not able to access properties defined in X into Project Y. I understand that using above approach I can't make use of plugin-management but I was unable to find anything related to properties on web.

Also please note I can't use the artifact X as parent as we have project level parent already defined.

Could you please guide on the same.

like image 246
jack Avatar asked Jun 05 '18 11:06

jack


People also ask

How do I access property in POM xml?

In maven pom. xml , a property is accessed by using ${property_name} . You can define your custom properties in Maven.

What is the use of dependency management in Maven?

Dependency management in Maven allows teams to manage dependencies for multi-module projects and applications. These can consist of hundreds or even thousands of modules. Using Maven can help teams define, create, and maintain reproducible builds.

How do you use properties in POM?

Properties are the last required piece to understand POM basics. 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.


Video Answer


1 Answers

You can only inherit properties from another pom, if you declare that as a parent. Importing a pom with type import only imports its dependencies, as described in the documentation. Since using the other pom as a parent is not possible in your case, let me suggest an alternative:

The codehaus Properties Maven Plugin 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).

like image 191
David Avatar answered Oct 16 '22 16:10

David