Why does liquibase not start when the spring boot application starts? - spring-boot

I am trying to create a basic database structure when starting a spring boot application. I need that when starting the task :bootRun was run by liquibase.
It starts without errors, a connection to the database is created, but liquibase does not start. It does not give errors, there is no information in the log about it.
Why doesn't it start automatically with spring boot?
build.gradle
plugins {
id 'org.springframework.boot' version '3.0.0-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'spring.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5:3.1.0.M1'
implementation group: 'org.liquibase', name: 'liquibase-core', version: '4.12.0'
runtimeOnly'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
compileOnly 'org.projectlombok:lombok'
}
tasks.named('test') {
useJUnitPlatform()
}
application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/db
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=none
spring.liquibase.change-log=classpath:db/changelog/db.changelog-root.yaml
src/db/changelog/db.changelog-root.yaml
databaseChangeLog:
- logicalFilePath: db/changelog/db.changelog-root.yaml
- changeSet:
id: create-a-structure
author: your_liquibase_username
changes:
- createTable:
tableName: app_user
columns:
- column:
name: id
type: BIGINT
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: username
type: varchar(254)
constraints:
unique: true
nullable: false
- column:
name: password
type: varchar(64)
constraints:
nullable: false
- column:
name: first_name
type: varchar(50)
nullable: false
- column:
name: last_name
type: varchar(50)
nullable: false

I believe the spring property for the changelog file should not have the classpath reference:
spring.liquibase.change-log=db/...
instead of
spring.liquibase.change-log=classpath:db/...

I had a similar problem. This solved it for me https://stackoverflow.com/a/41960712/3999476
I have a multi module maven project, I had to move the dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
to the same module where I added the dependency for liquibase.

Just noticed that you've put the liquibase yaml in:
src/db/changelog/db.changelog-root.yaml.
Since the specified value is:
spring.liquibase.change-log=classpath:db/changelog/db.changelog-root.yaml
Could you put the file into:
src/main/resources/db/changelog/db.changelog-root.yaml?

Related

Building app with spring-boot + Cassandra, getting "Unable to load class 'com.datastax.oss.driver.api.mapper.entity.naming.GetterStyle'"

Facing isssue while integration with datastax with cassandra
gradle file :
plugins {
id 'org.springframework.boot' version '2.3.12.RELEASE'
id 'io.spring.dependency-management' version '1.0.13.RELEASE'
id 'java'
id 'org.sonarqube' version '3.2.0'
}
group = 'in.demo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
def javaDriverVersion ='4.14.1'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation ('org.springframework.boot:spring-boot-starter')
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation('org.springframework.boot:spring-boot-starter-web') {
exclude module: 'spring-boot-starter-tomcat'
exclude group: 'com.fasterxml.jackson.core', module: 'jackson-core'
exclude group: 'com.fasterxml.jackson.core', module: 'jackson-databind'
exclude group: 'com.fasterxml.jackson.core', module: 'jackson-annotations'
}
implementation('org.springframework.boot:spring-boot-starter-jetty')
implementation('org.springframework.boot:spring-boot-starter-webflux')
runtimeOnly('org.springframework.boot:spring-boot-devtools')
// datastax driver
implementation group: 'com.datastax.oss', name: 'java-driver-core', version: javaDriverVersion
implementation group: 'com.datastax.oss', name: 'java-driver-query-builder', version: javaDriverVersion
//implementation group: 'com.datastax.oss', name: 'java-driver-mapper-runtime', version: javaDriverVersion
implementation group: 'com.datastax.oss', name: 'native-protocol', version: '1.5.0'
annotationProcessor group: 'com.datastax.oss', name: 'java-driver-mapper-processor', version: javaDriverVersion
//compileOnly group: 'com.datastax.oss', name: 'java-driver-mapper-processor', version: '4.14.1'
compileOnly group: 'com.datastax.oss', name: 'java-driver-mapper-runtime', version: javaDriverVersion
implementation 'org.mapstruct:mapstruct:1.3.1.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.1.Final'
}
tasks.named('test') {
useJUnitPlatform()
}
#Data
#AllArgsConstructor
#NoArgsConstructor
#Entity(defaultKeyspace = "quest_test")
public class TestTable {
private UUID id;
private String name;
}
import com.datastax.oss.driver.api.mapper.annotations.Dao;
import com.datastax.oss.driver.api.mapper.annotations.Select;
#Dao
public interface TestTableDao {
#Select
TestTable findById();
}
import com.datastax.oss.driver.api.mapper.annotations.DaoFactory;
import com.datastax.oss.driver.api.mapper.annotations.Mapper;
#Mapper
public interface TestTableMapper {
#DaoFactory
TestTableDao testTableDao();
}
getting error
Unable to load class 'com.datastax.oss.driver.api.mapper.entity.naming.GetterStyle'.
i tried to interaction with different project also but still not able to solve issue
. for testing part i added library mappstruct which is generation code on compile time
With drivers 4x+, you need to declare an annotation processor for the mappers code to be generated at build time.
Gradle file
dependencies {
annotationProcessor group: 'com.datastax.oss', name: 'java-driver-mapper-processor', version: javaDriverVersion
compile group: 'com.datastax.oss', name: 'java-driver-mapper-runtime', version: javaDriverVersion
}
Documentation
Here more code to work with Driver 4x
You might be interested in the full-fledged Spring PetClinic application running with your technical stack.

How to connect Spring and Oracle db (oracle cloud) 19c

if i try to connect this error show
oracle.net.ns.NetException: Invalid connection string format, a valid format is: "host:port:sid" (CONNECTION_ID=plSfz1GBTiKKWbZqZbjUrA==)
i think yml datasource or jpa is wrong...
but I checked out gogle anywhere but,
oracle cloud(wallet) <-> spring is I can't found it
plz help me!
this is my build.gradle
plugins {
id 'org.springframework.boot' version '2.5.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'war'
}
group = ''
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.projectlombok:lombok:1.18.20'
implementation 'junit:junit:4.13.1'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
compileOnly 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
runtimeOnly 'com.oracle.database.jdbc:ojdbc8'
implementation 'com.oracle.ojdbc:ucp'
implementation 'com.oracle.database.security:oraclepki'
implementation 'com.oracle.database.security:osdt_core'
implementation 'com.oracle.database.security:osdt_cert'
annotationProcessor 'org.projectlombok:lombok'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
this is my application.yml
# local, dev, prod 공통 설정
server:
port: 8080
tomcat:
uri-encoding: UTF-8
spring:
datasource:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:#[TNSNAME]?TNS_ADMIN=./src/main/resources/Wallet
username:
password:
jpa:
database-platform: org.hibernate.dialect.Oracle12cDialect
open-in-view: true
hibernate:
ddl-auto: none
# ddl-auto: create-drop
# ddl-auto: validate
If you use TNS_ADMIN, it has to be path to TNS_ADMIN file, and your connection has to be declared in tnsnames.ora.
Related reading: https://docs.oracle.com/en/cloud/paas/autonomous-database/adbsa/connect-jdbc-thin-wallet.html#GUID-BE543CFD-6FB4-4C5B-A2EA-9638EC30900D
Refer to this blog for code sample and also instructions.
i resolved this way,
spring:
datasource:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:#<***TLSNAME>?TNS_ADMIN=D:/Wallet
username: username
password: password

Cloud client config application do not get properties from Config Server Spring boot

I have a web application and I want to use Server Configuration from Spring Boot.
Both applications are located on localhost machine. I have made first two applications from scratch and they worked together, but when i use the client that has many dependencies in it (not just the cloud-config and web dependency) it is not working anymore. How do I know? I have a variable in properties file in server and i try to use it in my client with #Value("${atena}")
and error appears java.lang.IllegalArgumentException: Could not resolve placeholder 'atena' in value "${atena}".
The following image is my server config application.
The main class from server has the following annotation #EnableConfigServer
In atena-config.yml I have only the variable name:
atena: 'Hellllloooooo'
bootstrap.yml content
server:
port: 9000
spring:
profiles:
active: native
and build.gradle dependencies:
dependencies {
implementation 'org.springframework.cloud:spring-cloud-config-server'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
I am sure the server it is correct, something is wrong with my client.
--------------------> Client side
I have a restcontroller:
#RestController
#RequestMapping("/songs")
public class SongController {
#Value("${atena}")
String variable;
#GetMapping(value="/check-from")
public String viewVariable(){
return variable;
}
}
in which I am trying to get the variable from server config.
bootstrap.yml from client
spring:
application:
name: atena-config
cloud:
config:
uri: http://localhost:9000
And lastly the build.gradle from client:
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.apis:google-api-services-youtube:v3-rev206-1.25.0'
implementation 'org.springframework.boot:spring-boot-starter'
implementation('org.apache.tomcat:tomcat-jdbc:9.0.10')
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation('org.mybatis:mybatis:3.4.6')
implementation('org.mybatis:mybatis-spring:1.3.2')
implementation('org.springframework.boot:spring-boot-starter-jdbc')
implementation('org.springframework.cloud:spring-cloud-starter-config')
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
I really don't know what happen, I am pretty sure that these dependencies are the problem, but I have not figured out which one, i can not exclude any of them, because I am using them in project.
Never mind. I have fixed it. Indeed the problem was from my dependencies, my gradle.build was with problem. I have created a new project with spring initializer having all the dependencies and copied the new gradle.build from there and now is it working.
This is the new build.gradle from client
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
}
ext {
set('springCloudVersion', 'Greenwich.RELEASE')
}
dependencies {
compile 'com.google.apis:google-api-services-youtube:v3-rev206-1.25.0'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.0'
implementation 'org.springframework.cloud:spring-cloud-starter-config'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}

Liquibase changelog not running in spring boot

I´m trying to run a springboot api and configure the database using liquibase.
The problem here is I followed all steps described in several tutorial but the changelog is never executed.
The code:
Gradle config:
group = 'com.strixtools'
version = '0.0.1-SNAPSHOT'
description = """Strix Tools"""
buildscript {
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:2.1.1.RELEASE"
classpath "gradle.plugin.com.palantir.gradle.docker:gradle-docker:0.20.1"
}
}
group = 'strixtools'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'com.palantir.docker'
bootJar {
baseName = 'gs-spring-boot-docker'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
configurations.all {
exclude module: "spring-boot-starter-logging"
exclude module: "logback-classic"
}
compile ('org.springframework.boot:spring-boot-starter:2.1.1.RELEASE')
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version:'2.1.1.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version:'2.1.1.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version:'2.1.1.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version:'2.1.1.RELEASE'
compile group: 'org.springframework.security.oauth.boot', name: 'spring-security-oauth2-autoconfigure', version: '2.1.1.RELEASE'
compile group: 'org.apache.commons', name: 'commons-lang3', version:'3.4'
compile group: 'io.jsonwebtoken', name: 'jjwt', version:'0.9.0'
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version:'2.6.5'
compile group: 'org.liquibase', name: 'liquibase-core'
compile group: 'com.h2database', name: 'h2', version:'1.4.196'
runtime group: 'org.postgresql', name: 'postgresql'
testCompile(group: 'org.springframework.boot', name: 'spring-boot-starter-test', version:'2.1.1.RELEASE')
compile group: 'org.springframework.boot', name: 'spring-boot-starter-log4j2', version:'2.1.1.RELEASE'
}
task stage {
dependsOn build
}
task unpack(type: Copy) {
dependsOn bootJar
from(zipTree(tasks.bootJar.outputs.files.singleFile))
into("build/dependency")
}
docker {
name "${project.group}/${bootJar.baseName}"
copySpec.from(tasks.unpack.outputs).into("dependency")
buildArgs(['DEPENDENCY': "dependency"])
}
aplication.yml
strix-api:
host: https://aaa.com
client: asdasdasfasdf
secret: afadsfsdaf
user: afdadsfsadf
pass: afadsfsdafsdaf
---
spring:
profiles: local
datasource:
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: sa
password:
liquibase:
change-log: classpath:db/changelog-master.xml
enabled: true
drop-first: true
jpa:
hibernate:
ddl-auto: none
show-sql: true
h2:
console:
enabled: true
and my SpringAplication class
#SpringBootApplication
public class StrixToolsApplication {
public static void main(String[] args) {
SpringApplication.run(StrixToolsApplication.class, args);
}
//TODO: we should move this to another side
#Bean(name = "messageSource")
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageBundle = new ReloadableResourceBundleMessageSource();
messageBundle.setBasename("classpath:messages/messages");
messageBundle.setDefaultEncoding("UTF-8");
return messageBundle;
}
#Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}
The problem is liquibase is never executing. If I change the changelog name to anthing else it doesn't even throws an error.
Thanks for your help
Regards
In my case the problem was that I had a dependency declared for the gradle plugin, like so
liquibaseRuntime("org.liquibase:liquibase-core:3.8.1")
but not for liquibase at runtime, like so
runtimeOnly("org.liquibase:liquibase-core:3.8.1")
Maybe you are missing the right version of liquibase. Try with 2.0.1.
Here you have a small sample: (you can refactor the notation that you are using for versions and groups).
apply plugin: 'liquibase'
plugins {
id 'io.spring.dependency-management' version '1.0.5.RELEASE'
id 'org.liquibase.gradle' version '2.0.1'
}
ependencies {
implementation('org.springframework.boot:spring-boot-starter-actuator')
implementation('org.springframework.boot:spring-boot-starter-web')
implementation('org.springframework.boot:spring-boot-starter-security')
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.springframework.boot:spring-boot-starter-validation')
implementation('org.liquibase:liquibase-core')
runtime 'mysql:mysql-connector-java'
liquibaseRuntime group: 'org.liquibase', name: 'liquibase-core', version: liquibaseCoreVersion
liquibaseRuntime group: 'org.liquibase', name: 'liquibase-groovy-dsl', version: liquibaseGroovyDslVersion
liquibaseRuntime 'mysql:mysql-connector-java'
}
liquibase {
activities {
main {
changeLogFile 'src/main/resources/db/liquibase-changelog.xml'
url 'jdbc:mysql://' + System.env.DATABASE_HOST + ':' + System.env.DATABASE_PORT + '/' + System.env.DATABASE_NAME + '?useSSL=false'
username System.env.DATABASE_USER
password System.env.DATABASE_PASSWORD
driver 'com.mysql.jdbc.Driver'
}
}
}

Configure application.properties in spring boot for public url in keycloak

I'm developing spring boot application (v 2.1.0) with keycloak to secure app (v 4.5.0).
I already configured keycloak security in gradle and application.properties.
However keycloak returns unauthorized (401 Error) even for urls which are not added to security constraints.
Gradle and application.properties file are provided following
Gradle
buildscript {
ext.kotlin_version = '1.3.11' // Required for Kotlin integration
ext.spring_boot_version = '2.1.0.RELEASE'
ext.keycloak_version = '4.5.0.Final'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // Required for Kotlin integration
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#spring-support
classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
}
}
apply plugin: 'kotlin' // Required for Kotlin integration
apply plugin: "kotlin-spring" // https://kotlinlang.org/docs/reference/compiler-plugins.html#spring-support
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
jar {
baseName = 'gs-rest-service'
version = '0.1.0'
}
repositories {
jcenter()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile group: 'org.hibernate', name: 'hibernate-envers', version: '5.1.0.Final'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // Required for Kotlin integration
compile("org.jetbrains.kotlin:kotlin-reflect")
compile "org.springframework.boot:spring-boot-starter-web"
// compile 'org.springframework.boot:spring-boot-starter-security'
//KeyCloak
compile group: 'org.keycloak', name: 'keycloak-spring-boot-starter', version: '4.7.0.Final'
compile group: 'org.keycloak', name: 'keycloak-spring-boot-adapter', version: '4.7.0.Final'
compile "org.keycloak:keycloak-admin-client:$keycloak_version"
//RestEasy
// https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-client
compile group: 'org.jboss.resteasy', name: 'resteasy-client', version: '3.6.2.Final'
// https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxrs
compile group: 'org.jboss.resteasy', name: 'resteasy-jaxrs', version: '3.6.2.Final'
// https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jackson2-provider
compile group: 'org.jboss.resteasy', name: 'resteasy-jackson2-provider', version: '3.6.2.Final'
// //Oauth2
// // https://mvnrepository.com/artifact/org.springframework.security.oauth/spring-security-oauth2
// compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.3.4.RELEASE'
// https://mvnrepository.com/artifact/org.springframework/spring-jdbc
compile group: 'org.springframework', name: 'spring-jdbc', version: '5.1.0.RELEASE'
// https://mvnrepository.com/artifact/org.postgresql/postgresql
compile group: 'org.postgresql', name: 'postgresql', version: '9.3-1100-jdbc41'
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Application.properties
#Server
server.port = 8090
# Database
spring.jpa.database=postgresql
#spring.datasource.platform
spring.jpa.show-sql=false
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/dbname
spring.datasource.username=postgres
spring.datasource.password=123
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
# new
keycloak.realm = realm
keycloak.auth-server-url = http://localhost:8080/auth
keycloak.ssl-required = external
keycloak.resource = client_id
keycloak.credentials.secret = client_secret
keycloak.realm-key=public_key
keycloak.security-constraints[0].auth-roles[0]=USER
keycloak.security-constraints[0].security-collections[0].patterns[0]=/user/*
keycloak.security-constraints[0].security-collections[0].patterns[1]=/createStudent
keycloak.security-constraints[0].auth-roles[1]=admin
keycloak.security-constraints[0].security-collections[1].patterns[0]=/createStudent2
keycloak.security-constraints[0].security-collections[1].patterns[1]=/createRole
keycloak.security-constraints[0].security-collections[1].patterns[2]=/roles
keycloak.security-constraints[0].security-collections[1].patterns[3]=/assignRole
keycloak.security-constraints[0].security-collections[1].patterns[4]=/users
I want login and main page of the app to be public
/login and /home
Thanks in advance!

Resources