简体   繁体   English

如何在POM中获取maven版本号

[英]How to get maven version number inside POM

I need to get maven version number (eg 3.0.5, 3.1.0) from inside pom.xml file I need it to be able to add correct dependencies for: 我需要从pom.xml文件中获取maven版本号(例如3.0.5,3.1.0)我需要它才能为以下内容添加正确的依赖项:

<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. 版本3.1.0需要相同版本的库,3.0.5版本相同。 I would assume that there has to be something like ${maven.version} in poms but I couldn't find it. 我会假设在${maven.version}必须有${maven.version} ,但我找不到它。

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 编辑:我需要该项目在maven 3.0和3.1中工作所以我不能静态地做它必须获得当前正在运行的maven的版本

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. 并在您的pom文件中定义具有此属性的依赖项。

  <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: 或者对于plugin-api,如下所示:

  <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. 对于maven模型,如果您使用3.0.1或3.0.5或3.1并不重要,因为模型(pom.xml)尚未更改。 For the plugin-api you can use 3.0.5 and get it running in Maven 3.1 as well. 对于plugin-api,你可以使用3.0.5并让它在Maven 3.1中运行。 BTW: The above things are from a plugin which works under Maven 3.0 and 3.1. BTW:以上内容来自一个在Maven 3.0和3.1下运行的插件。

You can add it yourself via a settings.xml file. 您可以通过settings.xml文件自行添加它。

${settings.mvn.version} would refer to: ${settings.mvn.version}会引用:

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

Or add {$mvn.version} directly in the pom.xml , by using: 或者使用以下{$mvn.version}直接在pom.xml添加{$mvn.version}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对 TestNG version# 和 Selenium-JAVA version# 感到困惑 - 如何获取 Maven POM.xml 文件的版本号 - Confused about TestNG version# and Selenium-JAVA version# - How to get version number for Maven POM.xml file 如何替换 Maven 子模块 pom.xml 文件中的版本号? - How to replace version number in Maven submodule pom.xml files? 如何在Eclipse代码模板中获取Maven pom.xml版本? - How to get Maven pom.xml version in Eclipse code template? 如何像Pom一样从Java方法获取Maven项目版本 - How to Get Maven Project Version From Java Method as Like at Pom 如何在普通pom中保留版本号并在其他pom文件中使用(spring maven项目) - How to keep version number in common pom and use it in other pom files (spring maven project) 将Maven POM版本号加载到Java Project中 - Load Maven POM version number into Java Project 在 Maven 的父 POM 中从子级获取版本 - Get the version from child in the parent POM in Maven Maven:如何读取父 POM 版本 - Maven: How to read the Parent POM version 如何将语义版本控制应用于 java maven 项目以自动增加 pom.xml 中的版本 - how to apply semantic versioning to the java maven project to automatically increment the version inside pom.xml 如何在Maven pom.xml中指定Eclipse插件的版本? - How to specify version of an Eclipse plugin in Maven pom.xml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM