Issue
I cannot find anything that explains how to access spring profile specific application properties inside of pom.xml
. Is this possible? I find lots of info about doing the opposite (accessing Maven profile properties in the application) but nothing on this.
My specific use case is being able to set the Tomcat manager URL in the Tomcat plugin from a properties file like this below. Please keep in mind that my question is only what is in the title and that I'm not asking for help configuring the Tomcat plugin so I will not include it in the question tags.
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>${tomcat.manager.url}</url>
<server>${tomcat.server.name}</server>
</configuration>
</plugin>
I have these profiles in my POM:
<profiles>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<activatedProperties>test</activatedProperties>
</properties>
</profile>
</profiles>
And this in the same POM's build element:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
In my application.properties
:
# active profile placeholder for the active maven profile. Defaults to dev.
spring.profiles.active=@activatedProperties@
and in my application-test.properties
:
tomcat.manager.url=http://mydomain:8080/manager/text
tomcat.server.name=myserver
but when I run_
mvn tomcat7:redeploy -DskipTests -P test
it does not try to deploy to http://mydomain:8080/manager/text
, but uses the default of localhost:8080
.
Solution
See the read-project-properties
goal of the Properties Maven Plugin:
The read-project-properties goal reads property files and URLs and stores the properties as project properties.
So, add the following to your POM:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${project.basedir}/src/main/resources/application-test.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Answered By - Gerold Broser