Reputation: 2389
I'm having hard time to understand maven build profiles.
Basically my structure of profile directory is this
src/main/resources/profiles/dev/database.properties
src/main/resources/profiles/test/database.properties
I have this configuration in my pom.xml
<build>
...
<filters>
<filter>src/main/resources/profiles/${build.profile.id}/database.properties</filter>
</filters>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources/</directory>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>dev</build.profile.id>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<build.profile.id>test</build.profile.id>
</properties>
</profile>
</profiles>
Then in my java based datasource configuration:
@Configuration
@PropertySource("classpath:profiles/${spring.profiles.active}/database.properties")
When I run my application I put the VM argument -Dspring.profiles.active=test
so it will load the database.properties
under profiles/test. Then suddenly I just realized that I can do this without the configuration I've added in pom.xml. Is this still correct implementation of build profiles even though I don't need the configuration in my pom.xml?
Update: Here's my resolution Now I understand the use of maven profile, what I've done is to create another layer which will hold the configuration based on the active profile. Ben is right, I'm mixing spring profile with maven profile.
I've created another file database.properties under resources with this content:
db.driver=${db.driver}
db.url=${db.url}
db.username=${db.username}
db.password=${db.password}
Now my Datasource configuration is only referring to that new layer without knowing which profile is activated since maven will resolve this during compile time.
@Configuration
@PropertySource("classpath:database.properties")
Upvotes: 0
Views: 2334
Reputation: 1892
It depends on what your goal is, it appears you're conflating Maven Profiles with Spring Profiles which are different concepts. Maven Profiles are used to cause different behavior at BUILD time, vs. Spring Profiles which are used to cause different behavior at RUN time. If you're simply looking to load a different profile based on which profile is running there are a few different possibilities. If you are using spring boot all you need is to have an application-<profile-name>.properties
file in src/main/resources
- one for each profile. If you are using just Spring MVC you could have two @Configuration classes
, one for each @Profile
and in each configure the @PropertiesSource
with the appropriate database.properties
file for the active profile. Or, you can leave it as is and have the file loaded via the directory path specified in the SpEL. In any case you should not need to do anything to the Maven profile as these are all simply runtime concerns and everything under src/main/resources will by default be included in the generated package (war, ear, executable jar, etc.).
Upvotes: 3