Maven资源过滤

时间:2019-02-05 10:03:10

标签: maven java-ee

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

我在做什么错?谢谢!

1 个答案:

答案 0 :(得分:3)

更多评论/简短答案:

您必须在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(例如)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 您有任何想法吗? < / p>

嗯,到目前为止,还没有头绪,...对我来说,它按预期运行,并生成了:

build.date=2019-02-05 10:55

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

(也许您“覆盖” timestamp属性...,听起来好像不是一个很好的(个体)属性名称(因为any1 / thing可以引用“时间戳记”,这是更好的选择)像com.my.company.myVerySpecialTimestamp!?;) 并且:看看target/classes/build.properties会告诉您什么搞砸了:

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