Spring Boot- Profile setting Through YML file is not working - spring

In Spring Boot program, profile setting using application.yml is not working whereas the same program is working using applicaiton.properties. the following actions has been performed but it ended up in vain
In Eclipse ide Project-Clean has been done
Maven project updated
Spaces in YML file removed and corrected
YML file is deleted and recreated.
applicaiton.yml
spring:
profiles:
active: dev
applicaiton-dev.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///ntspbms616db
username: root
password: root
dbcp2:
max-total: 100
initial-size: 10
max-conn-lifetime-millis: 10000000
type: org.apache.commons.dbcp2.BasicDataSource
The error i got after running the program is
Enter the Employee Name
Raja
Enter the Employee Designation
Manager
Enter the Basic Salary
9898999
12:28:40.622 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character '\t(TAB)' that cannot start any token. (Do not use \t(TAB) for indentation)
in 'reader', line 4, column 2:
How to resolve the issue?

This error means literally what is says.
The "tab" is not permitted by the YAML specs:
To maintain portability, tab characters must not be used in
indentation, since different systems treat tabs differently. Note that
most modern editors may be configured so that pressing the tab key
results in the insertion of an appropriate number of spaces.
So, you should double check for the tab characters in your yaml files and if exist, replace them by double space.
Also, just to add, I can see incorrect formatting of dbcp2 section, without any intendation, so, it should look like this:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///ntspbms616db
username: root
password: root
dbcp2:
max-total: 100
initial-size: 10
max-conn-lifetime-millis: 10000000
type: org.apache.commons.dbcp2.BasicDataSource

Related

Spring Boot not loading keystore specified in application.yml

I am attempting to run and test a Spring Boot application that I have packaged into a zip file and unpacked on a Linux VM. The zip contains everything the application needs (at least to my knowledge). When I attempt to execute the application, it starts but quickly fails because it cannot load a keystore needed for SSH/TLS secure communications.
I have the following in my application.yml:
server:
port: 8091
ssl:
enabled: true
protocol: TLS
trust-store-type: JKS
trust-store: classpath:keystore/server.keystore
trust-store-password: <hidden>
key-store-type: JKS
key-store: classpath:keystore/ra/server.keystore
key-store-password: <hidden>
The directory structure on the test system is as follows:
[centos#route-assessor route-assessor]$ ls -R
.:
config elastic-apm-agent-1.10.0.jar lib run-route-assessor.sh services-0.0.1-SNAPSHOT.jar
./config:
application.yml keystore log4j2.xml
./config/keystore:
mp ra rg server.keystore
./config/keystore/mp:
server.keystore
./config/keystore/ra:
server.keystore
./config/keystore/rg:
server.keystore
./lib
<dependency jars>
Here is the exception thrown:
[2019-10-23 13:21:31.419] main WARN : context.AnnotationConfigServletWebServerApplicationContext:557 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Could not load key store 'classpath:keystore/server.keystore'
The Spring Boot "runtime" obviously sees and accesses the config directory, but doesn't seem to see the keystore directory contained within. Do I need to specify the paths differently in application.yml or do I need to put the keystore files somewhere else?
Note: I can run this application with the application.yml configured as shown from eclipse without any problem. Granted, all resources are located in src/main/resources for that situation.
UPDATE:
As per #borban's suggestion, I modified the application.yml as follows:
key-store: file:config/keystore/ra/server.keystore
trust-store: file:config/keystore/server.keystore
That seems to have solved one problem, but I'm not out of the woods yet:
[2019-10-23 15:07:17.671] main ERROR: boot.SpringApplication:821 - Application run failed
org.springframework.boot.web.server.WebServerException: Unable to start embedded Jetty server
...
Caused by: java.lang.IllegalStateException: no valid keystore
...
As far as I know, my keystore files are valid and correct (I've been using them on my Windows development box for months). They are copied over as part of the zip distribution. Is there something maybe I'm missing?
I'm also a little concerned with a few other messages in the log. I'm not sure if they're related, but it seems that they could be:
[2019-10-23 15:07:10.153] main WARN : resource.Resource:126 - java.lang.IllegalArgumentException: URI is not hierarchical
[2019-10-23 15:07:10.155] main WARN : resource.Resource:126 - java.lang.IllegalArgumentException: URI is not hierarchical
I don't recall seeing them before.
Are you are trying to access this keystore outside of the classpath and from the filesystem itself? From the folder structure you are giving, that seems to be the case. If you remove the "classpath" prefix and then update your path appropriately, it should work.
From this post, it looks like you have to prefix with "file"
https://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html
You can use maven resource plugin to add the folders you need to the plugin:
https://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html
then use without classpath??

Mapping a JMS JNDI Entries with Thorntail

I need to map the following JMS configuration I currently run in WildFly with Thorntail:
<jms-queue name="ExampleQueue" entries="queue/exampleQueue java:/jboss/exported/jms/queue/exampleQueue"/>
I haven't found any example which defines "entries" but I need it for remote JMS Clients. So I have come up with this configuration which, however doesn't work:
thorntail:
management:
security-realms:
ApplicationRealm:
in-memory-authentication:
users:
bob:
password: Password1!
in-memory-authorization:
users:
jmsuser:
roles:
- guest
messaging-activemq:
servers:
default:
jms-queues:
exampleQueue: {}
entries: queue/exampleQueue java:/jboss/exported/jms/queue/exampleQueue
Which causes:
Caused by: java.lang.RuntimeException: while parsing a block mapping
in 'reader', line 18, column 11:
exampleQueue: {}
^
expected <block end>, but found BlockMappingStart
in 'reader', line 19, column 13:
entries: queue/exampleQueue java ...
I'm a bit lost. Can you help me to fix it?
Thanks!
You've got it almost correct. What you need is (only showing the Messaging part):
messaging-activemq:
servers:
default:
jms-queues:
exampleQueue:
entries:
- queue/exampleQueue
- java:/jboss/exported/jms/queue/exampleQueue
(I think queue/exampleQueue should perhaps be jms/queue/exampleQueue, but not totally sure.)
For a complete working example, you can see e.g. https://github.com/rhoar-qe/thorntail-test-suite/tree/master/javaee/messaging-remote

Failed to bind properties under 'server' to org.springframework.boot.autoconfigure.web.ServerProperties:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-09-04 12:23:24.383 ERROR 12320 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'server' to org.springframework.boot.autoconfigure.web.ServerProperties:
Property: server
Value:
Origin: class path resource [application.properties]:2:0
Reason: No converter found capable of converting from type [java.lang.String] to type [#org.springframework.boot.context.properties.ConfigurationProperties org.springframework.boot.autoconfigure.web.ServerProperties]
Action:
Update your application's configuration
My application.properties file look like this:
server:
port: ${PORT:9191}
spring:
datasource:
url: jdbc:sqlserver://PC382682:1433;databaseName=imvenkat
username: imvenkat
password: imvenkat
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
I know the issue is related to Spring boot, but how should I change my application.properties file to address this issue?
The problem is that you're using YAML format within a properties file. The properties are being parsed line by line, so the properties parser reads server:, and doesn't know how to bind to server directly, which causes the error you see.
Either rename your file to application.yml or change the properties:
server.port=${PORT:9191}
spring.datasource.url=jdbc:sqlserver://PC382682:1433;databaseName=imvenkat
spring.datasource.username=imvenkat
spring.datasource.password=imvenkat
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
This Error can occur when there is mis match between parent version and your spring-boot-devtools dependency.
Remove the version from spring-boot-devtools dependency, that will solve the issue.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
This error can also occur if your config file is in yaml format but you have an error in your yaml so one line looks like a property. Happened to me, previous this was just a broken configuration line, now it breaks the application.
So if you have something broken (look at the url=) like:
server:
port: ${PORT:9191}
spring:
datasource:
url=jdbc:sqlserver://PC382682:1433;databaseName=imvenkat
the same cryptic error can occur. Fixing the config line fixes this error.

spring.profiles.include does not work from command line parameter

I made trivial Spring Boot application and packaged it as JAR.
When I call
java -jar target/boot-active-include-0.0.1-SNAPSHOT.jar
I got
No active profile set, falling back to default profiles: default
When I call
echo spring.profiles.include=B > application.properties
java -jar target/boot-active-include-0.0.1-SNAPSHOT.jar
I got
The following profiles are active: B
When I call
rm application.properties
java -Dspring.profiles.include=B -jar target/boot-active-include-0.0.1-SNAPSHOT.jar
I got
No active profile set, falling back to default profiles: default
But I expect The following profiles are active: B
Can I include Spring profile from command line?
Version: 1.5.1.RELEASE
PS. spring.profiles.active works as expected. But I don't want replace active profiles.
I see what's happening now, and I think it's arguably something I missed when making this change.
Prior to that change, spring.profiles.include was only considered when it was used in a configuration file. Following that change, spring.profiles.include is now considered from any source (System properties, command line arguments, etc) as long as spring.profiles.active is also set.
You can see this by running your sample with both properties set:
java -Dspring.profiles.include=alpha -Dspring.profiles.active=bravo -jar target/boot-active-include-0.0.1-SNAPSHOT.jar
Both the alpha and bravo profiles are active:
2017-02-09 11:30:49.467 INFO 40409 --- [ main] my.Example : The following profiles are active: alpha,bravo
I've opened an issue so that we can straighten this out.

grails database migration plugin problems after upgrade to grails 3

I was using a previous version grails-database-migration plugin for a while and never had any big issues with it. However, lately I upgraded the whole project to grails 3.0.9 and did some additional development, the behavior as follows:
Imported the current prod DB structure into local machine (that DB copy is without the latest changes and new entities)
Execute: grails -Dgrails.env=staging dbm-gorm-diff changlog.xml
What I expected at this point is new changlog.xml file with all changes of existing entities and new ones.
What I get:
Newly defined entities automatically got added into the DB.
The changes in changlog.xml only included the changes of already existing tables, such as:
also, If I try running grails -Dgrails.env=staging run-app
ERROR grails.boot.GrailsApp - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springLiquibase_dataSource': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: liquibase.integration.spring.SpringLiquibase.createDatabase(Ljava/sql/Connection;Lliquibase/resource/ResourceAccessor;)Lliquibase/database/Database;
FAILURE: Build failed with an exception.
What went wrong: Execution failed for task ':bootRun'.
Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/bin/java''
finished with non-zero exit value 1
...
...
Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. | Error Failed to start server (Use --stacktrace to see the full trace)
Here is the portion of my application.yml
dataSource:
pooled: true
url: jdbc:mysql://127.0.0.1:3306/triz?useUnicode=yes&characterEncoding=UTF-8
driverClassName: "com.mysql.jdbc.Driver"
jmxExport: true
username: root
password: password
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
properties:
jmxEnabled: true
initialSize: 5
maxActive: 50
minIdle: 5
maxIdle: 25
maxWait: 10000
maxAge: 600000
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 60000
validationQuery: SELECT 1
validationQueryTimeout: 3
validationInterval: 15000
testOnBorrow: true
testWhileIdle: true
testOnReturn: false
jdbcInterceptors: ConnectionState
defaultTransactionIsolation: 2
environments:
development:
dataSource:
dbCreate: create
# url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
test:
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
staging:
dataSource:
url: jdbc:mysql://127.0.0.1:3306/triz_staging?useUnicode=yes&characterEncoding=UTF-8
and gradle.build
buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
mavenCentral()
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath 'com.bertramlabs.plugins:asset-pipeline-gradle:2.5.0'
// classpath 'com.bertramlabs.plugins:less-asset-pipeline:2.6.7'
classpath "org.grails.plugins:hibernate:4.3.10.5"
classpath 'org.grails.plugins:database-migration:2.0.0.RC4'
}
}
...
...
dependencies {
...
compile 'org.liquibase:liquibase-core:3.3.2'
runtime 'org.grails.plugins:database-migration:2.0.0.RC4'
}
UPDATE
I have another way to approach this problem:
My plan was to generate a changelog based on my current prod DB and then generate a diff for the changes I made. Sounds simple and straightforward; however, it didn't work out as expected. Here is what I did:
Dumped prod DB
Removed liquibase tables
Run: grails dbm-generate-changelog changelog-init.xml --add
At this point, I expected changelog-init.xml to contain the current state of DB. But, instead it applied the changes based on my models first, and then tried generating the diff. Eventually, I ended up with a changelog including my entire existing DB with changes applied from gorm.
What am I doing wrong here?
Additional Observations
It looks like, whenever I try to run ANY migration related commands, grails applies all the changes before that, even through my config says:
staging:
dataSource:
dbCreate: ~
url: jdbc:mysql://127.0.0.1:3306/triz_staging?useUnicode=yes&characterEncoding=UTF-8
properties:
jmxEnabled: true
Also tried completely removing dbCreate. Didn't change anything...
I am done, have no idea where to move next!!!
Well, here is a deal...
I am not sure if that was the real reason, but all I did is moved datasource config from application.yml to application.groovy and everything got back to normal.
I would be happy to hear thoughts.
Thanks.

Resources