java - 在基于 Gradle 的 Spring Rest 项目中向 Manifest 添加提交
问题描述
我想MANIFEST
在构建jar
.
上下文如下:
我有一个gradle
基于Spring-boot
依赖项的项目。这是一个REST
api项目。这是我的假设:我尝试过的所有插件都被依赖buildJar
项提供的任务覆盖。Spring
所以我的问题如下,
如何通过在项目中定义一个非常简单的 gradle 任务将提交哈希添加到清单中?
我已经知道如何使用以下任务打印最后一个哈希
task getHash {
def p1 = 'git rev-parse HEAD'.execute()
p1.waitFor()
println p1.text
}
这是build.gradle
详细信息:
buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.foo.bar'
version = '0.0.4-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// Spring dependencies
compile('org.springframework.boot:spring-boot-starter-web')
//Clickhouse-jdbc
compile group: 'ru.yandex.clickhouse', name: 'clickhouse-jdbc', version: '0.1.40'
// Swagger
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
// https://mvnrepository.com/artifact/org.json/json
compile group: 'org.json', name: 'json', version: '20180813'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
解决方案
Spring Boot 提供了一个bootJar
扩展,您可以使用它来配置 MANIFEST:
bootJar {
manifest {
attributes(
"GIT_REV": getHash()
)
}
}
您可以getHash()
在构建脚本中定义为一个简单的函数:
ext.getHash = {
def p1 = 'git rev-parse HEAD'.execute()
p1.waitFor()
return p1.text
}
注意:这个简单的示例不应按原样复制和粘贴:您应该在构建阶段getHash()
调用该方法,而不是在配置阶段。
推荐阅读
- regex - 我可以使用反向引用作为关联数组键吗?
- html - 从模板调用views.py中的方法
- sql - 比较不同数据类型的日期
- oracle - 在 Oracle 中,我想创建一个“路由接口”,它根据参数插入到单独的表中
- excel - 获得#价值!工作表上的错误,但 VBA 测试子返回它应该返回的内容
- amazon-web-services - 如何在 AWS sagemaker 中运行预训练模型?
- javascript - 如何检测用户当前使用的推送通知设备?
- react-native - 无法加载脚本。请确保您正在运行 Metro 服务器(运行“react-native start”)或您的捆绑包
- javascript - Ext JS 编码 base64 图像设置为背景 url
- amazon-web-services - 我可以配置我的 .ssh/config 文件以使用我的 aws pem 文件作为所有 ec2 连接的默认值吗