Thomas Kelley
Thomas Kelley

Reputation: 10302

Modifying artifact version in a Maven Mojo

Just starting to get into Maven and building custom Mojos. Hoping someone can help here.

The general idea is that I want to modify the version in the POM without modifying the POM itself. I'm going to use this functionality to add qualifiers to the end of the version that gets deployed. For example, I might change 1.0 to 1.0-SNAPSHOT, 1.0-RELEASE, 1.0.123 (based on the most recent incremental version), or 1.0-a1b2c3d (based on a Git commit hash). The goal here is that the developer doesn't have to worry about changing the version number (when it doesn't matter), but rather the build handles that.

So I'm trying to update the version number as part of the initialize phase, but it's not sticking in all the places I'm expecting. Here's my Mojo:

package com.tomkel.maven;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;

/**
 * This goal overrides the current artifact version
 *
 * @goal overrideVersion
 *
 * @phase initialize
 */
public class VersionOverrideGoal
    extends AbstractMojo
{
    /**
     * @parameter default-value="${project}"
     */
    private MavenProject mavenProject;

    /**
     * Run the Mojo
     * @throws org.apache.maven.plugin.MojoExecutionException
     */
    public void execute()
        throws MojoExecutionException
    {
        this.mavenProject.setVersion("TRY_THIS");
        this.mavenProject.getArtifact().setVersion("TRY_THIS_TOO");
    }

}

But when I include this in my project's POM:

<plugin>
    <groupId>com.tomkel</groupId>
    <artifactId>tk-version-override-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
        <execution>
            <goals>
                <goal>overrideVersion</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Then I see something like this during the install phase of the project build:

[INFO] Building jar: /home/tkelley/git/tomkel/some-project/target/some-project-1.2.3.jar
[INFO] 
[INFO] --- maven-install-plugin:2.3:install (default-install) @ some-project ---
[INFO] Installing /home/tkelley/git/tomkel/some-project/target/some-project-1.2.3.jar to /home/tkelley/.m2/repository/com/tomkel/some-project/TRY_THIS_TOO/some-project-TRY_THIS_TOO.jar
[INFO] Installing /home/tkelley/git/tomkel/some-project/pom.xml to /home/tkelley/.m2/repository/com/tomkel/some-project/TRY_THIS_TOO/some-project-TRY_THIS_TOO.pom

Because the artifact is built as some-project-1.2.3.jar, my repository (nexus) is adding this as version 1.2.3. But that's not what I want -- I'd like it be added as TRY_THIS_TOO.

Is there something I'm doing wrong here?

If it helps, I've put the Mojo code, the POMs, and build logs into a Gist here: https://gist.github.com/tomkel5/8199801

Thanks in advance!

Upvotes: 2

Views: 719

Answers (1)

solatic
solatic

Reputation: 56

It sounds like you want to use the Versions Maven Plugin to manage your versioning.

Upvotes: 2

Related Questions