Full impl

This commit is contained in:
Braydon 2023-08-03 06:43:38 -04:00
parent 579008c360
commit 011ecef3a6
5 changed files with 189 additions and 21 deletions

1
.gitignore vendored

@ -18,6 +18,7 @@ cmake-build-*/
out/ out/
build/ build/
work/ work/
target/
.idea_modules/ .idea_modules/
atlassian-ide-plugin.xml atlassian-ide-plugin.xml
com_crashlytics_export_strings.xml com_crashlytics_export_strings.xml

@ -1,2 +1,4 @@
# maven-git-versioning # 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.
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.

107
pom.xml

@ -1,34 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<name>Maven Git Versioning</name>
<groupId>me.braydon</groupId> <groupId>me.braydon</groupId>
<artifactId>maven-git-versioning</artifactId> <artifactId>git-versioning-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0</version>
<packaging>maven-plugin</packaging>
<properties> <properties>
<maven.compiler.source>8</maven.compiler.source> <java.version>8</java.version>
<maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>${java.version}</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>${java.version}</maven.compiler.target>
<maven-plugin-tools.version>3.9.0</maven-plugin-tools.version>
</properties> </properties>
<build> <build>
<plugins> <plugins>
<!--Used for compiling the source code with the proper Java version-->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <version>3.11.0</version>
<configuration> <configuration>
<source>8</source> <source>${java.version}</source>
<target>8</target> <target>${java.version}</target>
<!--Enable incremental builds, this is reversed due to-->
<!--a bug as seen in https://issues.apache.org/jira/browse/MCOMPILER-209-->
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration> </configuration>
</plugin> </plugin>
<!--Handles shading of dependencies in the final output jar-->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version> <version>3.5.0</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<!-- Exclude META-INF directory from being shaded -->
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.MF</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.DSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions> <executions>
<execution> <execution>
<phase>package</phase> <phase>package</phase>
@ -39,12 +64,60 @@
</executions> </executions>
</plugin> </plugin>
</plugins> </plugins>
<resources>
<resource> <!-- Plugin Management -->
<directory>src/main/resources</directory> <pluginManagement>
<filtering>true</filtering> <plugins>
</resource> <!-- Generate a help mojo -->
</resources> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>${maven-plugin-tools.version}</version>
<executions>
<execution>
<id>help-mojo</id>
<goals>
<goal>helpmojo</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build> </build>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>5.12.0.202106070339-r</version>
<scope>compile</scope>
</dependency>
<!-- Maven Plugin -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.9.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-tools-java</artifactId>
<version>${maven-plugin-tools.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>${maven-plugin-tools.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project> </project>

@ -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.
* <p>
* "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.
* <p>
* If successful, a map entry is returned containing
* the commit hash as the key, and the commit depth
* as the value.
* <p>
* If there is no local git repository, or
* an error has occurred, then null is returned.
* </p>
*
* @return the map entry containing the git data
* @see Map.Entry for map entry
*/
public static Map.Entry<String, Integer> 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<Ref> 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;
}
}

@ -12,6 +12,11 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; 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 * @author Braydon
*/ */
@Mojo(name = "versioning", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true) @Mojo(name = "versioning", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
@ -40,16 +45,22 @@ public final class VersioningMojo extends AbstractMojo {
int depth = 0; // The amount of commits since the last tag int depth = 0; // The amount of commits since the last tag
String commitHash = "unknown"; // The commit hash of the current commit String commitHash = "unknown"; // The commit hash of the current commit
long before = System.currentTimeMillis(); // The time before retrieving git data
Map.Entry<String, Integer> gitData = GitUtils.getGitData(); // Get the git data Map.Entry<String, Integer> gitData = GitUtils.getGitData(); // Get the git data
if (gitData == null) { // No git data if (gitData == null) { // No git data
System.out.println("Failed to retrieve git data, is this a git repository?"); System.out.println("Failed to retrieve git data, is this a git repository?");
} else { } else {
depth = gitData.getValue(); // Set the depth 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 // Updating the output jar name
Build build = project.getBuild(); // Get the build 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
)));
} }
} }