【问题标题】:Maven resources filteringMaven资源过滤
【发布时间】:2019-02-05 10:03:10
【问题描述】:

我想将构建信息写入属性文件。我找到了 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>

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

我的属性文件如下所示:

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

我做错了什么?谢谢!

【问题讨论】:

    标签: maven jakarta-ee


    【解决方案1】:

    扩展评论/简短回答:

    你必须看看target/classes...不是src/main/resources!;)

    ...src/main/resources 中的文件保持未过滤/修改。


    哦,不错。抱歉,我可以在 target/classes 文件夹中找到属性文件。但是如何从我的应用程序中读取此属性文件?

    请参见此处:-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");
    

    ..with (e.g.) Spring:

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

    但是,既然你标记了(,你应该有一个非常具体和“复杂”的方法来做到这一点(将属性加载到应用程序中),所以我们应该问! :)(见: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
    

    ..以及(已编辑)Properties.load() 代码。

    (也许你“覆盖”timestamp 属性......这听起来不是一个很好的(个人)属性名称(因为任何 1/事物(可以)引用“时间戳”,更好的是像 @ 987654335@ !?;) 并且:看看target/classes/build.properties 就知道出了什么问题:

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

    【讨论】:

    • 哦,不错。抱歉,我可以在 target/classes 文件夹中找到属性文件。但是如何从我的应用程序中读取此属性文件?
    • 谢谢,现在可以了,但是构建时间戳不是这样的格式:yyyy-MM-dd HH:mm 我的格式是这样的:1549362759203 你知道吗?
    猜你喜欢
    • 2016-08-16
    • 2013-04-11
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多