使用maven項目+git來管理項目,現在想在發版的時候,知道包實際使用的git記錄的版本號。
<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.properties文件給生成到項目的resource目錄下。這個時候,在項目加載resource的時候,就可以直接加載這個git信息文件,在代碼裏面就可以使用@value來使用這個文件裏面,在日誌中把這個使用的git版本好給打印出來。
生成的git.properties文件如下;