Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get maven properties at build time

Tags:

java

spring

maven

I'm trying to get some properties from the pom.xml at build time based on the documentation.

My goal is to inject them in my Spring boot application.properties but the properties are not replaced.

Here's my code

pom.xml

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>xx</groupId>
    <artifactId>xx</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>xx</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <type>jar</type>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
    </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-tomcat</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jetty</artifactId>
            </dependency>
</dependencies>

<build>
        <finalName>xx</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins> 
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>static</directory>
                <targetPath>static</targetPath>
            </resource>
        </resources> 
</build>

src\main\resources\application.properties

application.version=${project.version}

Am I missing something? Is this supposed to work?

like image 853
Marc Avatar asked May 27 '16 18:05

Marc


People also ask

How do you get POM properties at runtime?

Get the pom properties using the properties plugin This will write all properties defined in the properties section of the pom into the version. properties file. But this has its downside. If you want a property to appear in the file, you will have to define it in the properties section.

What is Maven build timestamp?

maven.build.timestamp. the UTC timestamp of build start, in yyyy-MM-dd'T'HH:mm:ss'Z' default format, which can be overridden with maven.build.timestamp.format POM property.

What is properties in POM xml?

Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example: In your case you have defined properties as version of java.

What is goal and profile in Maven build?

A Build profile is a set of configuration values, which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such as Production v/s Development environments.


1 Answers

If you are using the spring boot parent pom the default placeholders have been re-configured to not conflict with the ${} syntax of spring itself.

To replace the maven properties use @ - so @project.version@ instead of ${project.version}.

filtering of the application.properties file is enabled by default.

like image 119
wemu Avatar answered Oct 01 '22 07:10

wemu