使用maven项目+git来管理项目,如今想在发版的时候,知道包实际使用的git记录的版本号。java
<plugin> <groupId>pl.project13.maven</groupId> <artifactId>git-commit-id-plugin</artifactId> <version>2.1.5</version> <executions> <execution> <goals> <goal>revision</goal> </goals> </execution> </executions> <configuration> <!--日期格式;默认值:dd.MM.yyyy '@' HH:mm:ss z;--> <dateFormat>yyyyMMddHHmmss</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>/src/main/resource/git.properties</generateGitPropertiesFilename> <!--".git"文件夹未找到时,构建是否失败;若设置true,则构建失败;若设置false,则跳过执行该目标;默认值:true;--> <failOnNoGitDirectory>true</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>
在pom.xml里面的plugins里面添加上上面的这个这插件,具体参考下面图,找位置。git
添加完以后,上面我把这个生成的git.properties文件给生成到项目的resource目录下。这个时候,在项目加载resource的时候,就能够直接加载这个git信息文件,在代码里面就能够使用@value来使用这个文件里面,在日志中把这个使用的git版本好给打印出来。maven
生成的git.properties文件以下;插件