diff --git a/.gitignore b/.gitignore
index 71c0c56..d7f0fc0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -18,6 +18,7 @@ cmake-build-*/
out/
build/
work/
+target/
.idea_modules/
atlassian-ide-plugin.xml
com_crashlytics_export_strings.xml
diff --git a/README.md b/README.md
index 5a96414..866047a 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,4 @@
# maven-git-versioning
-This plugin seamlessly converts standard "-SNAPSHOT" versions into a more descriptive format, "-dev-{gitDepth}-{gitHash}", allowing you to gain valuable insights into the exact commit from which each jar was built.
\ No newline at end of file
+
+This plugin seamlessly converts standard "-SNAPSHOT" versions into a more descriptive format, "
+-dev-{gitDepth}-{gitHash}", allowing you to gain valuable insights into the exact commit from which each jar was built.
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index b6b2772..fba4e71 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,34 +1,59 @@
-4.0.0
+ Maven Git Versioningme.braydon
- maven-git-versioning
- 1.0-SNAPSHOT
+ git-versioning-maven-plugin
+ 1.0
+ maven-plugin
- 8
- 8
- UTF-8
+ 8
+ ${java.version}
+ ${java.version}
+ 3.9.0
+
org.apache.maven.pluginsmaven-compiler-plugin
- 3.8.1
+ 3.11.0
-
- 8
+
+ ${java.version}
+
+
+
+ false
+
+
org.apache.maven.pluginsmaven-shade-plugin
- 3.1.0
+ 3.5.0
+
+ false
+
+
+
+ *:*
+
+ META-INF/*.MF
+ META-INF/*.SF
+ META-INF/*.DSA
+ META-INF/*.DSA
+
+
+
+ package
@@ -39,12 +64,60 @@
-
-
- src/main/resources
- true
-
-
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-plugin-plugin
+ ${maven-plugin-tools.version}
+
+
+ help-mojo
+
+ helpmojo
+
+
+
+
+
+
+
+
+ org.projectlombok
+ lombok
+ 1.18.28
+ provided
+
+
+ org.eclipse.jgit
+ org.eclipse.jgit
+ 5.12.0.202106070339-r
+ compile
+
+
+
+
+ org.apache.maven
+ maven-plugin-api
+ 3.9.3
+ provided
+
+
+ org.apache.maven.plugin-tools
+ maven-plugin-tools-java
+ ${maven-plugin-tools.version}
+ provided
+
+
+ org.apache.maven.plugin-tools
+ maven-plugin-annotations
+ ${maven-plugin-tools.version}
+ provided
+
+
\ No newline at end of file
diff --git a/src/main/java/me/braydon/plugin/GitUtils.java b/src/main/java/me/braydon/plugin/GitUtils.java
new file mode 100644
index 0000000..add1ff6
--- /dev/null
+++ b/src/main/java/me/braydon/plugin/GitUtils.java
@@ -0,0 +1,81 @@
+package me.braydon.plugin;
+
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.internal.storage.file.FileRepository;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.AbstractMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Braydon
+ */
+public final class GitUtils {
+ /**
+ * Can't construct utility classes.
+ */
+ public GitUtils() {
+ throw new UnsupportedOperationException("Cannot instantiate utility class.");
+ }
+
+ /**
+ * Get the git data for the current repository.
+ *
+ * "Git data" is referred to as the latest commit
+ * hash, as well as the commit depth, or in other
+ * words, the amount of commits since the last tag.
+ *
+ * If successful, a map entry is returned containing
+ * the commit hash as the key, and the commit depth
+ * as the value.
+ *
+ * If there is no local git repository, or
+ * an error has occurred, then null is returned.
+ *
+ *
+ * @return the map entry containing the git data
+ * @see Map.Entry for map entry
+ */
+ public static Map.Entry getGitData() {
+ File gitDir = new File(".git");
+ if (!gitDir.exists()) { // Not a git repository, return null
+ return null;
+ }
+ try (
+ FileRepository repository = new FileRepository(gitDir); // Create a new file repository
+ Git git = new Git(repository); // Create a new git instance
+ RevWalk walk = new RevWalk(repository); // Create a new rev walk of the repository
+ ) {
+ RevCommit latestCommit = git.log().setMaxCount(1).call().iterator().next(); // Get the latest commit
+
+ // Find the commits since the last tag
+ int commitsSinceLastTag = 0;
+ List tags = git.tagList().call();
+ RevCommit commit = walk.parseCommit(repository.resolve("HEAD"));
+ while (true) {
+ for (Ref tag : tags) {
+ // If the commit is not the same as the tag, continue
+ if (!walk.parseCommit(tag.getObjectId()).equals(commit)) {
+ continue;
+ }
+ walk.dispose(); // Dispose of the rev walk
+
+ // Return the git data
+ return new AbstractMap.SimpleEntry<>(latestCommit.getName(), commitsSinceLastTag);
+ }
+ commitsSinceLastTag++; // Increment commits since last tag
+ commit = walk.parseCommit(commit.getParent(0)); // Get the parent commit
+ }
+ } catch (IOException | GitAPIException ex) {
+ System.err.println("Failed fetching git data:");
+ ex.printStackTrace();
+ }
+ return null;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/me/braydon/plugin/VersioningMojo.java b/src/main/java/me/braydon/plugin/VersioningMojo.java
index 9e0ad7b..aa0aaa4 100644
--- a/src/main/java/me/braydon/plugin/VersioningMojo.java
+++ b/src/main/java/me/braydon/plugin/VersioningMojo.java
@@ -12,6 +12,11 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
+ * The purpose of this mojo is to uniquely identify
+ * development builds of projects by modifying the
+ * output jar name to contain a commit depth, as well
+ * as a commit hash.
+ *
* @author Braydon
*/
@Mojo(name = "versioning", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
@@ -34,22 +39,28 @@ public final class VersioningMojo extends AbstractMojo {
return;
}
String target = matcher.group(); // Extract the found target version
-
+
// Fetch git data
System.out.println("Retrieving git data..."); // Log git data retrieval
int depth = 0; // The amount of commits since the last tag
String commitHash = "unknown"; // The commit hash of the current commit
+ long before = System.currentTimeMillis(); // The time before retrieving git data
Map.Entry gitData = GitUtils.getGitData(); // Get the git data
if (gitData == null) { // No git data
System.out.println("Failed to retrieve git data, is this a git repository?");
} else {
depth = gitData.getValue(); // Set the depth
- commitHash = gitData.getKey(); // Set the commit hash
+ commitHash = gitData.getKey().substring(0, 7); // Set the commit hash (trimmed)
+ System.out.println("Commits since last tag (depth): " + depth);
+ System.out.println("Last commit: " + commitHash);
}
+ System.out.println("Data retrieval took " + (System.currentTimeMillis() - before) + "ms");
// Updating the output jar name
Build build = project.getBuild(); // Get the build
- build.setFinalName(build.getFinalName().replace(target, "-dev-" + depth + "-" + commitHash));
+ build.setFinalName(build.getFinalName().replace(target, String.format("-dev-%s-%s",
+ depth, commitHash
+ )));
}
}
\ No newline at end of file