How to set build path configuration in spring boot webapp - spring

I am in new to spring boot. I have created a web app using spring boot. My application require some properties file to do the processing. In eclipse What I do i set the path on Run configuration like bellow.
Now When I run the application I gets the require file on path and run smoothly.
Now I want to deploy the war file on some server. How do i provide this path to my application.
Bellow is the project structure of my project. and files are here
highlighted
How do I set this file path using application.properties or any other way so that, I don't have to provide the path from run configuration, and the .war can be deploy on any server.
Update 1 : This what I tried.
Created a customStart.bat
content of the file is
set CATALINA_OPTS="-engine.home="/src/main/resources/" -Dlog4j.configuration=config/log4j.xml -Dlog4j.debug=true"
call startup.bat %CATALINA_OPTS%
But still that argument is not set. How do I do that?

As an option you can add your properties to %tomcat_home%\conf\catalina.properties
Just put them at the end of the file as follows:
log4j.configuration=config/log4j.xml
.....

From spring boot official documentation (This is a highlight of the concept of creating custom spring boot variable) bellow you'll find a link to a Q/A that describes the solution.
Spring Boot jars are shipped with meta-data files that provide details
of all supported configuration properties. The files are designed to
allow IDE developers to offer contextual help and “code completion” as
users are working with application.properties or application.yml
files.
The majority of the meta-data file is generated automatically at
compile time by processing all items annotated with
#ConfigurationProperties. However, it is possible to write part of the
meta-data manually for corner cases or more advanced use cases.
Configuration meta-data files are located inside jars under
META-INF/spring-configuration-metadata.json They use a simple JSON
format with items categorized under either “groups” or “properties”
and additional values hint categorized under "hints":
Here is an example of a meta data config file :
{"groups": [
{
"name": "server",
"type": "org.springframework.boot.autoconfigure.web.ServerProperties",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
},
{
"name": "spring.jpa.hibernate",
"type": "org.springframework.boot.autoconfigure.orm.jpa.JpaProperties$Hibernate",
"sourceType": "org.springframework.boot.autoconfigure.orm.jpa.JpaProperties",
"sourceMethod": "getHibernate()"
}
...
],"properties": [
{
"name": "server.port",
"type": "java.lang.Integer",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
},
{
"name": "spring.jpa.hibernate.ddl-auto",
"type": "java.lang.String",
"description": "DDL mode. This is actually a shortcut for the \"hibernate.hbm2ddl.auto\" property.",
"sourceType": "org.springframework.boot.autoconfigure.orm.jpa.JpaProperties$Hibernate"
}
...
],"hints": [
{
"name": "spring.jpa.hibernate.ddl-auto",
"values": [
{
"value": "none",
"description": "Disable DDL handling."
},
{
"value": "validate",
"description": "Validate the schema, make no changes to the database."
},
{
"value": "update",
"description": "Update the schema if necessary."
},
{
"value": "create",
"description": "Create the schema and destroy previous data."
},
{
"value": "create-drop",
"description": "Create and then destroy the schema at the end of the session."
}
]
}
]}
Each “property” is a configuration item that the user specifies with a
given value. For example server.port and server.servlet-path might be
specified in application.properties as follows:
server.port=9090 server.servlet-path=/home The “groups” are higher
level items that don’t themselves specify a value, but instead provide
a contextual grouping for properties. For example the server.port and
server.servlet-path properties are part of the server group.
Notes:
The groups section is note required
“hints” are additional information used to assist the user in configuring a given property. When configuring the spring.jpa.hibernate.ddl-auto property, a tool can use it to offer some auto-completion help for the none, validate, update, create and create-drop values.
You can easily generate your own configuration meta-data file from
items annotated with #ConfigurationProperties by using the
spring-boot-configuration-processor jar
You can check this Q/A
** for more details check the spring boot apendix section **

Related

Spring Integration - Unable to see metrics for MessageHandler, MessageChannel and MessageSource

I have a prometheus metrics registry and captor beans registered as seen below:
"prometheusMeterRegistry": {
"aliases": [],
"scope": "singleton",
"type": "io.micrometer.prometheus.PrometheusMeterRegistry",
"resource": "class path resource [org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.class]",
"dependencies": [
"prometheusConfig",
"collectorRegistry",
"micrometerClock"
]
},
"integrationMicrometerMetricsCaptor": {
"aliases": [],
"scope": "singleton",
"type": "org.springframework.integration.support.management.micrometer.MicrometerMetricsCaptor",
"resource": null,
"dependencies": []
},
However, when I check the actuator/prometheus endpoint, I don't see any metrics for MessageHandler, MessageChannel and MessageSource which are mentioned here. The only spring-integeration metrics available are:
spring_integration_sources 1.0
spring_integration_handlers 17.0
spring_integration_channels 15.0
I also ran data through my flows, still can't see the metrics. What am I missing?
Thank you for such a great sample!
So, your problem is here:
<!-- Enable Spring Integration Metrics -->
<int:management/>
First of all you must not do this since Spring Boot auto-configures this for us. (We probably need to improve Spring Boot docs to mention that feature).
Secondly you still can do this but you need to use this option:
<xsd:attribute name="default-counts-enabled" use="optional">
<xsd:annotation>
<xsd:documentation>
The default value for components that don't match 'counts-enabled-patterns'.
Defaults to false, or true when an Integration MBean Exporter is provided.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
and set it to true.
In the current Spring Integration 5.4 version it is deprecated already. We definitely are going to remove it altogether in the next 6.0.

spring cloud how to set encrypt.key property

I work with spring cloud and I want to set encrypt.key property.
My application has bootstrap.yml but I don't have bootstrap.properties.
How can I set encrypt.key?
I get
{
"description": "The encryption algorithm is not strong enough",
"status": "INVALID"
}
when I executed localhost:8888/encrypt
Simply put encrypt.key=keyName in you bootstrap.yml or bootstrap.properties file to avoid the error
{"description": "The encryption algorithm is not strong enough", "status": "INVALID" }
while running config server locally.

Viewing current Spring (Boot) properties

I run a Spring Boot application as a .jar file which partly takes its properties from application.yml residing inside the jar while the other part of properties is being provided from another application.yml residing outside the jar. Some of the properties from the outside overwrite the properties from the inside. In order to test whether the properties were overwritten properly I would like to see the currently active ones. Is that achieveable at all out of the box? Or is the only solution to extend my application by property output logic?
If you add Spring Boot Actuator to your dependencies, you can view a lot of configuration and other info at actuator endpoints. You can view properties at /configprops endpoint.
At least as of spring boot 2.0 actuator/env will return the list of all properties per propertySources in order of their precedence, ie. if a property is redefined in >1 sources then the 1st occurrence reading from the top is the one that is active.
For a single property actuator/env/<property-name> will return the effective value and in which source it's defined
{
"property": {
"source": "applicationConfig: [file:../application-tom.properties]",
"value": "DEBUG"
},
...
}
Note: I dont know if this would reflect any changes that might happen when programmatically modifying the spring context. But that is smth. one should not do anyhow.

How to store application.properites values using manifest.yml to contain passwords?

So my application.properties would look like:
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=mydb
spring.datasource.username=user
spring.datasource.password=123456
spring.jpa.database-platform=org.hibernate.dialect.SQLServer2012Dialect
I don't want others to be able to see my user and password when they go into my application.properties file.
Is there an alternative way to push values to cloud foundry? Something like manifest.yml?
Attempt to create manifest.yml
I tried to make a manifest file so I can bind it with my application on cloud foundry.
VCAP_SERVICES =
{
"oraclesql": [
{
"name": "OrcaleDb",
"label": "oraclesql",
"tags": [
"oracledb",
"oracle",
"relational"
],
"plan": "free",
"credentials": {
"uri": "jdbc:sqlserver://localhost:1433;databaseName=mydb",
"username": "user",
"password": "123456"
}
}
]
}
Created application.yml
//this works
spring:
application:
name: tester
datasource:
driverClassName: jdbc:sqlserver://localhost:1433;databaseName=mydb
username: user
password: 123456
initialize: false
jpa:
databasePlatform: org.hiberate.dialect.Oracle10gDialect
Yes and no. Unfortunately the manifest.yml doesn't work quite like that. The file provides the settings that will be used for cf push. It's application agnostic and doesn't know anything about Spring, Java, your programming language or your application framework of choice. Thus it cannot make changes to your application.properties file or other framework specific configuration.
Fortunately, there are many ways to configure Spring Boot (see here) and one of them is via environment variables. What's fortunate about this is that you can set environment variables via manifest.yml. Thus if you set the proper environment variables in manifest.yml, you can configure your application.
For reference, here are instructions for setting env variables in manifest.yml.
https://docs.cloudfoundry.org/devguide/deploy-apps/manifest.html#env-block
It looks roughly like this:
---
...
env:
ENV_1: val_1
ENV_2: val_2
Spring Boot will map environment variables to properties using the rules explained here. A basic example would be SPRING_DATASOURCE_USERNAME maps to spring.datasource.username in application.properties.
What you actually want to do here is have your database connection as a cloud foundry service rather than defined in your application.properties. As Daniel's answer suggests, Spring can pick up ENV variables, but setting connection details in the manifest is not the idiomatic way to do this.
By having a service, the connection details are stored in the VCAP_SERVICES variable when bound to the application - spring can read from there. The spring-music app shows the prototypical way of doing this.

Consul not registering services when it comes up

In my configurations, service definitions are kept in /etc/consul.d/server.
Consul is started with following command:
consul agent -config-dir /etc/consul.d/server
When consul is started, none of the service is registered. However, same services can be registered using web API with same definition files.
What is the issue with multiple service definitions as they are not getting registered?
Do you have multiple files? If so -> make sure they all end in .json.
Also When loading configuration, Consul loads the configuration from files and directories in lexical order. For example, configuration file basic_config.json will be processed before extra_config.json. Configuration specified later will be merged into configuration specified earlier. In most cases, "merge" means that the later version will override the earlier. In some cases, such as event handlers, merging appends the handlers to the existing configuration. The exact merging behavior is specified for each option in the docs.
https://www.consul.io/docs/agent/options.html
Otherwise try to put all your service definitions into one json file.
using the services key in your configuration file. (not the plural S)
{
"services": [
{
"id": "red0",
"name": "redis",
"address": "127.0.0.1",
"port": 6000,
},
{
"id": "red1",
"name": "redis",
"address": "127.0.0.1",
"port": 7000,
},
]
}

Resources