How does maven replace text #env# in application.yml? - maven

There is some profiles defined in pom.xml.
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
spring.profiles.active: #env# defined in application.yml and bootstrap.yml.
When I run mvn install -P test, text #env# in application.yml would be replaced by test.
How does it work?
Why it does't work for bootstrap.xml?

It works for application.yml because you are obviously using the Spring Boot Starter Parent. See the POM here: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-starters/spring-boot-starter-parent/pom.xml
The magic part is the <resources> configuration within that parent POM. You see that the application config files are explicitly copied with filtering. That is why the maven-resources-plugin resolves placeholders in these files.
If you want to add more files to be handled like this you can add your own <resources> section to your POM and extend it by more file patterns.

Related

Modify properties tag in POM.xml file from Jenkins

I have following properties in pom.xml file :-
<properties>
<cm_java_home>C:/Java/jdk1.8.0-102/bin</cm_java_home>
</properties>
But, I need to change this during building job from Jenkins, and it should look like as given below:-
<properties>
<cm_java_home>/home/abc/Java/jdk-1.8.0-102/jre</cm_java_home>
</properties>
How to modify my properties tag in pom.xml file using Jenkins job configuration ?
Simply, to modify any property of pom.xml file, use -D=value.
For instance, in my case,
I used the following:-
mvn clean install -Dcm_java_home="/home/abc/Java/jdk1.8.0_102/jre"

Could i use maven profile to swith between different value application.properties

I have application-prod.yml application-dev.yml, and application.properties which containing just one line code like below
spring.profiles.active=dev
for maven production build, it should use spring.profiles.active=prod , then it will build with application-prod.yml, for development build, it should use spring.profiles.active=dev, then maven
will use application-dev.yml to build
could I use pom.xml's different profile to do switch for this value switch in applicaiton.properties?
You can use a Maven property for this, reference it in your yml file (with ${...}) and filter the resource (i.e. the yml file) with the maven resources plugin.
It seems that what you're after is "externalized configuration". According to the excellent 12factor guidelines, it is best not to keep such config inside your code-repository.
Refer to the relevant section in the Spring Boot manual to see which options you have (and there are many). What it comes down to is that you provide your application.yml/properties file on the filesystem and your application will read it from there, rather than from the classpath.
Also, note that spring-profiles are not meant to be used to distinguish between development environments, but rather to put the application in different functional modes (e.g. to enable or disable specific features).
If you want the content of your properties file changed at build time, then you can use Maven filtering. Maven filtering allows to replace a placeholder in your properties (or yaml) file by values from Maven properties.
Assuming you have a property in your POM called targetEnv, which might have either the value dev or prod (depending on the active Maven profile), then you can refer it in your properties file (or yaml file) by using the following syntax :
spring.profiles.active=#targetEnv#
However, if you want to follow Spring Boot recommandations, it is better to enable and disable the Spring profiles by the means of environment variables in your target environment. For instance, you can use an environment variable spring.profiles.active with the desired value and it will override the value in your properties file.
You need to define a custom property in each of your Maven profiles and set their values to match with suffixes of corresponding properties files that you want to load with a particular profile.
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>release</id>
<properties>
<activatedProperties>release</activatedProperties>
</properties>
</profile>
Next, in the build section of the same file, configure filtering for the Resources Plugin. That will allow you to insert properties defined in the previous step into any file in the resources directory, which is the subsequent step.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
…
</build>
Finally, add the following line to the application.properties.
spring.profiles.active=#activatedProperties#
For more details, please see spring boot properties per maven profile
For official guide to load from external configLoad from external Config

log4j.properties in my /src/test/resources is not overriding main

How do I get my src/test/resources/log4j.properties file to override my /src/main/resources/log4j.properties during testing? The first ends up under target/test-classes, and the second is under target/classes.
Yes, I've looked for duplicates, but in this case, I don't have a mix of loggers - I'm using
only slf4j-log4j12.
You could use maven profiles combined with filtering and maven-war-plugin, all in pom.xml. With this you can define what you need to have in log4j.properties.
Example with logback:
Defining a variable in config.test.properties:
basepath_log=/var/log/ProjectName
Defining a placeholder in logback.xml (equivalent to log4j.properties):
<appender>
...
<file>${basepath_log}/ProjectName.log</file>
...
</appender>
Profile definition:
<profiles>
<profile>
<id>test</id>
<properties>
<targetenv>test</targetenv>
</properties>
</profile>
</profiles>
Filtering (pom.xml):
<filters>
<filter>src/main/resources/filter/config.${targetenv}.properties</filter>
</filters>
Then you could use profiles directly with maven using option -Ptest or selecting it in your IDE (Netbeans, Eclipse...)
Sorry, my bad. This bug suggested maybe my issue was a dependency on a bad version. Sure enough - 2.3 was too old; updated version behaves as expected.

read Maven variable from properties file using profile

I want to read a maven variable for configure a build plugin from a properties file. It's not needed in and further project files e.g. context files.
1) made a profile (it works, can use mvn ... -P private)
<profile>
<id>private</id>
<properties>
<env>private</env>
</properties>
</profile>
2) created the filter file with this content (it works)
foo.path=/home/foo/path
3) try to configure the plugin (does not work)
<build>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>foo-plugin</artifactId>
<version>${foo-plugin.version}</version>
<configuration>
<!--<fooPath>home/foo/path></fooPath> that works -->
<fooPath>${foo.path}</fooPath> <!--works not -->
</configuration>
...
</build>
Thx a lot
The name of your property is 'env' but you don't use env anywhere in your configuration.
When Maven docs mention "filter files" they usually mean a file used when processing resources (i.e. copying resources from /src/main/resources to target/classes). As far as I know the properties in those files aren't used for plugin configuration out-of-the-box. I have used the Codehaus properties-maven-plugin:read-project-properties goal do do what you are attempting. Make sure you bind the goal to the lifecycle before any plugins that need the properties for config.
Also, see this answer; you may load properties used to configure other plugins, but not to configure core Maven project elements.

I can't get maven to use properties defined in ~/.m2/settings.xml?

I am using ~/.m2/settings.xml to store a number of property names used throughout the pom.xml files in my project. If I make the XML invalid (by adding another < for example), maven immediately generates an error, saying that it cannot parse that file. If I leave the XML valid, settings in my appBeans.xml file do not pick of references to properties defined in settings.xml.
Has anyone experienced this problem? I am sort of at my wits end here.
Reflecting properties from Maven configurations works by resources filtering.
Make sure your settings.xml, project pom and the target xml file contain correct configurations and reside in correct places.
If I understood correctly, you want to store a property name and value in the settings.xml so the props can be used in your project files. I'll provide a working example:
Define a default profile and properties in settings.xml:
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<someProp>Value</someProp>
</properties>
</profile>
</profiles>
Define resource folder's filtering=true in pom.xml:
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
Define the property in the xml file (that resides in the src/main/resources folder):
...
<element>${someProp}</element>
...
After this you should see the filtered result e.g. in target/classes/appBeans.xml.
Bear in mind that if you're using Eclipse & m2eclipse or similar plugin, it probably won't start using the updated settings.xml without restarting Eclipse and it's automatic build will sometimes overwrite your files in the target folder. I'm talking from experience here :)
Maven properties do not get reflected in miscellaneous XML files.
If you add one of these properties to the <properties/> element of the specific pom that runs the specific plugin that reads allBeans.xml, does that work? I believe that it will not, and your problem will turn out to be adding to the <configuration/> for the plugin to pass the maven properties to it.
If you edit your question to show the plugin that processes appBeans.xml I can make this more specific.

Resources