com.labun.buildnumber.JGitBuildNumberMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jgit-buildnumber-maven-plugin Show documentation
Show all versions of jgit-buildnumber-maven-plugin Show documentation
Extracts Git metadata and a freely composable build number in pure Java without Git command-line tool. Eclipse m2e compatible.
package com.labun.buildnumber;
import static com.labun.buildnumber.BuildNumberExtractor.propertyNames;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.TreeMap;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.sonatype.plexus.build.incremental.BuildContext;
import lombok.Getter;
import lombok.Setter;
/** Extracts Git metadata and creates build number. Publishes them as project properties. */
@Getter
@Setter
@Mojo(name = "extract-buildnumber", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true)
public class JGitBuildNumberMojo extends AbstractMojo implements Parameters {
@Component
private BuildContext buildContext;
// ---------- parameters (user configurable) ----------
private @Parameter String namespace;
private @Parameter String dirtyValue;
private @Parameter Integer shortRevisionLength;
private @Parameter String gitDateFormat;
private @Parameter String buildDateFormat;
private @Parameter String dateFormatTimeZone;
private @Parameter String countCommitsSinceInclusive;
private @Parameter String countCommitsSinceExclusive;
private @Parameter String countCommitsInPath;
private @Parameter String buildNumberFormat;
private @Parameter File repositoryDirectory;
private @Parameter Boolean runOnlyAtExecutionRoot;
private @Parameter Boolean skip;
private @Parameter Boolean verbose;
// ---------- parameters (read only) ----------
@Parameter(property = "project.basedir", readonly = true, required = true)
private File baseDirectory;
@Parameter(property = "session.executionRootDirectory", readonly = true, required = true)
private File executionRootDirectory;
/** The maven project. */
@Parameter(property = "project", readonly = true)
private MavenProject project;
/** The maven parent project. */
@Parameter(property = "project.parent", readonly = true)
private MavenProject parentProject;
// ---------- implementation ----------
/** Extracts buildnumber fields from git repository and publishes them as maven properties.
* Executes only once per build. Return default (unknown) buildnumber fields on error. */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
long start = System.currentTimeMillis();
// set some parameters to Maven specific values
if (getRepositoryDirectory() == null) setRepositoryDirectory(project.getBasedir()); // ${project.basedir}
validateAndSetParameterValues();
if (skip) {
getLog().info("Execution is skipped by configuration.");
return;
}
if (verbose) getLog().info("JGit BuildNumber Maven Plugin - start");
if (verbose) getLog().info("executionRootDirectory: " + executionRootDirectory + ", baseDirectory: " + baseDirectory);
try {
// accesses Git repo only once per build
// http://www.sonatype.com/people/2009/05/how-to-make-a-plugin-run-once-during-a-build/
if (!runOnlyAtExecutionRoot || executionRootDirectory.equals(baseDirectory)) {
BuildNumberExtractor extractor = new BuildNumberExtractor(this, msg -> getLog().info(msg));
String headSha1 = extractor.getHeadSha1();
String dirty = extractor.isGitStatusDirty() ? dirtyValue : null;
List