Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Maven properties in application.properties in Spring Boot

I'm trying to load properties from pom.xml into application.properties. I want to create two profiles: dev and prod to use different database urls. I'm using Jenkins as CI, in all my apps (Spring MVC mainly, without Boot project) are using maven profiles to deploy to Tomcat. There is any posibility to do this using maven properties? I tried something like that: spring.datasource.url=${jdbc.url}

like image 529
Jakub Pomykała Avatar asked Apr 18 '16 20:04

Jakub Pomykała


People also ask

How do I pass application properties in spring boot?

Spring Boot application converts the command line properties into Spring Boot Environment properties. Command line properties take precedence over the other property sources. By default, Spring Boot uses the 8080 port number to start the Tomcat. Let us learn how change the port number by using command line properties.

How do you specify file path in application properties in spring boot?

Spring Boot loads the application. properties file automatically from the project classpath. All you have to do is to create a new file under the src/main/resources directory.

How can you access a value defined in the application what is properties file in spring boot?

You can use @Value("${property-name}") from the application. properties if your class is annotated with @Configuration or @Component . You can make use of static method to get the value of the key passed as the parameter.

Can we change application properties in spring boot?

Spring boot provides command line configuration called spring.config.name using that we can change the name of application. properties. Here properties file name will be my-config.


1 Answers

Before you do it, consider externalizing the properties file out of your deployable package. This way you can deploy the same compilation on every environment. It will save your Jenkins some work that is actually unnecessary. The best practice is to build your application only once, however, if you are not convinced, here is how to do it.

  1. In your pom.xml define the profiles with appropriate values for the property.

    <profile>     <id>dev</id>    <properties>        <jdbc.url>your_dev_URL</jdbc.url>    </properties> </profile> 
  2. Setup the Maven Resources Plugin to filter the directory which contains your application.properties file.

    <build>     <resources>         <resource>             <directory>src/main/resources</directory>             <filtering>true</filtering>         </resource>     </resources>     ... </build> 
  3. If you use Spring Boot 1.3 or more, you should be aware of the fact that to avoid conflicts between Spring Boot placeholders and tokens filtered by the Maven Resources Plugin, the framework introduced a solution that requires using a different syntax for filtered values.

    Now, instead ${property.key} you should use @property.key@. In this case, your application.properties must contain the following sample to work as you expect:

    [email protected]@ 

You can also check out a post about separating Spring properties files for different Maven profiles. That way you will externalize the values from your pom.xml.

like image 68
Daniel Olszewski Avatar answered Sep 21 '22 08:09

Daniel Olszewski