首页 > 其他 > 详细

maven常用的插件

时间:2020-11-14 22:46:09      阅读:39      评论:0      收藏:0      [点我收藏+]

  补充几个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

 

maven常用的插件

原文:https://www.cnblogs.com/qlqwjy/p/13974847.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!