Maven资源过滤

6

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

我做错了什么?谢谢!
1个回答

4

一段扩展的评论/简短回答:

你需要查看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");

..使用Spring(例如):

@PropertySource("classpath:/build.properties")
...
@Value("${build.date}")
String buildDate;
< p > < em >但是,既然您标记了,那么您应该有一种非常具体且“复杂”的方法来完成这个操作(将属性加载到应用程序中),所以我们应该询问! :) (参见: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属性...听起来不像一个很好的(个人)属性名称(因为任何人/事物都可以引用“timestamp”,最好使用类似com.my.company.myVerySpecialTimestamp的东西!?;) 并且:查看target/classes/build.properties可以告诉您出了什么问题:

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


哦,好的。抱歉,我在target/classes文件夹中找到了属性文件。但是我该如何从我的应用程序中读取这个属性文件呢? - solarenqu
谢谢,现在没问题了,但是构建时间戳不像这个格式:yyyy-MM-dd HH:mm,我的格式是这样的:1549362759203。你有什么想法吗? - solarenqu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接