maven常用的外掛
阿新 • • 發佈:2020-11-14
補充幾個maven常用的外掛。
1.git-commit-id-plugin
官網:https://github.com/git-commit-id/git-commit-id-maven-plugin
這個外掛在分散式部署環境非常有用,可以通過此外掛看出打包所處的git環境,分支、倉庫、提交的head資訊等。
包含兩個目標:
1.git-commit-id:revision:將構建時的資訊儲存到指定檔案中或maven的屬性中。
2.git-commit-id:validateRevision:校驗屬性是否符合預期值,預設繫結階段:verify。(這個不常用)
1. 增加git.properties資訊和增加Controller檢視資訊
1.pom引入
<!-- git-commit-id-plugin --> <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>yyyyMMddHHmmss</dateFormat> <!--是否生成"git.properties"檔案;預設值:false;--> <generateGitPropertiesFile>true</generateGitPropertiesFile> <!--指定"git.properties"檔案的存放路徑--> <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename> </configuration> </plugin>
2.執行revision 目標
liqiang@root MINGW64 /e/xiangmu/springboot-ssm (master) $ mvn git-commit-id:revision [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building springboot-ssm 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- git-commit-id-plugin:3.0.1:revision (default-cli) @ springboot-ssm --- [INFO] dotGitDirectory E:\xiangmu\springboot-ssm\.git [INFO] Collected git.build.user.name with value qiaozhi [INFO] Collected git.build.user.email with value qiao_liqiang@163.com [INFO] Collected git.branch with value master [INFO] --always = true [INFO] --dirty = -dirty [INFO] --abbrev = 7 [INFO] Tag refs [[]] [INFO] Created map: [{}] [INFO] evalCommit is [9120cf8905b7a6a44fbfe755c511b0602efc16e7] [INFO] Collected git.commit.id.describe with value 9120cf8-dirty [INFO] Collected git.commit.id.describe-short with value 9120cf8-dirty [INFO] Collected git.commit.id with value 9120cf8905b7a6a44fbfe755c511b0602efc16e7 [INFO] Collected git.commit.id.abbrev with value 9120cf8 [INFO] Collected git.dirty with value true [INFO] Collected git.commit.user.name with value qqiaoliqiang [INFO] Collected git.commit.user.email with value 954318308@qq.com [INFO] Collected git.commit.message.full with value ▒▒▒▒git▒▒Ϣ▒鿴▒▒▒ [INFO] Collected git.commit.message.short with value ▒▒▒▒git▒▒Ϣ▒鿴▒▒▒ [INFO] Collected git.commit.time with value 20201113140739 [INFO] Collected git.remote.origin.url with value https://github.com/qiao-zhi/springboot-ssm.git [INFO] Collected git.tags with value [INFO] evalCommit is [9120cf8905b7a6a44fbfe755c511b0602efc16e7] [INFO] Tag refs [[]] [INFO] Created map: [{}] [INFO] Collected git.closest.tag.name with value [INFO] evalCommit is [9120cf8905b7a6a44fbfe755c511b0602efc16e7] [INFO] Tag refs [[]] [INFO] Created map: [{}] [INFO] Collected git.closest.tag.commit.count with value [INFO] Collected git.total.commit.count with value 78 [INFO] Collected git.local.branch.ahead with value 0 [INFO] Collected git.local.branch.behind with value 0 [INFO] Collected git.build.time with value 20201114202916 [INFO] Collected git.build.version with value 0.0.1-SNAPSHOT [INFO] Collected git.build.host with value root [INFO] including property git.build.user.email in results [INFO] including property git.build.host in results [INFO] including property git.dirty in results [INFO] including property git.local.branch.behind in results [INFO] including property git.remote.origin.url in results [INFO] including property git.closest.tag.name in results [INFO] including property git.local.branch.ahead in results [INFO] including property git.total.commit.count in results [INFO] including property git.commit.id.describe-short in results [INFO] including property git.commit.user.email in results [INFO] including property git.commit.time in results [INFO] including property git.commit.message.full in results [INFO] including property git.build.version in results [INFO] including property git.commit.message.short in results [INFO] including property git.commit.id.abbrev in results [INFO] including property git.branch in results [INFO] including property git.build.user.name in results [INFO] including property git.closest.tag.commit.count in results [INFO] including property git.commit.id.describe in results [INFO] including property git.commit.id in results [INFO] including property git.tags in results [INFO] including property git.build.time in results [INFO] including property git.commit.user.name in results [INFO] Writing properties file to [E:\xiangmu\springboot-ssm\target\classes\git.properties] (for module springboot-ssm)... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 16.472 s [INFO] Finished at: 2020-11-14T20:29:16+08:00 [INFO] Final Memory: 23M/245M [INFO] ------------------------------------------------------------------------
會在編譯目錄生成git.properties檔案:
檔案內容是git相關資訊。內容大致如下:
#Generated by Git-Commit-Id-Plugin #Sat Nov 14 20:29:16 CST 2020 git.branch=master git.build.host=root git.build.time=20201114202916 git.build.user.email=qiao_liqiang@163.com git.build.user.name=qiaozhi git.build.version=0.0.1-SNAPSHOT git.closest.tag.commit.count= git.closest.tag.name= git.commit.id=9120cf8905b7a6a44fbfe755c511b0602efc16e7 git.commit.id.abbrev=9120cf8 git.commit.id.describe=9120cf8-dirty git.commit.id.describe-short=9120cf8-dirty git.commit.message.full=\u589E\u52A0git\u4FE1\u606F\u67E5\u770B\u63D2\u4EF6 git.commit.message.short=\u589E\u52A0git\u4FE1\u606F\u67E5\u770B\u63D2\u4EF6 git.commit.time=20201113140739 git.commit.user.email=954318308@qq.com git.commit.user.name=qqiaoliqiang git.dirty=true git.local.branch.ahead=0 git.local.branch.behind=0 git.remote.origin.url=https\://github.com/qiao-zhi/springboot-ssm.git git.tags= git.total.commit.count=78
build.host是build的計算機名稱。下面是提交資訊以及分支資訊、總提交次數等資訊。
可以通過Controller暴露資訊,這樣可以通過Controller檢視打包資訊。
(1)類資訊如下:
package cn.qlq.git; import lombok.Data; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; /** * @author: 喬利強 * @date: 2020/11/13 12:45 * @description: */ @Configuration @PropertySource(value = "classpath:git.properties", ignoreResourceNotFound = true) public class BuildInfoProperties { @Autowired private Environment env; @Bean public BuildInfo getBuildInfo() { BuildInfo buildInfo = new BuildInfo(); buildInfo.gitDirty = env.getProperty("git.dirty"); buildInfo.gitRemoteOriginUrl = env.getProperty("git.remote.origin.url"); buildInfo.gitTags = env.getProperty("git.tags"); buildInfo.gitBranch = env.getProperty("git.branch"); buildInfo.gitCommitUserName = env.getProperty("git.commit.user.name"); buildInfo.gitCommitTime = env.getProperty("git.commit.time"); buildInfo.gitCommitMessageFull = env.getProperty("git.commit.message.full"); buildInfo.gitCommitId = env.getProperty("git.commit.id"); buildInfo.gitBuildVersion = env.getProperty("git.build.version"); buildInfo.gitBuildUserName = env.getProperty("git.build.user.name"); buildInfo.gitBuildTime = env.getProperty("git.build.time"); buildInfo.gitBuildHost = env.getProperty("git.build.host"); return buildInfo; } @Data public static class BuildInfo { private String gitDirty; private String gitRemoteOriginUrl; private String gitTags; private String gitBranch; private String gitCommitUserName; private String gitCommitTime; private String gitCommitMessageFull; private String gitCommitId; private String gitBuildVersion; private String gitBuildUserName; private String gitBuildTime; private String gitBuildHost; } }
Controller資訊如下:
package cn.qlq.git; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author: 喬利強 * @date: 2020/11/13 12:46 * @description: */ @RestController public class BuildInfoController { @Autowired private BuildInfoProperties.BuildInfo buildInfo; @GetMapping(value = "/buildinfo", produces = {"application/json;charset=UTF-8"}) public String getBuildInfo() { return buildInfo.toString(); } }
(2)啟動測試:
$ curl http://localhost:8088/buildinfo % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- 100 411 100 411 0 0 13258 0 --:--:-- --:--:-- --:--:-- 27400BuildInfoProperties.BuildInfo(gitDirty=true, gitRemoteOriginUrl=https://github.com/qiao-zhi/springboot-ssm.git, gitTags=, gitBranch=master, gitCommitUserName=qqiaoliqiang, gitCommitTime=20201113140739, gitCommitMessageFull=增加git資訊檢視外掛, gitCommitId=9120cf8905b7a6a44fbfe755c511b0602efc16e7, gitBuildVersion=0.0.1-SNAPSHOT, gitBuildUserName=qiaozhi, gitBuildTime=20201114202916, gitBuildHost=root)
補充 :關於Environment 和 @PropertySource 的關係
Spring抽象了一個Environment來表示Spring應用程式環境配置,它整合了各種各樣的外部環境,並且提供統一訪問的方法。
@PropertySource 是Java Config方式的註解,其屬性會自動註冊到相應的Environment
@GetMapping(value = "/testEnv") public String testEnv() { String serverPort = env.getProperty("server.port"); String commitId = env.getProperty("git.commit.id"); return serverPort + "\t" + commitId; }
測試:Environment可以讀到application.properties配置的資訊和@PropertySource 讀取的properties檔案的資訊
$ curl http://localhost:8088/testEnv % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- 100 45 100 45 0 0 64 0 --:--:-- --:--:-- --:--:-- 66 8088 9120cf8905b7a6a44fbfe755c511b0602efc16e7
2.配置打包名稱, 可以從打包後文件檢視commit的版本號
1.修改pom中打包的版本資訊
<version>0.0.1-SNAPSHOT-${git.commit.id.abbrev}</version>
2.執行package打包
結果:
[INFO] Building war: E:\xiangmu\springboot-ssm\target\springboot-ssm-0.0.1-SNAPSHOT-9120cf8.war