备忘:SpringBoot集成GradleGIT插件
步骤一:
Gradle:
增加配置如下:
repositories {
....
//增加 maven { url "https://plugins.gradle.org/m2/"}
}
dependencies {
....
//增加
classpath("gradle.plugin.com.gorylenko.gradle-git-properties:gradle-git-properties:2.0.0-beta1")
}
apply plugin: "com.gorylenko.gradle-git-properties"
步骤二:
1.增加启动监听:
@EnableApolloConfig@SpringBootApplication(exclude = {MybatisAutoConfiguration.class})
@EnableScheduling
public class MainApplication {
public static void main(String[] args) {
EnvConfig.initApollo();
SpringApplication springApplication = new SpringApplication(MainApplication.class);
springApplication.addListeners(new StartupErrorListener(), new StartupReadyListener());
springApplication.run(args);
}
}
2.在启动后增加配置文件读取:
/*** 增加容器启动事件监听方法
*
* @author hex
*/
public class StartupReadyListener implements ApplicationListener<ApplicationReadyEvent> {
private static final Logger logger = LoggerFactory.getLogger(StartupReadyListener.class);
/** {@inheritDoc} */
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
logger.info("应用启动完成!");
try {
Resource resource = new ClassPathResource("git.properties");
Properties properties = new Properties();
properties.load(new InputStreamReader(resource.getInputStream()));
logger.info("git.branch", properties.getProperty("git.branch"));
logger.info("git.commit.id:{}", properties.getProperty("git.commit.id"));
logger.info("git.commit.id.describe:{}", properties.getProperty("git.commit.id.describe"));
logger.info("git.commit.message.full:{}", properties.getProperty("git.commit.message.full"));
logger.info("git.commit.time:{}", properties.getProperty("git.commit.time"));
logger.info("git.closest.tag.name:{}", properties.getProperty("git.closest.tag.name"));
logger.info("git.closest.tag.commit.count:{}", properties.getProperty("git.closest.tag.commit.count"));
} catch (FileNotFoundException e) {
logger.error("配置文件不在类路径下!");
} catch (IOException e) {
logger.error("配置文件读取失败");
}
}
}
验证:
启动后回显如下信息:
2019-01-08 17:20:21.220 [main] INFO c.s.p.n.config.StartupReadyListener - 应用启动完成!
2019-01-08 17:20:21.221 [main] INFO c.s.p.n.config.StartupReadyListener - git.branch
2019-01-08 17:20:21.221 [main] INFO c.s.p.n.config.StartupReadyListener - git.commit.id:19ca6affcce012c89be19939acb29b90c71c58c8
2019-01-08 17:20:21.221 [main] INFO c.s.p.n.config.StartupReadyListener - git.commit.id.describe:T_pay-notify_1.0.1-38-g19ca6af-dirty
2019-01-08 17:20:21.221 [main] INFO c.s.p.n.config.StartupReadyListener - git.commit.message.full:修复GIT信息回显BUG!
2019-01-08 17:20:21.221 [main] INFO c.s.p.n.config.StartupReadyListener - git.commit.time:2019-01-08T17:21:32+0800
2019-01-08 17:20:21.221 [main] INFO c.s.p.n.config.StartupReadyListener - git.closest.tag.name:T_pay-notify_1.0.1
2019-01-08 17:20:21.221 [main] INFO c.s.p.n.config.StartupReadyListener - git.closest.tag.commit.count:38
以上是 备忘:SpringBoot集成GradleGIT插件 的全部内容, 来源链接: utcz.com/z/510549.html