views:

48

answers:

2

I'd like to be able to discover the version of my plugin during its execution; 0.0.1-SNAPSHOT, 0.0.1, 1.0-SNAPSHOT, etc.

Can this be done? The AbstractMojo class doesn't really give you much information about the plugin itself.

EDIT - I am using the following code as a workaround. It assumes that the MANIFEST for the plugin can be loaded from a resource URL built using the resource URL of the plugin itself. It's not nice but seems to work for MANIFEST located in either file or jar class loader:

String getPluginVersion() throws IOException {
    Manifest mf = loadManifest(getClass().getClassLoader(), getClass());
    return mf.getMainAttributes().getValue("Implementation-Version");
}

Manifest loadManifest(final ClassLoader cl, final Class c) throws IOException {
    String resourceName = "/" + c.getName().replaceAll("\\.", "/") + ".class";
    URL classResource = cl.getResource(resourceName);
    String path = classResource.toString();

    int idx = path.indexOf(resourceName);
    if (idx < 0) {
        return null;
    }

    String urlStr = classResource.toString().substring(0, idx) + "/META-INF/MANIFEST.MF";

    URL url = new URL(urlStr);

    InputStream in = null;
    Manifest mf = null;
    try {
        in = url.openStream();
        mf = new Manifest(in);
    } finally {
        if (null != in) {
            in.close();
        }
        in = null;
    }

    return mf;
}
A: 

First, add the following dependency to your plugin's POM:

<dependency>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-project</artifactId>
  <version>2.0</version>
</dependency>

Then you can just do the following:

public class MyMojo extends AbstractMojo {

private static final String GROUP_ID = "your-group-id";
private static final String ARTIFACT_ID = "your-artifact-id";

/**
 * @parameter default-value="${project}"
 */
MavenProject project;

public void execute() throws MojoExecutionException {
    Set pluginArtifacts = project.getPluginArtifacts();
    for (Iterator iterator = pluginArtifacts.iterator(); iterator.hasNext();) {
        Artifact artifact = (Artifact) iterator.next();
        String groupId = artifact.getGroupId();
        String artifactId = artifact.getArtifactId();
        if (groupId.equals(GROUP_ID) && artifactId.equals(ARTIFACT_ID)) {
            System.out.println(artifact.getVersion());
            break;
        }
    }
}
Shane Bell
I don't think this is correct. If the plugin is run against a POM then the version of the POM will be returned, not the version of the plugin.
Paul Grime
Hi Paul, yeah you're right it was giving the version of the project, not the plugin. I've updaed the code accordingly. It's not a particularly elegant solution, but seems to work. Perhaps someone else knows a cleaner way to do this.
Shane Bell
+1  A: 

I don't think your "workaround" with the manifest file is such a bad idea. Since it's packed inside the .jar of your plugin you should always have access to it.

For this post to be an answer, here is another idea: Let maven do the dirty work for you during the build of your plugin: have a placeholder in your plugin source:

private final String myVersion = "[CURRENT-VERSION]";

use ant-plugin or something else to replace that placeholder with the current version before compilation.

xor_eq