美文网首页
2019-07-22 在线查询jar包的git commit号

2019-07-22 在线查询jar包的git commit号

作者: 颜路_陈 | 来源:发表于2019-07-24 09:40 被阅读0次

目标

实现在线查询交付件中所有jar包commit版本号功能

步骤1

每个jar包编译时,都把git commit信息提取到target/class文件夹下,方便后续使用
pom添加如下插件

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>pl.project13.maven</groupId>
                    <artifactId>git-commit-id-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>revision</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <verbose>true</verbose>
                        <dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
                        <generateGitPropertiesFile>true</generateGitPropertiesFile>
                        <generateGitPropertiesFilename>${project.build.outputDirectory}/git_commit_detailed_info.properties</generateGitPropertiesFilename>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

步骤二

创建Git信息类;
由于默认收集到的版本信息较多,不需要所有的都打印出来

#Generated by Git-Commit-Id-Plugin
#Mon Jul 22 22:39:18 CST 2019
git.branch=master
git.build.host=USER-20170219KK
git.build.time=2019-07-22T22\:39\:18+0800
git.build.user.email=bamboo0519@qq.com
git.build.user.name=Administrator
git.build.version=0.0.1-SNAPSHOT
git.closest.tag.commit.count=
git.closest.tag.name=
git.commit.id=8c7004985eb098c16470cfd7a8d9bde953146c5a
git.commit.id.abbrev=8c70049
git.commit.id.describe=8c70049-dirty
git.commit.id.describe-short=8c70049-dirty
git.commit.message.full=commit all
git.commit.message.short=commit all
git.commit.time=2019-07-21T21\:33\:42+0800
git.commit.user.email=chenl0907@dtdream.com
git.commit.user.name=c0907
git.dirty=true
git.remote.origin.url=git@code.aliyun.com\:bamboo0519/my-reponsitory.git
git.tags=
git.total.commit.count=27

取其中一部分:

    @Slf4j
    @Data
    static class GitCommitInfo {
        private static final String GIT_COMMIT_ID = "git.commit.id";
        private static final String GIT_BUILD_TIME = "git.build.time";
        private static final String GIT_BUILD_USER_NAME= "git.build.user.name";
        private static final String GIT_REMOTE_ORIGIN_URL= "git.remote.origin.url";
        private static final String DEFAULT_VALUE= "null";
        private String commitId;
        private String buildUserName;
        private String buildTime;
        private String repositoryName;
        private String jarFilePath;

        static GitCommitInfo convert(Properties properties, String fileFullPath) {
            GitCommitInfo info = new GitCommitInfo();
            if(properties ==  null) {
                log.warn("convert[]parameter is null");
                return info;
            }
            info.setCommitId(properties.getProperty(GIT_COMMIT_ID, DEFAULT_VALUE));
            info.setBuildTime(properties.getProperty(GIT_BUILD_TIME, DEFAULT_VALUE));
            info.setBuildUserName(properties.getProperty(GIT_BUILD_USER_NAME, DEFAULT_VALUE));

            String remoteUrl = properties.getProperty(GIT_REMOTE_ORIGIN_URL, DEFAULT_VALUE);
            if(DEFAULT_VALUE.equals(remoteUrl)) {
                info.setRepositoryName(remoteUrl);
            } else {
                String[] splits = remoteUrl.split("/");
                String repositoryName = splits[splits.length-1];

                // 去掉仓库名称后缀中的 .git
                repositoryName = repositoryName.substring(0, repositoryName.length() - ".git".length());
                info.setRepositoryName(repositoryName);
                info.setJarFilePath(fileFullPath);
            }
            return info;
        }



        /**
         * 举例:
         * fileFullPath:
         *  file:/D:/WORKSPACE/aliyuncode/my-reponsitory/ibamboo-customizable-arch/     \
         *      target/ibamboo-customizable-arch-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib       \
         *      /demo_001-1.0-SNAPSHOT.jar!/git_commit_detailed_info.properties
         *  repositoryName:
         *      my-reponsitory
         *
         *   期望返回值:模块名称(my-reponsitory后面的一个片段):
         *      demo_001-1.0
         */

//        static String getModuleName(String fileFullPath, String repositoryName) {
//            int pos = fileFullPath.indexOf(repositoryName);
//            if (pos > 0) {
//                return fileFullPath.substring(pos+repositoryName.length()+1,
//                        fileFullPath.indexOf("/", pos+repositoryName.length()+1));
//            }
//
//            return "null";
//        }
    }

步骤三
添加测试接口

@RestController
public class TestController {
    private static final String GIT_INFO_PROPERTIES_FILE_NAME = "git_commit_detailed_info.properties";
    @GetMapping("/version")
    public List<GitCommitInfo> getVersion() {
        Enumeration<URL> urls;
        try {
            urls = Thread.currentThread().getContextClassLoader().getResources(GIT_INFO_PROPERTIES_FILE_NAME);
            System.out.println("getVersion[] found file? "+urls.hasMoreElements());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        List<GitCommitInfo> gitCommitInfos = new ArrayList<>();
        while(urls.hasMoreElements()) {
            URL url = urls.nextElement();
            try(InputStream in = url.openStream()) {
                Properties p = new Properties();
                p.load(in);
                gitCommitInfos.add(GitCommitInfo.convert(p,url.getFile()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 读取key
        return gitCommitInfos;
    }

步骤四

测试结果:


查询结果.png

相关文章

网友评论

      本文标题:2019-07-22 在线查询jar包的git commit号

      本文链接:https://www.haomeiwen.com/subject/mooglctx.html