我想在构建 jar
时将提交哈希添加到 MANIFEST
。
上下文如下:
我有一个基于 gradle
的项目,具有 Spring-boot
依赖项。它是一个 REST
api 项目。这是我的假设:我尝试过的所有插件都被 Spring
依赖项提供的 buildJar
任务覆盖。
所以我的问题如下,
我如何通过在项目中定义一个非常简单的 gradle 任务来将提交哈希添加到 list 中?
我已经知道如何使用以下任务打印最后一个散列
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()
方法,而不是在配置阶段。
关于java - 在基于 Gradle 的 Spring Rest 项目中添加对 Manifest 的提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52644515/