spring 如何将时间戳属性从build.gradle传递到application.properties?

bxgwgixi  于 2023-08-02  发布在  Spring
关注(0)|答案(2)|浏览(128)

我在build.gradle中定义了一个timestamp值,如下所示

build.gradle

def timestamp = new Date().format('yyyyMMddHHmm')
println "Timestamp:" + timestamp

springBoot {
    mainClass = 'com.example.login.Application'
    buildInfo {
        properties {
            additionalProperties = [
                    'timestamp': "${timestamp}"
            ]
        }
    }
}
.
.
.

字符串
现在我的build/resources/main/META-INF中的build-info.properties看起来像这样,

build.artifact=login
build.group=com.example
build.name=login
build.timestamp=202307111234

应用.属性

current.timestamp=@timestamp@


我尝试从AppFilter.java类的build-info属性中检索build.time,如下所示

public class AppFilter {
    @Autowired
    private BuildProperties buildProperties;

    public AppFilter() {
    }

    public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
       String timestamp = buildProperties.get("build.timestamp");
       System.out.println("Timestamp=>"+timestamp);
       .
       .
       .
    }
}


有没有一种方法可以将timestamp属性从build.gradle传递到application.properties,以便当前的.timestamp将具有来自我的gradle构建的timestamp值?
得到以下错误,

调试后出错

String timestamp = buildProperties[NullPointerException]....


我是新手。任何帮助都是感激的。
谢了,谢了

smdncfj3

smdncfj31#

以下是如何使用自己的replace来执行此操作:

  • build.gradle.kts*
import org.apache.tools.ant.filters.ReplaceTokens

tasks.named<ProcessResources>("processResources") {
    filesMatching("application.properties") {
        filter(ReplaceTokens::class, "tokens" to mapOf("current.timestamp" to "abc"))
    }
}

字符串
我相信Groovy会是这样的:

  • build.gradle*
import org.apache.tools.ant.filters.ReplaceTokens

tasks.named("processResources", ProcessResources) {
    filesMatching("application.properties") {
        filter(ReplaceTokens, tokens: ["current.timestamp": "abc"])
    }
}


因此,我建议使用Sping Boot 构建信息:
https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#integrating-with-actuator.build-info
完成后,您可以注入BuildProperties bean以获取其值:
https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/info/BuildProperties.html

  • 工作示例:*
  • build.gradle.kts*
plugins {
    `java-library`
    id("org.springframework.boot") version "3.1.+"
    id("io.spring.dependency-management") version "1.1.0"
}

springBoot {
    buildInfo {
        properties {
            additional.set(mapOf("timestamp" to "Timestamp: ${Date()}"))
        }
    }
}

  • 测试 *
@SpringBootTest
public class BuildInfoTest {
    @Autowired
    BuildProperties buildProperties;

    @Test
    void foo() {
        System.out.println("====================================");
        System.out.println(buildProperties.getName());
        System.out.println(buildProperties.getTime());
        System.out.println(buildProperties.get("timestamp"));
        System.out.println("====================================");
    }
}

  • 输出:*
====================================
my-project
2023-07-11T19:55:18.473Z
Timestamp: Tue Jul 11 15:55:18 EDT 2023
====================================

t40tm48m

t40tm48m2#

将CopySpec更改为expand(timestamp: timestamp)并将属性文件更改为current.timestamp=$timestamp
传递给expand()的Map中的键应该是您要替换的令牌。
@timestamp@样式的令牌与ReplaceTokens过滤器一起使用,如果您愿意,也可以使用它。详情请参见https://docs.gradle.org/current/userguide/working_with_files.html#sec:filtering_files。

相关问题