Full impl
This commit is contained in:
parent
579008c360
commit
011ecef3a6
1
.gitignore
vendored
1
.gitignore
vendored
@ -18,6 +18,7 @@ cmake-build-*/
|
||||
out/
|
||||
build/
|
||||
work/
|
||||
target/
|
||||
.idea_modules/
|
||||
atlassian-ide-plugin.xml
|
||||
com_crashlytics_export_strings.xml
|
||||
|
@ -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.
|
||||
|
||||
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
107
pom.xml
@ -1,34 +1,59 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project 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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<name>Maven Git Versioning</name>
|
||||
<groupId>me.braydon</groupId>
|
||||
<artifactId>maven-git-versioning</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<artifactId>git-versioning-maven-plugin</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>maven-plugin</packaging>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>8</java.version>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<maven-plugin-tools.version>3.9.0</maven-plugin-tools.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!--Used for compiling the source code with the proper Java version-->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<version>3.11.0</version>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
<source>${java.version}</source>
|
||||
<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>
|
||||
</plugin>
|
||||
|
||||
<!--Handles shading of dependencies in the final output jar-->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<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>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
@ -39,12 +64,60 @@
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<!-- Plugin Management -->
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<!-- Generate a help mojo -->
|
||||
<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>
|
||||
|
||||
<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>
|
81
src/main/java/me/braydon/plugin/GitUtils.java
Normal file
81
src/main/java/me/braydon/plugin/GitUtils.java
Normal file
@ -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;
|
||||
|
||||
/**
|
||||
* 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<String, Integer> 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
|
||||
)));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user