我想写一个属性文件的构建信息.我找到了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>
Run Code Online (Sandbox Code Playgroud)
如果开始mvn clean package构建成功,但我build.properties在src/main/resources下的文件将不包含构建信息.
我的属性文件如下所示:
build.version=${pom.version}
build.date=${timestamp}
build.url=${pom.url}
build.name=${pom.name}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?谢谢!
扩展评论/简短回答:
你必须看target/classes...而不是src/main/resources!;)
...其中的文件src/main/resources保持未过滤/接触状态。
哦,好棒。抱歉,我可以在 target/classes 文件夹中找到属性文件。但是我如何从我的应用程序中读取这个属性文件呢?
请另请参阅此处: -在基于 servlet 的应用程序中在哪里放置以及如何读取配置资源文件?
..使用“标准”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");
Run Code Online (Sandbox Code Playgroud)
..与(例如)弹簧:
@PropertySource("classpath:/build.properties")
...
@Value("${build.date}")
String buildDate;
Run Code Online (Sandbox Code Playgroud)
但是,由于您标记了java-ee(您应该有一种非常具体且“复杂”的方法来执行此操作(将属性加载到应用程序中)),所以我们应该问!:)(参见:http ://www.adam-bien.com/roller/abien/entry/injecting_properties_into_java_ee )
谢谢,现在可以了,但是构建时间戳看起来不像这样的格式: yyyy-MM-dd HH:mm 我的格式是这样的: 1549362759203你有什么想法吗?
嗯,到目前为止,没有任何线索......对我来说,它按预期工作,生成:
build.date=2019-02-05 10:55
Run Code Online (Sandbox Code Playgroud)
..以及(编辑过的)Properties.load()代码。
(也许你“覆盖”timestamp属性......这听起来不像是一个非常好的(单独)属性名称(因为任何1/事物(可以)引用“时间戳”,更好的是像com.my.company.myVerySpecialTimestamp!?;)并且:A看看就会target/classes/build.properties告诉你出了什么问题:
| 归档时间: |
|
| 查看次数: |
880 次 |
| 最近记录: |