Spring Boot Actuator doesn't read git.properties - java

I have a simple project with Spring Actuator, also i have a maven plugin generating git.properties (resides in classes directory).
However when i run my app, /actuator/info request shows:
{
"git": {}
}
Documentation says autoconfigure should pick up properties automatically https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-application-info-git

My mistake, git.properties was not a 'properties' file in fact.
I didn't see {} around a file content.
after changing configuration/format to properties in git-commit-id-plugin plugin, it works
(plugin was generating git info in json)

Related

#Value inside class file in third party jar spring boot

I am working on a migrating an old Spring XML config-based app to Spring Boot and there are third party jars which have #Value("$ properties referenced in them. I tried loading a custom property file placed under /resources in the new Spring Boot workspace and loading it with #PropertySource("classpath:file") but during the Spring Boot run the property does not seem to be loaded and get this below error:
Could not resolve placeholder 'com.example.propertyName' in value "${com.example.propertyName}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178)
~[spring-core-5.2.2.RELEASE.jar:5.2.2.RELEASE]
You can bind the properties values using annotation #Value then mentioned the key on it.
you can follow the below link for end to end example
click

How to run a java task automatically from an external jar?

I want to generate a java jar which when included on the classpath of another project will launch a periodic task that does something in the background.
This is very similar to eureka client. You include the dependency and add an annotation after which a service is started automatically to poll eureka server.
How can I do that?
Edit: I got it to work using maven, following the example provided in the comments
github.com/shauank/spring-boot/tree/master/client (client which is having taskexecutor)
github.com/shauank/spring-boot/tree/master/application (Application which uses jar created in step1)
You can use concept of Autoconfiguration. Same is used by Eureka and Config server.
Under src/main/resource create spring.factories and add following entry
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
location.to.your.executor
Your class,
pacakage location.to.your.executor
class MyExecutor{
public MyExecutor(){
//Your code for task executor
}
}
Now, above code can be build as jar and included into another spring boot project.
So, when you run another jar, spring boot will look for auto configuration onto spring.factories class and load classes defined into it.

Set active Spring Boot profile for Tomcat Webapp internally (via application.properties)

I'd like to use two different spring profiles proda and prodb using Spring Boot 2.0.0 (setting the profile in the application.properties).
In Spring Boot, you can also set the active profile in application.properties, as shown in the following example:
spring.profiles.active=production
Source: 74.6
For now I'm only trying to get proda to work. I've got three different property files:
application-proda.yml
database:
conn:
blablabla: blablaqwer
and
application-prodb.yml
database:
conn:
blablabla: albalbrewq
and also
application.properties
spring.profiles.active=proda
When running the application in the IDE, or packaging it as jar with maven, everything works as expected (active profiles [proda] are set, application-proda.yml is loaded). Calling this in (for example a #PostConstruct of) some class:
System.out.println(Arrays.toString(env.getActiveProfiles()));
will result in
[proda]
but when buildung as war with maven, and deploying it to a Tomcat server, the same code will result in
[]
and application-proda.yml is not loaded. That means the application didn't read the application.properties and therefore didn't load the active profile proda for some reason.
But the finished war has all the needed files under WEB-INF\classes\.
I've seen some solutions where you can set -Dspring.profiles.active=proda as a command line parameter, or set the active profiles in the web.xml, but this is not what I need, as I don't have a web.xml and I'd like to use the Spring Boot feature and declare the profiles in the application.properties. It should work just like in the IDE or packaged as a jar with maven.

Jenkins Gradle test fails on Commons Configuration

I have a project that uses Apache Commons Configuration. The project is built using gradle. I have some unit test cases written on this project and gradle test works fine when run locally.
However when the unit tests are run in Jenkins it fails.
Please see the screenshot of the error. The error seems to be to do something with Commons Configuration that I am using. Please help.
org.apache.commons.configuration.ConfigurationRuntimeException: No ConfigurationProvider registered for tag disabledAdministrativeMonitors
org.apache.commons.configuration.ConfigurationException: org.apache.commons.configuration.ConfigurationRuntimeException: org.apache.commons.configuration.ConfigurationRuntimeException: No ConfigurationProvider registered for tag disabledAdministrativeMonitors
at org.apache.commons.configuration.DefaultConfigurationBuilder.createConfigurationAt(DefaultConfigurationBuilder.java:752) ~[commons-configuration-1.6.jar:1.6]
at org.apache.commons.configuration.DefaultConfigurationBuilder.initCombinedConfiguration(DefaultConfigurationBuilder.java:628) ~[commons-configuration-1.6.jar:1.6]
at org.apache.commons.configuration.DefaultConfigurationBuilder.getConfiguration(DefaultConfigurationBuilder.java:560) ~[commons-configuration-1.6.jar:1.6]
The ConfigurationProvider try to load a configuration xml file which is default the config.xml.
In my case the project is build using Jenkins. Jenkins provides a config.xml in Jenkins home dir. This is loaded first instead of my desired one. Maybe that applies for you too?
Example Jenkins config.xml
<?xml version=’1.1' encoding=’UTF-8'?>
<hudson>
<disabledAdministrativeMonitors>
...
</disabledAdministrativeMonitors>
...

Spring boot configuration in a multi-Module maven project

I'm having a problem properly setting up spring boot for my multi-module maven project.
There is a module "api" that uses another module "core". Api has an application.properties file that contains spring.mail.host=xxx. According to the spring boot documentation this provides you with a default implementation of the JavaMailSender interface, ready to be autowired.
However the class that is responsible for sending out the e-mails resides in the "core" package. When I try to build that module the build fails because no implementation of JavaMailSender can be found.
My guess then was that the mailing config should reside in "core" in a separate application.properties. I created that and moved the spring.mail.host property from the "api" to the "core" property file.
This time the core module builds successfully, but "api" fails to build because of the same exception, so I think I just moved the problem.
I don't understand the required structure for handling this type of situations well enough so I was wondering what the correct way is for having a "core" module containing all the correct configuration for sending mails and having other modules use the mailing code and config that resides in it.
I found the answer in another stack overflow question: How to add multiple application.properties files in spring-boot?
It turns out there can only be 1 application.properties file in the final jar that spring boot creates. To have multiple files you have to rename one of the files to something custom. I named the properties of the core module "core-application.properties".
Then in the API module I added this to the spring boot application class:
#SpringBootApplication
#PropertySource(value = {"core-application.properties", "application.properties"})
Doing this I can correctly use the base properties file and overwrite them in the more specific modules. Also you can still create profile-specific properties file (core-application-production.properties) with this setup, no need to add those to the propertysource manually). Note that #PropertySource does not work for yaml configuration files at this moment.
there is one effective application.properties per project. you just keep 2 properties file for a success build.
when api module use core module, the application.properties in core module is overwrite by api.
Your API's pom.xml must has dependency of CORE module.
the solution is to define properties files as a value of #PropertiesSource in Starter class.
but it is beter to put "classpath:" behind the properties files.
for example in Intellij idea after adding the "classpatch:" word berhind the files name, values become to link. like this:
#SpringBootApplication
#PropertySource(value = {"classpath:core-application.properties", "classpath:application.properties"})
I hope to helped you.

Categories

Resources