gpt4 book ai didi

Maven资源过滤

转载 作者:行者123 更新时间:2023-12-04 20:27:49 25 4
gpt4 key购买 nike

我想将构建信息写入属性文件。我找到了 Maven 资源过滤插件。这就是我的 pom 相关部分的样子:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>project.mygroup</groupId>
<artifactId>prject</artifactId>
<version>1.0-20190130-1</version>
</parent>

<artifactId>project-war</artifactId>
<packaging>war</packaging>
<version>1.0-20190130-1</version>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<timestamp>${maven.build.timestamp}</timestamp>
<maven.build.timestamp.format>
yyy-MM-dd HH:mm
</maven.build.timestamp.format>

</properties>

<dependencies>
...
</dependencies>

<build>
<finalName>project</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

<plugins>
<!-- Create war from the project -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>

</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>

</project>

如果开始 mvn clean package构建成功,但我的 build.properties src/main/resources 下的文件将不包含构建信息。

我的属性文件如下所示:
build.version=${pom.version}
build.date=${timestamp}
build.url=${pom.url}
build.name=${pom.name}

我究竟做错了什么?谢谢!

最佳答案

扩展评论/简短回答:

你要看看target/classes ...不在 src/main/resources !;)

... src/main/resources 中的文件保持未过滤/触摸。

Ohh, nice. Sorry, I can found the property file in the target/classes folder. But how can I read this property file from my application?



请参见此处:- Where to place and how to read configuration resource files in servlet based application?

..使用“标准”java:
// assuming src/main/resources/build.properties
Properties buildProps = new Properties();
buildProps.load(
//this is fail safe for most situations (test/web container/...), but *any classloader* could do it.
Thread.currentThread().getContextClassLoader()
.getResourceAsStream("build.properties")
);
String buildDate = buildProps.getProperty("build.date");

..与(例如) Spring :
@PropertySource("classpath:/build.properties")
...
@Value("${build.date}")
String buildDate;

但是,既然你标记了 (,你应该有一个非常具体和“复杂”的方式来做到这一点(将属性加载到应用程序中)),所以我们应该问! :)(见: http://www.adam-bien.com/roller/abien/entry/injecting_properties_into_java_ee)

Thank you, now it's ok, but the build time stamp not looks like this format: yyyy-MM-dd HH:mm I have format like this: 1549362759203 Do you have any idea?



嗯,没有线索,到目前为止,......对我来说它按预期工作,生成:
build.date=2019-02-05 10:55

..和(已编辑) Properties.load()代码。

(也许你“覆盖”了 timestamp 属性......这听起来不是一个很好的(个人)属性名称(因为 any1/thing(可以)指的是“时间戳”,更好的是像 com.my.company.myVerySpecialTimestamp ! ?;)
和:看看 target/classes/build.properties告诉你出了什么问题:
  • maven资源过滤
  • 或属性加载(在您的代码中)


  • https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
  • https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html
  • ... https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html
  • 关于Maven资源过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54531832/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com