I am working on a spring-boot application. I have two profiles inside my POM, but when I am trying the build the project using clean install -Pdev its not reflecting the change in application.properties, it'll only reflect when I am using 'activeByDefault' tag in one of the profile.
<profiles>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
</profile>
<profile>
<id>release</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<activatedProperties>release</activatedProperties>
</properties>
</profile>
</profiles>
If I am running clean install -Pdev I am getting this in my application.properties.
activatedProperties=#activatedProperties#
If I am setting the <activeByDefault>true</activeByDefault> then I'll get the value inside application.properties.
activatedProperties=release.
The frustration is I am not able to use other profiles.
Add in your application.properties
spring.profiles.active=#activatedProperties#
If Maven doesn't find the directory which contains your application.properties file in runtime, you need to setup the Maven Resources Plugin to filter the directory.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
Or just add what profile you want to active in application.properties
spring.profiles.active=dev
NOTE: The Maven profile and the Spring profile are different things.
Related
I have two projects which are built from one maven multiproject, project A and project B. Project B recently came to live and differ only little from project A, namely it uses a couple of different configuration files. My idea was to create a profile for projectB and add additional configuration files / replace existing configuration files. I've tried several approaches on stackoverflow including copy-rename plugin and mvn dependency plugin. Part of the problem seems to be that both projects have a file with same config name.
My current/last approach was to use maven-resources-plugin in following way:
I created an additional directory: src/main/projectbresources and a projectbprofile:
<profile>
<id>projectb</id>
<activation><activeByDefault>false</activeByDefault></activation>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>src/main/projectbresources</directory>
<filtering>true</filtering>
</resource>
</resources>
<overwrite>true</overwrite>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
I have a file with same name in both src/main/resources and src/main/projectbresources however, no matter the profile I build with, always the file from src/main/resources is in the target folder. Additional files that are in projectbresources only are copied to target.
Based on your statement "Project B recently came to live and differ only little from project A, namely it uses a couple of different configuration files." I would say you don't need 2 separate projects. You can have only one project and based on activated profile choose different configurations. With this approach the configuration files for both cases can have same name but different content.
Your project structure will look like (as an example):
project root folder:
pom.xml
- src
-- main
--- resources-profileA
--- resources-profileB
Now, the pom.xml contains 2 profiles: profileA and profileB. Each profile define it's own build/resources.
<profiles>
<profile>
<id>projectA</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources-profileA</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<id>projectB</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources-profileB</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
More on that, you can make one profile active by default. This will help your dev environment to automatically use one. If you like to have a default active profile for that profile you can name the resource folder just resources (default maven name) and keep different name for second profile.
UPDATE:
To go 'easy' and only add some additional files, you can use for profile projectA the maven defaults and define onle a profile for projectB.
The project root folder become:
pom.xml
- src
-- main
--- resources
--- resources-profileB
and the pom.xml will contains only one profile inactive by default:
<profiles>
<profile>
<id>projectB</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources-profileB</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
How to build for the two profiles:
Build for no profile is equivalent of building for projectA:
mvn clean install
Buid for projectB profile will ignore resources in default folder and will use only resources defined in profile (src/main/resources-profileB).
mvn clean install -P projectB
In my pom.xml file, I have multiple profiles set up. I would like to use the current profile's values in my application.conf file. The Ninja Framework documentation only mentions mode configurations, however I can't find anything concerning profile configurations.
An example: The documentation mentions
database.name=database_production # will be used when no mode is set (or prod)
%prod.database.name=database_prod # will be used when running in prod mode
%dev.database.name=database_dev # will be used when running in dev mode
%test.database.name=database_test # will be used when running in test mode
How would I be able to set different database names depending on the currently used profile?
You could add whatever it is you want replaced as a property to your profile definition, and then enable maven resource filtering like this:
<build>
..
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
..
</build>
<profiles>
<profile>
<id>developmentProfile</id>
<properties>
<dbName>database.name=database_dev</dbName>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>productionProfile</id>
<properties>
<dbName>database.name=database_prod</dbName>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
Be careful, if you use spring-boot, you should reference the properties in the application.properties file like this for it to work:
someOtherProp = something
#dbName#
After that, building the app should filter the property correctly:
mvn clean install -PdevelopmentProfile
Result:
someOtherProp=something
database.name=database_dev
Little stuck here. I have a pom with 3 profiles. Theese profiles have different version name. I want to inject that version name into properties file when a specific profile is building.
My profiles:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<projectVersion>DEV</projectVersion>
</properties>
</profile>
<profile>
<id>test</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<projectVersion>1.0.0-RC1</projectVersion>
</properties>
</profile>
<profile>
<id>prod</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<projectVersion>1.0.0-Final</projectVersion>
</properties>
</profile>
</profiles>
and filter.properties looks like this:
projectName = defaultName
versionName = defaultVersion
How to do that? Im building project by command:
mvn clean install -D profile_name
What you need to do is to add a new section to your <build> section of your POM file.
Like this:
<build>
<resources>
<resource>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
This will look inside the specified folder (src/main/resources) on the specified files **/*.properties and change the files when it encounters defined variables.
So in order to this work your propertie file must be this way:
projectName = ${defaultName}
versionName = ${defaultVersion}
Be aware with these variables name. Maven will replace it with the defined names by you or the names of the Maven structure like ${projectVersion} will be replaced by the <version>1.0</version> tag of your pom file.
So instead of using:
<properties>
<projectVersion>1.0.0-Final</projectVersion>
</properties>
Change the name (and the version) of this variable to something else like:
<properties>
<defaultVersion>1.0.0-Final</defaultVersion>
<defaultName>someName</defaultName>
</properties>
On all your profiles.
And just run your maven command as:
mvn install -Pprofilename
Be careful with the profiles you shown. All of them are active by default and this is a problem because they all define the same maven property. Instead, you should mark only one as active by default.
You also don't show <resources> filtering to process filter.properties, so this can be a mistake, as well.
And a final though, you are controlling artifact version on maven profiles. I don't think it is a good idea. Please read about maven-release-plugin.
we have a project which initially was developed to run on a linux platform, we want to customize it and use it under windows platform. It is hosted on github. Any advice on how to manage files that are shared between the two platforms but have some specificity for each one. for example configuration files that contains paths, environment variables and the like.
thanks for suggestion.
Since you have tagged maven here is the solution, use profiles to differentiate between linux and windows.
<profiles>
<profile>
<id>linux</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<envName>linux</envName>
</properties>
</profile>
<profile>
<id>windows</id>
<properties>
<envName>windows</envName>
</properties>
</profile>
</profiles>
<build>
<outputDirectory>${project.basedir}/target/classes</outputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.class</include>
</includes>
</resource>
<resource>
<targetPath>${project.basedir}/target/classes/configurations</targetPath>
<directory>${basedir}/src/main/resources/configurations/${envName}</directory>
</resource>
</resources>
<plugins>
</build>
Say suppose you have some configuration files specific to OS. and when you want to build based on the enivronment you want the file to copied to configurations directory inside class, the above code snippet copies that. For that to work you need to create 2 directories inside src/main/resoruces/configurations/linux and src/main/resources/configurations/windows. when you run maven you need to use profile like mvn package -Plinux or mvn package -Pwindows.
I got maven project with pom.xml in which I replace placeholder in one of configuration files according to profile as follows:
<properties>
<mq-server.host>127.0.0.1</mq-server.host>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources/config</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>staging</id>
<properties>
<mq-server.host>local.staging.com</mq-server.host>
</properties>
</profile>
</profiles>
When I build project using maven all doing fine on the other hand my project is Web project that is synchronizing with local tomcat via eclipse plugin and when I'm running project on tomcat using "play" button in eclipse, placeholder is not being changed. I wonder how I can fix it without adding special file just for developer.