Maven资源过滤

Maven resources filtering

我想将构建信息写入 属性 文件。我找到了 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 构建成功,但我 src/main/resources 下的 build.properties 文件将不包含构建信息。

我的 属性 文件如下所示:

build.version=${pom.version}
build.date=${timestamp}
build.url=${pom.url}
build.name=${pom.name}

我做错了什么?谢谢!

扩展的 comment/short 答案:

您必须查看 target/classes... 而不是 src/main/resources!;)

...src/main/resources 中的文件仍然是 un-filterd/touched。


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?

..与 "standard" 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;

但是,因为你标记了 (, you should have a very specific & "sophisticated" way to do this (load properties into application)), so we should ask! :) (see: 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() 代码。

(也许你 "overwrite" timestamp 属性 ...听起来不是一个很好的(个人)属性 名字(因为 any1/thing (可以)参考 "timestamp",更好的是 com.my.company.myVerySpecialTimestamp !?;) 并且:看一下 target/classes/build.properties 就知道出了什么问题:

  • maven资源过滤
  • 或 属性 加载(在您的代码中)