com.labun.buildnumber.JGitBuildNumberAntTask Maven / Gradle / Ivy
package com.labun.buildnumber;
import java.io.File;
import java.util.Map;
import org.apache.tools.ant.Project;
/** Extracts Git metadata and creates build number. Publishes them as Ant properties. See {@link JGitBuildNumberAntTask#execute()}. */
public class JGitBuildNumberAntTask {
private Project project;
/** @param project Ant project setter */
public void setProject(Project project) {
this.project = project;
}
/**
* Reads {@code git.repositoryDirectory} property that should contain '.git' directory
* or be a subdirectory of such directory. If property wasn't set current work directory is used instead.
* Extracted properties names:
*
* - {@code git.revision}
* - {@code git.shortRevision}
* - {@code git.dirty}
* - {@code git.branch}
* - {@code git.tag}
* - {@code git.parent}
* - {@code git.shortParent}
* - {@code git.commitsCount}
* - {@code git.authorDate}
* - {@code git.commitDate}
* - {@code git.describe}
* - {@code git.buildDate}
* - {@code git.buildNumber}
*
* @throws Exception if git repo not found or cannot be read
*/
public void execute() throws Exception {
String repoDirString = project.getProperty("git.repositoryDirectory");
File repoDir = null != repoDirString ? new File(repoDirString) : new File(".");
String gitDateFormat = project.getProperty("git.gitDateFormat");
String buildDateFormat = project.getProperty("git.buildDateFormat");
String dateFormatTimeZone = null;
String countCommitsSinceInclusive = project.getProperty("git.countCommitsSinceInclusive");
String countCommitsSinceExclusive = project.getProperty("git.countCommitsSinceExclusive");
String prefix = project.getProperty("git.prefix");
String dirtyValue = project.getProperty("git.dirtyValue");
String shortRevisionLength = project.getProperty("git.shortRevisionLength");
Map properties = new BuildNumberExtractor(repoDir).extract(shortRevisionLength, gitDateFormat, buildDateFormat, dateFormatTimeZone,
countCommitsSinceInclusive, countCommitsSinceExclusive, dirtyValue);
for (Map.Entry property : properties.entrySet())
project.setProperty(prefix + property.getKey(), property.getValue());
}
}