Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Dependency Version As Property

Tags:

java

maven

I have a maven pom file that defines a dependency as such:

<dependencies>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.3.6</version>
    </dependency>
</dependencies>

It is often said that everything in the pom can be referenced as a Maven property:

https://bowerstudios.com/node/991

For example, you can read ${project.version}, ${project.build}, etc. Is there a way to read a dependency's version as a Maven property, ala ${project.dependencies.dependency.groupId=org.apache.httpcomponents&artifactId=httpclient.version} ?

like image 527
Ring Avatar asked May 21 '16 02:05

Ring


People also ask

What happens if we don't specify version in POM?

That being said, if the version is not given, then the version provided in the dependencyManagement or the parent pom will be used. For example: in the pom (only important sections are mentioned) given below, no version is provided for the artifact jstl.

What is type in Maven dependency?

There are two types of dependencies in Maven: direct and transitive. Direct dependencies are the ones that we explicitly include in the project.

What is dependencyManagement in Maven?

What Is Maven Dependency Management? 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.

What are Maven properties?

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. Now this property( java.


1 Answers

You could define a custom property under <properties> and refer to it from your dependency. Preferred way is to place the property in parent pom (if exist and is a multi module project). Alternately, you can skip the <version> altogether if you had defined the <dependency> in <dependency-management> section

<properties>
<http.client.version>4.3.6</http.client.version>
</properties>
...
<dependencies>
   <dependency>
     <groupId>org.apache.httpcomponents</groupId>
     <artifactId>httpclient</artifactId>
     <version>${http.client.version}</version>
   </dependency>
</dependencies>
like image 133
Balaji Katika Avatar answered Oct 17 '22 09:10

Balaji Katika