Skip to content

Instantly share code, notes, and snippets.

@yangl
Last active January 24, 2024 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yangl/d8918cdb5f395a717f78421ca3742594 to your computer and use it in GitHub Desktop.
Save yangl/d8918cdb5f395a717f78421ca3742594 to your computer and use it in GitHub Desktop.
动态生成版本号工具

1. 添加依赖

<build>
        <plugins>
            <plugin>
                <groupId>io.github.git-commit-id</groupId>
                <artifactId>git-commit-id-maven-plugin</artifactId>
                <version>7.0.0</version>
                <executions>
                    <execution>
                        <id>git-info</id>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <skip>false</skip>
                    <useNativeGit>true</useNativeGit>
                    <prefix>git</prefix>
                    <verbose>false</verbose>
                    <failOnNoGitDirectory>false</failOnNoGitDirectory>
                    <failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
                    <format>properties</format>
                    <gitDescribe>
                        <skip>true</skip>
                    </gitDescribe>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>templating-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>filter-src</id>
                        <goals>
                            <goal>filter-sources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2. 添加模板类放在src/main/java-templates下,里边写要动态生成的变量,如

package com.sf.kafka;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SFKafkaClientVersion {

	private static final Pattern majorMinorPatchPattern = Pattern.compile("([0-9]+)\\.([0-9]+)\\.([0-9]+)(.*)");

	// Pattern for version missing the patch number: eg: 1.14-SNAPSHOT
	private static final Pattern majorMinorPatter = Pattern.compile("([0-9]+)\\.([0-9]+)(.*)");

	// If the version string does not contain a patch version, add one so the
	// version becomes valid according to the SemVer library (see
	// https://github.com/zafarkhaja/jsemver).
	// This method (and it's only call above in the ctor) may be removed when SemVer
	// accepts null patch versions
	static String fixVersionString(String version) {
		if (null == version) {
			return null;
		}

		Matcher majorMinorPatchMatcher = majorMinorPatchPattern.matcher(version);

		if (majorMinorPatchMatcher.matches()) {
			// this is a valid version, containing a major, a minor, and a patch version
			// (and optionally
			// a release candidate version and/or build metadata)
			return version;
		}
		else {
			// the patch version is missing, so add one ("0")
			Matcher matcher2 = majorMinorPatter.matcher(version);

			if (matcher2.matches()) {
				int startMajorVersion = matcher2.start(1);
				int stopMinorVersion = matcher2.end(2);
				int startReleaseCandidate = matcher2.start(3);

				String prefix = new String(version.getBytes(), startMajorVersion,
						(stopMinorVersion - startMajorVersion));
				String patchVersion = ".0";
				String suffix = new String(version.getBytes(), startReleaseCandidate,
						version.length() - startReleaseCandidate);

				return (prefix + patchVersion + suffix);
			}
			else {
				// This is an invalid version, let the JSemVer library fail when it parses
				// it
				return version;
			}
		}
	}

	public static String getVersion() {
		return fixVersionString("${project.version}");
	}

	public static String getGitSha() {
		String commit = "${git.commit.id}";
		String dirtyString = "${git.dirty}";
		if (commit.contains("git.commit.id")) {
			// this case may happen if you are building the sources
			// out of the git repository
			commit = "";
		}
		if (dirtyString == null || Boolean.valueOf(dirtyString)) {
			return commit + "(dirty)";
		}
		else {
			return commit;
		}
	}

	public static String getGitBranch() {
		return "${git.branch}";
	}

	public static String getBuildUser() {
		String email = "${git.build.user.email}";
		String name = "${git.build.user.name}";
		return String.format("%s <%s>", name, email);
	}

	public static String getBuildHost() {
		return "${git.build.host}";
	}

	public static String getBuildTime() {
		return "${git.build.time}";
	}

}

3. 使用

SFKafkaClientVersion.getVersion();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment