Krzysztof Krasoń
Krzysztof Krasoń

Reputation: 27516

How to get maven version number inside POM

I need to get maven version number (e.g. 3.0.5, 3.1.0) from inside pom.xml file I need it to be able to add correct dependencies for:

<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-model</artifactId>
    <version>3.0.5</version>
</dependency>
<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-plugin-api</artifactId>
    <version>3.0.5</version>
</dependency>

Version 3.1.0 requires the libraries at the same version, same for 3.0.5. I would assume that there has to be something like ${maven.version} in poms but I couldn't find it.

EDIT: I need the project to work in both maven 3.0 and 3.1 so I can't do it statically it has to get the version of currently running maven

Upvotes: 4

Views: 32817

Answers (2)

khmarbaise
khmarbaise

Reputation: 97547

The best solution is to define a property like this:

<properties>
  <maven.version>3.0.3</maven.version>
</properties>

and define the dependencies with such property within your pom file.

  <dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-core</artifactId>
    <version>${maven.version}</version>
  </dependency>

or

  <dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-model</artifactId>
    <version>${maven.version}</version>
  </dependency>

or for the plugin-api like the following:

  <dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-plugin-api</artifactId>
    <version>${maven.version}</version>
  </dependency>

For the maven-model it does not really matter if you are using 3.0.1 or 3.0.5 or 3.1 cause the model (pom.xml) hasn't been changed. For the plugin-api you can use 3.0.5 and get it running in Maven 3.1 as well. BTW: The above things are from a plugin which works under Maven 3.0 and 3.1.

Upvotes: 4

flavian
flavian

Reputation: 28511

You can add it yourself via a settings.xml file.

${settings.mvn.version} would refer to:

<settings>
  <mvn>
    <version>3.1.0</version>
  <mvn>
</settings>

Or add {$mvn.version} directly in the pom.xml, by using:

<project>
...
  <properties>
     <mvn.version>3.1.0</mvn.version>
  </properties>
...
</project>

Upvotes: 0

Related Questions