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}

- 13,995
- 6
- 58
- 65

- 2,082
- 3
- 27
- 58
3 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.
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>
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>
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:spring.datasource.url=@jdbc.url@
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.

- 13,995
- 6
- 58
- 65
-
2This doesn't work without using
tag. – Alessandro C Dec 04 '17 at 16:15 -
Maven profiles are not activated by default. You need to activate it on your own. You can trigger the profile with the `-P` flag when you call the build. You are not forced to use `activeByDefault`. – Daniel Olszewski Dec 05 '17 at 08:22
-
2Note that if you use the yaml format for the properties, make sure you surround the placeholder by single quotes: `url '@jdbc.url@'`. – Valentin Grégoire May 23 '19 at 09:39
-
do you know how to reverse thing? Accessing application properties in pom xml – c.sankhala Mar 22 '21 at 05:40
-
Note that test resources won't be filtered. – Pieter De Bie Jul 20 '21 at 12:47
-
The ```@``` prefix does not work when defined in an imported configuration file. Any way to make this work? – TheRealChx101 Mar 24 '22 at 12:44
In addition to Daniel Olszewski, in my yml file I got an error: (Do not use @ for indentation)
So i fixed it by adding single quotes. Someone might find it helpful.
spring:
datasource:
url: '@jdbc.url@'

- 1,003
- 2
- 12
- 19
Of course there is. Just use Maven Filtering over your application.properties file and Maven will write your profile specific values in the file.
However, you must understand that while Maven profiles work at application package/build time, Spring Boot ones do at runtime. In other words, with Maven profiles you'll get profile specific immutable builds, while when using the ones from Spring Boot you'll be able to change your application configuration every time before launching it or even while it's running.
See also:

- 30,971
- 16
- 136
- 217
-
You can see my answer here: http://stackoverflow.com/questions/36635163/spring-boot-externalizing-properties-not-working/36635367#36635367 for an example on how to externalise properties – Marco Tedone Apr 18 '16 at 22:40