diff --git a/libbuild/GitRevMavenTask/GitRevMavenTask.java b/libbuild/GitRevMavenTask/GitRevMavenTask.java new file mode 100644 index 000000000..ebee6b1db --- /dev/null +++ b/libbuild/GitRevMavenTask/GitRevMavenTask.java @@ -0,0 +1,119 @@ + +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.List; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.plugins.annotations.Component; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevTag; +import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; + +/** + * Maven plugin to create property with YaCy release number, + * a 4 digit number based on commits to the Git repository + * + * @phase initialize + */ +@Mojo(name = "create") +public class GitRevMavenTask extends AbstractMojo { + + @Component + private MavenProject project; + /** + * Name of the buildNumber property + * + * parameter expression="${maven.buildNumber.buildNumberPropertyName}" + * default-value="releaseNr" + */ + @Parameter + private String branchPropertyName = "branch"; + @Parameter + private String buildNumberPropertyName = "releaseNr"; + @Parameter + private String commitDatePropertyName = "DSTAMP"; + + Log log = this.getLog(); + + public void setBuildNumberPropertyName(String revprop) { + this.buildNumberPropertyName = revprop; + } + + public void setCommitDatePropertyName(String dateprop) { + this.commitDatePropertyName = dateprop; + } + + @Override + public void execute() throws MojoExecutionException { + + String branch = null; + String revision = null; + String lastTag = null; + String commitDate = null; + + try { + + final File src = project.getBasedir(); // set Git root path to project root + final Repository repo = new FileRepositoryBuilder().readEnvironment() + .findGitDir(src).build(); + branch = repo.getBranch(); + branch = "master".equals(branch) ? "" : "_" + branch; + final ObjectId head = repo.resolve("HEAD"); + + final Git git = new Git(repo); + + final List tags = git.tagList().call(); + + final RevWalk walk = new RevWalk(repo); + final RevCommit headCommit = walk.parseCommit(head); + final SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd"); + commitDate = df.format(headCommit.getAuthorIdent().getWhen()); + walk.markStart(headCommit); + int distance = 0; + for (final RevCommit commit : walk) { + for (final RevTag tag : tags) { + if (commit.equals(tag.getObject())) { + lastTag = tag.getShortMessage(); + break; + } + } + if (lastTag != null || distance++ > 999) { + break; + } + } + walk.dispose(); + if (lastTag == null) { + revision = "0000"; + } else { + revision = Integer.toString(distance + 9000); + } + } catch (final IOException e) { + e.printStackTrace(); + } + if (project != null) { + project.getProperties().put(this.branchPropertyName, branch); + log.info("GitrevMavenTask: set property " + this.branchPropertyName + "='" + branch + "'"); + project.getProperties().put(this.buildNumberPropertyName, revision); + log.info("GitrevMavenTask: set property " + this.buildNumberPropertyName + "=" + revision); + project.getProperties().put(this.commitDatePropertyName, commitDate); + log.info("GitrevMavenTask: set property " + this.commitDatePropertyName + "=" + commitDate); + } else { + log.error("GitrevMavenTask: no Maven project"); + System.out.println(this.branchPropertyName + "=" + branch); + System.out.println(this.buildNumberPropertyName + "=" + revision); + System.out.println(this.commitDatePropertyName + "=" + commitDate); + } + } + + +} diff --git a/libbuild/pom.xml b/libbuild/pom.xml index d91b35cbf..596a541e6 100644 --- a/libbuild/pom.xml +++ b/libbuild/pom.xml @@ -3,11 +3,11 @@ 4.0.0 net.yacy - GitRevTask - 1.3 - jar - YaCy - version tool - YaCy + maven-plugin-gitrevisionnumber + 1.0 + maven-plugin + YaCy - Build Properties Utility + This plugin gives you a Git repository release number. in the format 9000 as maven property "releaseNr" and a Git repository timestamp as Maven property "DSTAMP" in the format yyyymmdd http://www.yacy.net @@ -26,8 +26,7 @@ - GitRevTask - + GitRevMavenTask @@ -68,8 +67,6 @@ - - org.codehaus.mojo @@ -85,26 +82,60 @@ + + org.apache.maven.plugins + maven-plugin-plugin + 3.2 + + + true + + + + mojo-descriptor + + descriptor + + + + + + - log4j - log4j - 1.2.16 - jar + org.apache.maven + maven-plugin-api + 3.1.0 org.eclipse.jgit org.eclipse.jgit - 1.2.0.201112221803-r + 1.3.0.201202151440-r + jar org.apache.ant ant - 1.8.3 + 1.9.2 + + + org.apache.maven + maven-project + 3.0-alpha-2 + + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.2 + + + org.apache.maven + maven-core + 3.1.1