2

I'm referring to the maven binary version that is returned when you usually run

mvn --version

from the command line which returns an output like the below,

Apache Maven 3.0.4
Maven home: /usr/share/maven
Java version: 1.6.0_45, vendor: Sun Microsystems Inc.
Java home: /home/uvindra/Apps/java6/jdk1.6.0_45/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.11.0-15-generic", arch: "amd64", family: "unix"

Note the maven version 3.0.4. What's best way of accessing this from within a custom Maven Mojo in Java? Is there a property available for this?

I want to run a validation against current maven version that is executing my Mojo, Thanks

UDJ
  • 281
  • 2
  • 10
  • First why do you need to know that ? Is your MOJO only working with a particular Maven version? Using maven-enforcer-plugin? Did you set prerequisites in your pom (but be carefull has been deprecated for Maven 3.X). – khmarbaise Feb 11 '14 at 09:47
  • I am providing a facility for external users to execute other maven plugins via my mojo using the [mojo-executor](https://github.com/TimMoore/mojo-executor) plugin. There is a chance that a given plugin might only be compatible with certain maven versions. Administrators can specify what maven version a given plugin is compatible with, when they define the plugins that users will execute in a centralized system. Then when users execute these plugins on their local machines I can skip incompatible plugins by validating against their locally installed maven version. – UDJ Feb 11 '14 at 11:37
  • Why would you like to do that? Which advantages does your approach has in comparsion to the pom file ? You are trying to create a plugin which runs Maven? Why not using Maven itself. What is the problem you have faced with? Apart from that if a plugin is not compatible with the Maven version you have to use a different plugin version otherwise your build will not work. This has to be defined in the pom file which describes the build. For such purposes a pluginManagement section in a company pom is the right way and to force users to use such things is maven-enforcer-plugin. – khmarbaise Feb 11 '14 at 11:49
  • What does an Administrator has to do with a build system like Maven? – khmarbaise Feb 11 '14 at 11:49
  • Whole purpose is to use maven to enforce certain rules on a code base, not for actually doing a build. The restriction is that existing pom files of the code base cannot be changed.So including plugins in a company pom file is out.Developers will explicitly invoke the Mojo to check if their code breaks any rules. Administrators define rules that need to be validated in the form of maven plugins such as the 'maven-enforcer-plugin' in a central registry. When the developers invoke my Mojo it will connect to the registry and download the list of plugins that have been defined and execute them – UDJ Feb 11 '14 at 12:17

2 Answers2

1

You could get the information about the current Maven version by using the following code snippet:

@Component
private RuntimeInformation runtime;

public void execute() {
  ArtifactVersion version = runtime.getApplicationVersion();
  getLog().info("Version: " + version);
...
}

The runtime also offers information about getBuildNumber(), getIncrementalVersion(), getMajorVersion(), getMinorVersion() and getQualifier(). That should fit your needs.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • I cant seem to locate the RuntimeInformation class, what package is it part of? – UDJ Feb 11 '14 at 11:54
1

Using getApplicationVersion() as mentioned by khmarbaise has been deprecated, getMavenVersion() is the recommended function to be used. You need to include maven-core 3.0.2 or higher as a dependency to use the RuntimeInformation class.

here is the complete usage example,

Required pom dependency

<dependency>
   <groupId>org.apache.maven</groupId>
   <artifactId>maven-core</artifactId>
   <version>3.0.2</version>
</dependency>

Package import

import org.apache.maven.rtinfo.RuntimeInformation;

Usage

 /**
 *
 * @component
 */
private RuntimeInformation runtime;

public void execute() {
     String version = runtime.getMavenVersion();
     getLog().info("Maven Version: " + version);
     ...
}

@khmarbaise, Thanks for pointing me in the right direction

UDJ
  • 281
  • 2
  • 10