6

I would like to write out build informations to a property file. I have found Maven resource filtering plugin. And this is how my pom relevant part looks like:

<?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>

If start mvn clean package the build is success, but my build.properties file under src/main/resources will not contain the build informations.

My property file looks like this:

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

What am I doing wrong? Thank you!

solarenqu
  • 714
  • 2
  • 13
  • 40

1 Answers1

4

An extended comment/short answer:

You have to look at target/classes...not at src/main/resources!;)

...the files in src/main/resources remain 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?

Please, see also here: - Where to place and how to read configuration resource files in servlet based application?

..with "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");

..with (e.g.) Spring:

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

But, since you tagged (, 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?

Humm, no clue, so far, ... for me it works as expected, with generated:

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

..and the (edited) Properties.load() code.

(Maybe you "overwrite" timestamp property ... and it sounds not like a very good (individual) property name (cause any1/thing (can) refer(s) to "timestamp", better something like com.my.company.myVerySpecialTimestamp !?;) And: A look at target/classes/build.properties tells you what messed up:

  • the maven resources filtering
  • or the property loading (in your code)

xerx593
  • 5,580
  • 4
  • 23
  • 43
  • 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? – solarenqu Feb 05 '19 at 10:14
  • 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? – solarenqu Feb 05 '19 at 10:37