Configure active profile in SpringBoot via Maven - java

I'm trying to set an active profile in Spring Boot application using Maven 3.
In my pom.xml I set default active profile and property spring.profiles.active to development:
<profiles>
<profile>
<id>development</id>
<properties>
<spring.profiles.active>development</spring.profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
but every time I run my application, I receive the following message in logs:
No active profile set, falling back to default profiles: default
and the SpringBoot profile is set to default (reads application.properties instead application-development.properties)
What else should I do to have my SpringBoot active profile set using Maven profile?
Any help highly appreciated.

The Maven profile and the Spring profile are two completely different things. Your pom.xml defines spring.profiles.active variable which is available in the build process, but not at runtime. That is why only the default profile is activated.
How to bind Maven profile with Spring?
You need to pass the build variable to your application so that it is available when it is started.
Define a placeholder in your application.properties:
spring.profiles.active=#spring.profiles.active#
The #spring.profiles.active# variable must match the declared property from the Maven profile.
Enable resource filtering in you pom.xml:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
…
</build>
When the build is executed, all files in the src/main/resources directory will be processed by Maven and the placeholder in your application.properties will be replaced with the variable you defined in your Maven profile.
For more details you can go to my post where I described this use case.

Or rather easily:
mvn spring-boot:run -Dspring-boot.run.profiles={profile_name}

There are multiple ways to set profiles for your springboot application.
You can add this in your property file:
spring.profiles.active=dev
Programmatic way:
SpringApplication.setAdditionalProfiles("dev");
Tests make it very easy to specify what profiles are active
#ActiveProfiles("dev")
In a Unix environment
export spring_profiles_active=dev
JVM System Parameter
-Dspring.profiles.active=dev
Example: Running a springboot jar file with profile.
java -jar -Dspring.profiles.active=dev application.jar

You can run using the following command. Here I want to run using spring profile local:
spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=local"

In development, activating a Spring Boot profile when a specific Maven profile is activate is straight. You should use the profiles property of the spring-boot-maven-plugin in the Maven profile such as :
<project>
<...>
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<profiles>
<profile>development</profile>
</profiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profiles>
</...>
</project>
You can run the following command to use both the Spring Boot and the Maven development profile :
mvn spring-boot:run -Pdevelopment
If you want to be able to map any Spring Boot profiles to a Maven profile with the same profile name, you could define a single Maven profile and enabling that as the presence of a Maven property is detected. This property would be the single thing that you need to specify as you run the mvn command.
The profile would look like :
<profile>
<id>spring-profile-active</id>
<activation>
<property>
<name>my.active.spring.profiles</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<profiles>
<profile>${my.active.spring.profiles}</profile>
</profiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
And you can run the following command to use both the Spring Boot and the Maven development profile :
mvn spring-boot:run -Dmy.active.spring.profiles=development
or :
mvn spring-boot:run -Dmy.active.spring.profiles=integration
or :
mvn spring-boot:run -Dmy.active.spring.profiles=production
And so for...
This kind of configuration makes sense as in the generic Maven profile you rely on the my.active.spring.profiles property that is passed to perform some tasks or value some things.
For example I use this way to configure a generic Maven profile that packages the application and build a docker image specific to the environment selected.

You should use the Spring Boot Maven Plugin:
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.1.RELEASE</version>
<configuration>
<profiles>
<profile>foo</profile>
<profile>bar</profile>
</profiles>
</configuration>
...
</plugin>
...
</plugins>
...
</build>
...
</project>

I would like to run an automation test in different environments.
So I add this to command maven command:
spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=productionEnv1"
Here is the link where I found the solution: [1]https://github.com/spring-projects/spring-boot/issues/1095

I wanted to clarify the excellent answer https://stackoverflow.com/a/42391322/13134499 from Daniel Olszewski if you want to use it just for tests.
How to bind Maven profile with Spring for tests?
As you did, define the variable in pom.xml
...
<properties>
<spring.profiles.active>development</spring.profiles.active>
</properties>
Define a placeholder in your application-test.properties in src/test/resources:
spring.profiles.active=#spring.profiles.active#
Enable resource filtering in you pom.xml:
<build>
...
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
<testResources>
</build>

file structure:
/src/main/resources
=>
application.properties:
spring.profiles.active:#spring.profiles.active#'
application-dev.properties
application-prod.properties
IDE-Eclipse:
Right click on the project=>Run As=>Run Configuration=>Arguments=>VM Arguments:-Dspring.profiles.active=dev
CMD:
mvn spring-boot:run -Dspring.profiles.active=dev
mvn clean install -Dspring.profiles.active=dev

Related

How to prevent running tests when specific spring profile is active?

I have a Spring Boot application build on Maven. I'm using Spring profiles to distinguish environment-specific configuration. I would like to prevent running tests when a specific Spring profile is active. Reason: I would like to prevent running tests with production properties (spring.profiles.active=prod). I would like to do this globally (maybe with some Maven plugin) instead of on each test separately.
Do you have any checked solutions for this?
<profiles>
<profile>
<id>prod</id>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
</profiles>
You can use #IfProfileValue annotation. But you have to add some values to your active profile and read it with mentioned annotation. You can read more here (section 3.4.3): https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#integration-testing
EDIT:
Another solution is to exclude all (or selected tests) tests in the Surefire plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>${exclude.tests}</exclude>
</excludes>
</configuration>
</plugin>
...
<profile>
<id>prod</id>
<properties>
<exclude.tests>**/*.*</exclude.tests>
</properties>
</profile>
And then when you run mvn clean test -Pprod all tests will be skipped

Maven: profile properties not be picked up when running test

In pom.xml, I define a property:
<profile>
<id>local</id>
<properties>
<build.profile.id>local</build.profile.id>
<serverBaseUrl>http://127.0.0.1:8080</serverBaseUrl>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
And the serverBaseUrl was referenced in file application-email.xml:
<bean id="MailService" class="someclass">
<property name="aurl" value="${serverBaseUrl}"/>
</bean>
I expect that, when running test, using IntelliJ IDEA or using Maven test, ${serverBaseUrl} can be pick up from pom.xml automatically. However, it does not work like what I expect.
When not running test, the thing works exactly what I expect.
What's the problem here? Does maven or IntelliJ IDEA won't pick up profile properties when running test by default? How can I pick up profile's properties when I running the test?
Currently, I have a workaround: Define serverBaseUrl=xxx in config.properties and the property is picked up. This is a little ugly what I want to avoid.
This is more like a shot in the dark, I could not test it.
Add the resources tag in the build section of your pom.xml:
<build>
.....
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
<includes>
<include>application-email.xml</include>
</includes>
</testResource>
</testResources>
....
</build>
I suppose your application-email.xml is inside src/test/resources folder.

Profile activation in appengine:deploy

I'm trying to activate profiles of an AppEngine application using the maven command like the following :
mvn appengine:deploy -Dspring.profiles.active=prod
But it is ignored.
Is it possible to activate profiles using maven ?
I succeeded by linking Maven Profiles to Spring Profiles. In the following I explain how I did :
1 - Create Maven Profiles:
In pom.xml I identified my maven profiles, and will link them later to spring profiles by storing them in "spring.profiles.to.activate" property :
<!-- PROFILES -->
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profiles.to.active>dev</spring.profiles.to.active>
</properties>
</profile>
<profile>
<id>uat</id>
<properties>
<spring.profiles.to.active>uat</spring.profiles.to.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profiles.to.active>prod</spring.profiles.to.active>
</properties>
</profile>
</profiles>
2 - Activate Maven filtering :
I activated filtering in folder ${basedir}/src/main/webapp, by adding maven-war-plugin to build.
This will allow us to resolve placeholders ${...} (In this particular case ${spring.profiles.to.activate}) in the mentioned folder.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resources>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
</resources>
</webResources>
</configuration>
</plugin>
3- Activate profile Spring
In appengine-web.xml declare the system property : "spring.profiles.active" as being the maven property ${spring.profiles.to.activate}
<appengine-web-app
xmlns="http://appengine.google.com/ns/1.0">
<version>1</version>
<threadsafe>true</threadsafe>
<runtime>java8</runtime>
<system-properties>
<property name="spring.profiles.active" value="${spring.profiles.to.active}" />
</system-properties>
</appengine-web-app>
4 - Deploy to Appengine
# Dev
mvn appengine:deploy -Pdev
# UAT
mvn appengine:deploy -Puat
#PROD
mvn appengine:deploy -Pprod
#dev profile, try adding space between -P and dev
mvn appengine:deploy -P dev
#uat profile, try adding space between -P and uat
mvn appengine:deploy -P qa
#prod profile, try adding space between -P and prod
mvn appengine:deploy -P prd

Maven wildfly:deploy -P profileX doesn't trigger the right profile

I have 2 profiles in Maven's settings.xml but I cannot trigger them by the -P option, always the second is used:
<profiles>
<profile>
<id>wilfly-local</id>
<activeByDefault>true</activeByDefault>
<properties>
<wildfly-hostname>127.0.0.1</wildfly-hostname>
<wildfly-port>zzzz</wildfly-port>
<wildfly-username>xxx</wildfly-username>
<wildfly-password>S3cret</wildfly-password>
</properties>
</profile>
<profile>
<id>wildfly-remote</id>
<activeByDefault>false</activeByDefault>
<properties>
<wildfly-hostname>192.168.xxx.yyy</wildfly-hostname>
<wildfly-port>zzzz</wildfly-port>
<wildfly-username>xxx2</wildfly-username>
<wildfly-password>S3cret</wildfly-password>
</properties>
</profile>
</profiles>
...
<activeProfiles>
<activeProfile>wildfly-local</activeProfile>
<activeProfile>wildfly-remote</activeProfile>
</activeProfiles>
My pom.xml:
...
<build>
<finalName>mvnweb</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.2.1.Final</version>
<configuration>
<hostname>${wildfly-hostname}</hostname>
<port>${wildfly-port}</port>
<username>${wildfly-username}</username>
<password>${wildfly-password}</password>
</configuration>
</plugin>
</plugins>
</build>
...
The problem is when I try mvn wildfly:deploy -P wildfly-local, Maven deploys the war file on the remote server.
When I change the order of profiles then the second one is used (I checked it with -X option when I ran it). Both profiles deploys the project properly.
The activeByDefault tag do nothing, if I comment it out the result remains the same.
Can somebody tell me what should I do to get -P trigger work properly?
Thanks in advance!
From documentation:
The final piece of the settings.xml puzzle is the activeProfiles element. This contains a set of activeProfile elements, which each have a value of a profile id. Any profile id defined as an activeProfile will be active, reguardless of any environment settings. If no matching profile is found nothing will happen. For example, if env-test is an activeProfile, a profile in a pom.xml (or profile.xml with a corrosponding id will be active. If no such profile is found then execution will continue as normal.
Since you have both your profiles specified in your settings.xml they are activated both. Second one probably overwrites properties.
You should remove your activeProfiles from settings.xml if you want enable then only by -P CLI argument
<activeProfiles>
<activeProfile>wildfly-local</activeProfile>
<activeProfile>wildfly-remote</activeProfile>
</activeProfiles>

Maven: profile-based properties used in plugins section

I would like to mention I am relatively new in Maven configurations.
My situation:
I use Maven 3.0.5 to build J2E application
the application is deployed in four different environments: local, dev, test and prod
I use maven profiles to configure environment-specific configurations
I have defined these configurations in properties files in the file system.
This is the file system for those:
<my-project-root>
---profiles
------local
---------app.properties
------dev
---------app.properties
------test
---------app.properties
I load the corresponding property file with the following logic in my pom.xml:
<profiles>
<profile>
<id>local</id>
<!-- The development profile is active by default -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>local</build.profile.id>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<build.profile.id>dev</build.profile.id>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<build.profile.id>prod</build.profile.id>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<build.profile.id>test</build.profile.id>
</properties>
</profile>
</profiles>
<build>
<finalName>MyProject</finalName>
<plugins>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>profiles/${build.profile.id}</directory>
</resource>
</resources>
</build>
With this configuration I can use the respective properties for my current profile almost everywhere. Everywhere, but the <plugins> section. I would pretty much like to load e.g, my database url or credentials from such properties files, but if I include them in the app.properties they are not evaluated in the plugins section (e.g. I get value of ${endpoint} as database endpoint).
How do I get the properties loaded from files for the profile accessible in the <plugins> section?
PS: Yes, if I add those properties directly in the pom.xml as properties under <profiles> tag, they are accessible, but I would rather keep my passwords off the pom.
I was able to do what I wanted to do. I used properties-maven-plugin linked from, say this answer.
What I did was the following:
I added the properties-maven-plugin to read the files I needed loaded
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>profiles/${build.profile.id}/app.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
Regretfully, here I was not able to make the plugin read all property files in a directory, but I find this good enough.
I also needed to remove the error the plugin definition above gave for me in Eclipse (Plugin execution not covered by lifecycle configuration). To do thatI followed the instructions from the following post.
With those steps the properties I needed became available for the plugins, that used them.
Note: actually the properties get loaded after the compile maven command, but this is good enough for me, as all my property-dependant goals are to be executed after compile goal in sequence of goal calls in all my cases.

Categories

Resources