git-commit-id-plugin插件

2022/7/2 6:20:12

本文主要是介绍git-commit-id-plugin插件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

<plugin>
				<groupId>pl.project13.maven</groupId>
				<artifactId>git-commit-id-plugin</artifactId>
				<version>2.1.15</version>
				<executions>
					<execution>
						<goals>
							<goal>revision</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<!--日期格式;默认值:dd.MM.yyyy '@' HH:mm:ss z;-->
					<dateFormat>yyyy-MM-dd HH:mm:ssZ</dateFormat>
					<!--,构建过程中,是否打印详细信息;默认值:false;-->
					<verbose>true</verbose>
					<!-- ".git"文件路径;默认值:${project.basedir}/.git; -->
					<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
					<!--若项目打包类型为pom,是否取消构建;默认值:true;-->
					<skipPoms>false</skipPoms>
					<!--是否生成"git.properties"文件;默认值:false;-->
					<generateGitPropertiesFile>true</generateGitPropertiesFile>
					<!--指定"git.properties"文件的存放路径(相对于${project.basedir}的一个路径);-->
					<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
					<!--".git"文件夹未找到时,构建是否失败;若设置true,则构建失败;若设置false,则跳过执行该目标;默认值:true;-->
					<failOnNoGitDirectory>false</failOnNoGitDirectory>
					<!--git描述配置,可选;由JGit提供实现;-->
					<gitDescribe>
						<!--是否生成描述属性-->
						<skip>false</skip>
						<!--提交操作未发现tag时,仅打印提交操作ID,-->
						<always>false</always>
						<!--提交操作ID显式字符长度,最大值为:40;默认值:7;
                            0代表特殊意义;后面有解释;
                        -->
						<abbrev>7</abbrev>
						<!--构建触发时,代码有修改时(即"dirty state"),添加指定后缀;默认值:"";-->
						<dirty>-dirty</dirty>
						<!--always print using the "tag-commits_from_tag-g_commit_id-maybe_dirty" format, even if "on" a tag.
                            The distance will always be 0 if you're "on" the tag.
                        -->
						<forceLongFormat>false</forceLongFormat>
					</gitDescribe>
				</configuration>
			</plugin>
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 *
 * 系统版本号,关联git仓库提交信息,方便排查线上问题时回查历史代码
 *
 * 得到版本号 grep 'commitVersionNumber' app.log
 *
 * @author weiguangyue
 * @date 2022-07-01
 *
 */
public class Version {

    private static Properties PROPS = new Properties();

    static{
        InputStream inputStream = null;
        try{
            ClassLoader classLoader = Version.class.getClassLoader();
            inputStream = classLoader.getResourceAsStream("git.properties");
            if(inputStream != null){
                PROPS.load(inputStream);
            }else{
                System.out.println("git.properties is not found");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static String getRemoteOriginUrl(){
        return PROPS.getProperty("git.remote.origin.url","");
    }

    public static String getBuildTime(){
        return PROPS.getProperty("git.build.time","");
    }

    public static String getCommitTime(){
        return PROPS.getProperty("git.commit.time","");
    }

    public static String getCommitVersionNumber(){
        return PROPS.getProperty("git.commit.id","");
    }

    /**
     * 容器环境,快速得到版本号信息,而不是查询日志
     */
    public static void dump(){
        if(!System.getProperty("os.name").toLowerCase().contains("linux")){
            return;
        }
        String userDirStr = System.getProperty("user.dir");
        File userDir = new File(userDirStr);
        File versionInfoFile = new File(userDir,"git.properties");
        if(!versionInfoFile.exists()){
            try {
                versionInfoFile.createNewFile();
                PROPS.store(new FileOutputStream(versionInfoFile),"");
            } catch (IOException e) {
                System.err.println("dump git.properties error:"+e.getMessage());
            }
        }
    }

    public static void display(){
        System.out.println("--------------------------------version--------------------------------");
        System.out.println("gitUrl["+getRemoteOriginUrl()+"]");
        System.out.println("buildTime["+getBuildTime()+"]");
        System.out.println("commitTime["+getCommitTime()+"]");
        System.out.println("commitVersionNumber["+getCommitVersionNumber()+"]");
        System.out.println("------------------------------------------------------------------------");
    }

    public static void main(String[] args) {
        display();
    }
}



这篇关于git-commit-id-plugin插件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程