首页 > 技术文章 > maven项目添加git版本信息

yytxdy 2020-07-01 16:47 原文

使用插件

            <plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <verbose>true</verbose>
                    <generateGitPropertiesFile>true</generateGitPropertiesFile>
                    <injectAllReactorProjects>true</injectAllReactorProjects>
                </configuration>
            </plugin>

添加VersionController

@Controller
public class VersionController {
    private static final Logger logger = LoggerFactory.getLogger(VersionController.class);
    private static final String VERSION_FILE = "git.properties";

    @RequestMapping("/version")
    @ResponseBody
    public String version() {
        try (InputStream is = getClass().getClassLoader().getResourceAsStream(VERSION_FILE)) {
            return IOUtils.toString(is).replaceAll("\r\n", "<br/>");
        } catch (IOException e) {
            logger.error("", e);
            return "Can Not Found File: " + VERSION_FILE;
        }
    }
}

项目打包时会在classes目录下生成git.properties文件,访问http://localhost:8080/appname/version.do即可查看线上运行的项目版本信息

推荐阅读