Mic
Mic

Reputation: 313

Using maven version in plugin

I am writing a plugin to Maven. I want to customize action depend on Maven version (person's who use my plugin) something like:

if (MavenVer == "2.2.1") 
    //do some things
if (MavenVer == "3.0")
//do another things

I found out build-helper-maven-plugin but i'm not shure how to use it to do this job.

Upvotes: 0

Views: 153

Answers (2)

BenjaminLinus
BenjaminLinus

Reputation: 2110

I have been looking for an answer to your question and I have come to the conclusion that there is no simple way to get the maven version, or at least I couldn't find it. The best I was able to find is that the maven-enforcer-plugin contains code that looks up the maven version. However in looking at the maven-enforcer-plugin source code it is a little complicated and involves getting a handle to a RuntimeInformation instance through plexus (the IoC container used by Maven).

I recommend you take a look at the maven-enforcer-plugin and see if you can use what they did. Pay particular attention to RequireMavenVersion and EnforcerMojo classes.

Upvotes: 0

Andrey Borisov
Andrey Borisov

Reputation: 3170

you need to inject maven reference into plugina and simply check version using API http://maven.apache.org/ref/2.2.1/maven-project/apidocs/org/apache/maven/project/MavenProject.html#getVersion()

http://maven.apache.org/ref/3.0.3/maven-core/apidocs/org/apache/maven/project/MavenProject.html#getVersion()

code for injection

public class MyMojo extends AbstractMojo {
/**
 * @component
 */
ArtifactFactory factory;
/**
 * @component
 */
ArtifactResolver artifactResolver;
/**
 * @parameter default-value="${project.remoteArtifactRepositories}"
 */
List remoteRepositories;
/**
 * @parameter default-value="${localRepository}"
 */
ArtifactRepository localRepository;
/**
 * @parameter default-value="${project}"
 */
MavenProject mavenProject;

Upvotes: 1

Related Questions