4

我需要从 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>

版本 3.1.0 需要相同版本的库,与 3.0.5 相同。我认为必须有类似${maven.version}poms 的东西,但我找不到。

编辑:我需要项目在 maven 3.0 和 3.1 中工作,所以我不能静态地做它必须获得当前运行的 maven 的版本

4

2 回答 2

3

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.

于 2013-09-15T12:28:31.673 回答
0

您可以通过settings.xml文件自己添加。

${settings.mvn.version}将指:

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

或者{$mvn.version}直接在pom.xml, 中添加:

<project>
...
  <properties>
     <mvn.version>3.1.0</mvn.version>
  </properties>
...
</project>
于 2013-09-15T11:46:23.397 回答