Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven resources filtering

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!

like image 623
solarenqu Avatar asked Feb 05 '19 10:02

solarenqu


People also ask

What is resource filtering in Maven?

Resource Filtering. You can use Maven to perform variable replacement on project resources. When resource filtering is activated, Maven will scan resources for property references surrounded by ${ and }.

What is Maven clean plugin?

The Maven Clean Plugin, as the name implies, attempts to clean the files and directories generated by Maven during its build. While there are plugins that generate additional files, the Clean Plugin assumes that these files are generated inside the target directory.

What is resource tag in POM XML?

The reference for the maven project descriptor (version 4.0) states that "this element describes all of the classpath resources associated with a project or unit tests" (link).

What is Maven Antrun plugin?

This plugin provides the ability to run Ant tasks from within Maven. You can even embed your Ant scripts in the POM! It is not the intention of this plugin to provide a means of polluting the POM, so it's encouraged to move all your Ant tasks to a build.


1 Answers

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 java-ee(, 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)

  • https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
  • https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html
  • ... https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html
like image 169
xerx593 Avatar answered Oct 11 '22 14:10

xerx593