Spring Boot 2.0 使用Maven git-commit-id插件

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(2656)   2023-03-28 11:29:14

一.概述

您是否遇到过不知道您的应用程序的哪个版本部署的问题,例如测试环境?或者是否必须手动修改每个版本的版本信息才能使其在“关于”对话框中可用?然后,Maven git commit id插件已经开始救援!在这篇文章中,我们将用一个RESTful Web服务构建一个Spring Boot应用程序来检索版本信息。我们唯一需要做的就是配置  Maven git commit id插件并创建Web服务。在此之后,版本信息会在每次构建过程中自动更新!


二. 创建一个Spring Boot 2.0 应用程序

首先,我们使用REST风格的Web服务创建基本的Spring Boot 2.0应用程序,以检索手动输入的版本信息。使用JDK9和模块,所以我们会遇到一些“问题”。但是这些也会被解决!

我们创建了VersionController类,其中包含硬编码的版本信息。我们稍后将用从git commit id插件检索到的信息进行替换  。

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
@RestController
public class VersionController {
    @RequestMapping("/version", method= GET)
    public String versionInformation() {
        return "Version 1.0-SNAPSHOT";
    }
}
Application.java类中添加Spring Boot应用程序的入口点  
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
使用clean install 清理并重新编译应用。module-info.java 必须含以下几点:
module mygitcommitidplanet {
    requires spring.web;
    requires spring.boot;
    requires spring.boot.autoconfigure;
}
构建应用程序出现以下错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:2.6:jar (default-jar) on project mygitcommitidplanet: Execution default-jar of goal org.apache.maven.plugins:maven-jar-plugin:2.6:jar failed: An API incompatibility was encountered while executing org.apache.maven.plugins:maven-jar-plugin:2.6:jar: java.lang.ExceptionInInitializerError: null
这通过使用maven jar 插件的版本3.0.2来解决  。在pom.xml文件中添加以下插件的依赖:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
</plugin>
在项目的根目录使用下面的命令运行项目:
java -jar target/mygitcommitidplanet-1.0-SNAPSHOT.jar
启动项目,下面的警告就显示出来了:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1...
这似乎是一个已知的问题,参见  SPR-15859
在浏览器中输入地址http://localhost:8080/version 如预期的那样,返回硬编码值:
Version 1.0-SNAPSHOT

三.git commit id插件

现在我们已经有了一切,我们可以开始添加git commit id插件。所有属性的详细信息可以在git commit id插件的GitHub存储库中找到。

启用git commit id

将以下内容添加到POM文件的  插件部分  :
 

<plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
    <version>2.2.4</version>
    <executions>
        <execution>
            <id>get-the-git-infos</id>
            <goals>
                <goal>revision</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
        <prefix>git</prefix>
        <verbose>false</verbose>
        <generateGitPropertiesFile>true</generateGitPropertiesFile>
        <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
        <format>json</format>
        <gitDescribe>
            <skip>false</skip>
            <always>false</always>
            <dirty>-dirty</dirty>
        </gitDescribe>
    </configuration>
</plugin>
运行Maven build命令构建。在目录  target / classes中,  git.properties文件添加了JSON格式的版本信息  。
{
    "git.branch" : "master",
    "git.build.host" : "LT0265",
    "git.build.time" : "2018-01-21T17:34:26+0100",
    "git.build.user.email" : "gunter@mydeveloperplanet.com",
    "git.build.user.name" : "Gunter Rotsaert",
    "git.build.version" : "1.0-SNAPSHOT",
    "git.closest.tag.commit.count" : "",
    "git.closest.tag.name" : "",
    "git.commit.id" : "6f592254e2e08d99a8145f1295d4ba3042310848",
    "git.commit.id.abbrev" : "6f59225",
    "git.commit.id.describe" : "6f59225-dirty",
    "git.commit.id.describe-short" : "6f59225-dirty",
    "git.commit.message.full" : "Created basic Spring Boot application with webservice for retrieving hard-coded version information",
    "git.commit.message.short" : "Created basic Spring Boot application with webservice for retrieving hard-coded version information",
    "git.commit.time" : "2018-01-21T17:33:13+0100",
    "git.commit.user.email" : "gunter@mydeveloperplanet.com",
    "git.commit.user.name" : "Gunter Rotsaert",
    "git.dirty" : "true",
    "git.remote.origin.url" : "https://github.com/mydeveloperplanet/mygitcommitidplanet.git",
    "git.tags" : ""
}
现在仔细看看  mygitcommitidplanet-1.0-SNAPSHOT.jar文件。在目录  BOOT-INF / classes中,文件  git.properties可用。在这一点上,我们已经拥有了我们想要的:版本信息包含在我们的可交付成果中。我们总是可以查看  JAR文件来查找源代码的确切版本信息。

四.将版本信息添加到REST风格的Web服务

下一步是将我们的REST风格的Web服务中的硬编码版本信息替换为git.properties文件的内容  。由于  git.properties文件已经是JSON格式,我们唯一要做的就是读取文件的内容并将其返回到我们的Web服务中。

我们的  VersionController.java 文件变成以下内容:

@RequestMapping(value = "/version", method = GET)
public String versionInformation() {
    return readGitProperties();
}
private String readGitProperties() {
    ClassLoader classLoader = getClass().getClassLoader();
    InputStream inputStream = classLoader.getResourceAsStream("git.properties");
    try {
        return readFromInputStream(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
        return "Version information could not be retrieved";
    }
}
private String readFromInputStream(InputStream inputStream)
throws IOException {
    StringBuilder resultStringBuilder = new StringBuilder();
    try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
        String line;
        while ((line = br.readLine()) != null) {
            resultStringBuilder.append(line).append("\n");
        }
    }
    return resultStringBuilder.toString();
}

使用Maven构建并运行应用程序。再次,转到URL http://localhost:8080/version。显示我们的git.properties文件的内容  。

这正是我们想要的:版本信息可以随时检索并始终保持最新。如果您有客户端应用程序,例如浏览器应用程序,则可以在关于部分中轻松使用此信息。

验证git属性

 当您想限制格式时,也可以将验证添加到  git.properties文件。如果构建不符合验证配置,构建将失败。我们现在要添加一个验证,以便在存储库变脏时让构建失败。

首先,我们在我们的pom.xml 文件的配置部分  添加一个  ValidationProperties部分  :

<validationProperties>
    <!-- verify that the current repository is not dirty -->
    <validationProperty>
        <name>validating git dirty</name>
        <value>${git.dirty}</value>
        <shouldMatchTo>false</shouldMatchTo>
    </validationProperty>
</validationProperties>
其次,我们必须激活验证。这是在 git-commit-id插件的执行部分完成的  :
<execution>
    <id>validate-the-git-infos</id>
    <goals>
        <goal>validateRevision</goal>
    </goals>
    <phase>package</phase>
</execution>
我没有提交我对pom.xml 文件所做的更改  ,所以我的存储库很脏。使用Maven构建应用程序。正如所料,构建失败并出现以下错误:
Validation 'validating git dirty' failed! Expected 'true' to match with 'false'!
如果我们现在将shouldMatchTo更改  为  true并再次运行构建,则构建会成功。

总结

Maven的git的承诺ID插件是必不可少的,应该被添加到每个Maven项目。它会在构建期间自动创建版本信息,以便在部署应用程序时验证版本信息。不再讨论在环境中运行软件的哪个版本。

地址:https://www.leftso.com/article/446.html

相关阅读

Spring Boot 2.0,Spring框架的Spring Boot 中的Spring Boot Actuator变化讲解。并且了解如何在Spring Boot 2.0中使用Actuator...
Spring Boot 2.0 Redis整合,通过spring boot 2.0整合Redis作为spring缓存框架的实现。
spring boot 2.0 security 5.0 整合,实现自定义表单登录。spring boot 2.0框架使用。
Spring Boot 2.0 有哪些新特性_Spring Boot 2.0新功能,在本文中,我们将探讨为Spring Boot 2.0计划的一些更改和功能。我们还会描述这些变化如何帮助我们提高...
Spring Boot 2.0 入门 logoback配置实战教程,俗话说好马配好鞍。Spring Boot 框架从各方面给我们带来了开发效率。日志自然也不会落下。本文将讲解与Spring Bo...
Spring Boot 2.0 支持的Apache Camel 版本发布了_Apache Camel 2.22发布支持Spring Boot 2.0
Spring Boot 1.x升级到Spring Boot 2.0迁移指南
本文将介绍Spring Boot和HikariCP示例。HikariCP是快速,简单,可靠和生产就绪的JDBC连接池。在Spring Boot 2.0版本中,默认数据库池技术已从Tomcat P...