Liquibase maven plugin, issue with my database driver - java

I am trying use liquibase maven plugin to generate changelog xml file. I add plugin to my pom file like below.
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<configuration>
<propertyFile>web/src/main/resources/liquibase.properties</propertyFile>
<changeLogFile>web/src/main/resources/data/changelog/db.changelog-master.xml</changeLogFile>
</configuration>
</plugin>
I am using liquibase.properties:
url=jdbc:mysql://127.0.0.1:3306/recproject_test?
useUnicode=yes&characterEncoding=UTF-8
username=recproject
password=sample
classpath=/repository/mysql/mysql-connector-java/5.1.38/mysql-connector-
java-5.1.38.jar
changeLogFile=web/src/main/resources/data/changelog/db.changelog-1.0.0.xml
But when I run liquibase:generateChangeLog as a maven goal I get:
Error setting up or running Liquibase: liquibase.exception.DatabaseException: java.lang.RuntimeException: Cannot find database driver: com.mysql.cj.jdbc.Driver
I store /mysql-connector-java jar in External Libraries and classpath in liquibase.properites is a path to this file.

I've downloaded mysql-connector-java-5.1.38.jar from here but it doesn't contain com.mysql.cj.jdbc.Driver.
So you should use database driver com.mysql.jdbc.Driver or download newer version of driver if you want to use com.mysql.cj.jdbc.Driver .

Add driver=com.mysql.jdbc.Driver to your properties and if you comparing it to another MySql database then you'll need to also include referenceDriver=com.mysql.jdbc.Driver. It seems like Liquibase will default to the MySQL 8 driver unless you specify both the driver and referenceDriver

Related

How to get rid of Liquibase 4.0 error : Please use a relative path or add '/' to the classpath parameter

How to solve this error?
Specifying files by absolute path was removed in Liquibase 4.0. Please use a relative path or add '/' to the classpath parameter.
Suppose I don't have the liquibase installed. I just want to run my Spring project connected to postgres from maven/gradle/eclipse and I'm getting this error from all.
My maven configuration is using liquibase 4.9.1
I'm trying to use
db/changelog/db.changelog-master.xml
In My pom.xml I have
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<configuration>
<propertyFile>/src/main/resources/liquibase.properties</propertyFile>
<changeLogFile>/db/changelog/db.changelog-master.xml</changeLogFile>
</configuration>
</plugin>
What should I type in
cat src\main\resources\liquibase.properties
for classpath?
classpath:/db/changelog/db.changelog-master.xml
from eclipse I get
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.ChangeLogParseException:
Error parsing classpath:/db/changelog/db.changelog-master.yaml
..
Caused by: liquibase.exception.ChangeLogParseException:
Error parsing classpath:/db/changelog/db.changelog-master.yaml
..
Caused by: liquibase.exception.SetupException:
The file db/changelog-base.xml was not found in
..
Specifying files by absolute path was removed in Liquibase 4.0.
Please use a relative path or add '/' to the classpath parameter.
..
Caused by: liquibase.exception.ChangeLogParseException:
The file db/changelog-base.xml was not found in
..
Specifying files by absolute path was removed in Liquibase 4.0. Please use a relative path or add '/' to the classpath parameter.
Could you please help me?
UPDATE:
It worked from maven when I updated the changeLogFile value.
$ mvn liquibase:update
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<configuration>
<propertyFile>/src/main/resources/liquibase.properties</propertyFile>
<changeLogFile>/src/main/resources/db/changelog/db.changelog-master.xml</changeLogFile>
</configuration>
</plugin>
But how to run liquibase from eclipse, Java Spring boot project, I Run as java Application?
Basically I have a spring project that successfully run from eclipse, now with this liquibase configuration my application fails.
using
spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.xml
in My application.properties

SQLException returned null for URL:<no value> while injecting JDBC connection URL with flywaydb plugin

I am getting below exception while running integration tests for my service:
ERROR o.a.tomcat.jdbc.pool.ConnectionPool: Unable to create initial connections of pool.
Caused By: java.sql.SQLException: Driver:com.mysql.jdbc.Driver#xxxxx returned null for URL:<no value>
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:326)
I am using org.flywaydb plugin and is defined in pom.xml as following:
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<url>jdbc:mysql://localhost:3306/mydb</url>
<user>admin</user>
<password>pwd</password>
<validateOnMigrate>false</validateOnMigrate>
</configuration>
</plugin>
In application.yaml, I have defined the properties for integration-tests as following:
spring:
profiles:integration-tests
datasource:
driverClassName: org.h2.Driver
url: jdbc:h2:mem:mydb;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:/path/schema.sql'
username: root
password:
show_sql: false
First thing which is needed to be clarified is that maven flyway plugin and Spring Boot datasource properties are not related in terms of moment of usage:
Maven plugin is run at some point of application build process
SpringBoot reads datasource properties after application was started.
See What is the difference between Flyway Core and Flyway Maven Plugin?
Second thing:
SpringBoot uses in-memory database which accessible via this url: jdbc:h2:mem....
Maven Flyway plugin runs scripts (if any) against another DB url: jdbc:mysql....
That is why flyway configures another database, not the one used in the tests.
Third thing:
SpringBoot seems to not "see" datasource properties and/or driver url: <no value>. How do you run tests? Do you set correct spring-profile?

Unable to make a jar file

I am newbie with Springboot .I am unable to make jar file of Springboot with Mysql to deploy in AWS using Eclipse IDE.
I create an application which work perfectly fine in Localhost and when i want to deploy it into the AWS I comment all my application.properties file which has code like
#spring.jpa.hibernate.ddl-auto=update
#spring.datasource.url=jdbc:mysql://localhost:3306/db_digitalprofile?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
#spring.datasource.username=root
#spring.datasource.password=
#server.port=9090
#spring.jpa.show-sql = true
#spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
and make a new file named as application-prod.properties in src/main/resource with the code:
server.port=5000
spring.datasource.url=jdbc:mysql://${RDS_HOSTNAME}:${RDS_PORT}/${RDS_DB_NAME}
spring.datasource.username=${RDS_USERNAME}
spring.datasource.password=${RDS_PASSWORD}
spring.jpa.hibernate.ddl-auto=update
Now when i want to make a jar file from right click on project> Run As>Maven install to create a jar file it throw a error like:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
java.lang.IllegalStateException: Failed to load ApplicationContext
I had also add following dependency in pom.xml
<configuration>
<finalName> digitalProfile</finalName>
</configuration>
Failed to determine a suitable driver class
You need to check the dependencies. No driver found meaning, driver jar is missing which will be used to connect to Database. mysql driver should be present in your pom file or whatever your build file is.
Add this on your application.properties and it should fix your issue.
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

How to add PostgreSQL datasource to WildFly 9.0?

I've tried tutorial at mastertheboss.com:
./jboss-cli.sh
module add --name=org.postgres --resources=/tmp/postgresql-9.3-1101.jdbc41.jar --dependencies=javax.api,javax.transaction.api
/subsystem=datasources/jdbc-driver=postgres:add(driver-name="postgres",driver-module-name="org.postgres",driver-class-name=org.postgresql.Driver)
data-source add --jndi-name=java:/PostGreDS --name=PostgrePool --connection-url=jdbc:postgresql://localhost/postgres --driver-name=postgres --user-name=postgres --password=postgres
This tutorial works with WildFly 8.2, but it doesn't work with WildFly 9.0. 3rd step fails with error message:
{
"outcome" => "failed",
"failure-description" => "WFLYJCA0041: Failed to load module for driver [org.portgres]",
"rolled-back" => true
}
How to add Postgres datasource to WildFly 9.0?
I've encountered the same error and behavior of WildFly 9. I'm a complete newbie to WF, but after some research I've found that the trouble is in the module naming.
If I'm getting it well, the actual package names in the module are used to resolve the path to module.xml.
I've changed the steps to those below and it worked:
module add --name=org.postgresql --slot=main --resources=/usr/local/lib/postgresql-9.4-1201.jdbc4.jar --dependencies=javax.api,javax.transaction.api
/subsystem=datasources/jdbc-driver=postgres:add(driver-name="postgres",driver-module-name="org.postgresql",driver-class-name=org.postgresql.Driver)
I am running wildfly 10 in a docker:
#ADD DATASOURCES
RUN mkdir -p $JBOSS_HOME/modules/org/postgres/main
COPY files/postgresql-9.4.1208.jre7.jar $JBOSS_HOME/modules/org/postgres/main/
COPY files/module.xml $JBOSS_HOME/modules/org/postgres/main/
COPY files/standalone.xml $JBOSS_HOME/standalone/configuration
Where module.xml is
<module xmlns="urn:jboss:module:1.1" name="org.postgres">
<resources>
<resource-root path="postgresql-9.4.1208.jre7.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.servlet.api" optional="true"/>
</dependencies>
</module>
And standalone contains driver:
<driver name="postgresql" module="org.postgres">
<xa-datasource-class>org.postgresql.Driver</xa-datasource-class>
</driver>
then datasource can be:
<datasource jndi-name="java:jboss/datasources/PostgresDS" pool-name="PostgresDS" enabled="true" use-java-context="true">
<connection-url>jdbc:postgresql://ndis-db:5432/postgres</connection-url>
<driver>postgresql</driver>
...
Note that my ndis-db is a postgres docker. In your case can be localhost.
How I ended up with the error mentioned by you:
1. file name spelled wrongly
2. /modules/org ...etc contain a typo
3. module.xml misspelled as modules.xml
4. ...
Its very simple but could take more time if you will be new with JBOSS EAP/WilFly
Use below steps to create a datasource:
Go to bin folder of server where jboss-cli(Power script) file present: right click on jboss-cli(power script file)--> Run with power shell (a console will open).
Add the PostgreSQL JDBC driver as a core module.
module add --name=com.postgresql
--resources=/path/to/postgresql-9.3-1102.jdbc4.jar --dependencies=javax.api,javax.transaction.api
Register the PostgreSQL JDBC driver.
/subsystem=datasources/jdbc-driver=postgresql:add(driver-name=postgresql,driver-module-name=com.postgresql,driver-xa-datasource-class-name=org.postgresql.xa.PGXADataSource)
Add the PostgreSQL datasource.
data-source add --name=PostgresDS --jndi-name=java:jboss/PostgresDS
--driver-name=postgresql --connection-url=jdbc:postgresql://localhost:5432/postgresdb --user-name=admin --password=admin --validate-on-match=true --background-validation=false --valid-connection-checker-class-name=org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker
--exception-sorter-class-name=org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter
be careful with path path/to this is path where your downloded Postgresql-jdbc.jar is present.
Put your Postgres JDBC driver into deployment folder (just deploy).
Now use CLI console and enter this command:
data-source add --name=PostgresqlDS --jndi-name=java:jboss/datasources/PostgresqlDS --driver-name=postgresql-9.4-1201.jdbc41.jar --connection-url=jdbc:postgresql://localhost:5432/test --user-name=USER --password=PASSWORD
Check if your driver is jdbc4.
I don't know why but adding datasources by web console doesn't work. By CLI works.
The right solution for extending JDBC drivers is add driver as module to server.
In WildFly 9 you can do it using cli console. You can't do this by copy JDBC jar file (with xml) to "module" folder like in WildFly 8.
Execute commands:
module add --name=org.postgres --resources=postgresql-9.4-1201.jdbc41.jar --dependencies=javax.api,javax.transaction.api
/subsystem=datasources/jdbc-driver=postgres:add(driver-name="postgres",driver-module-name="org.postgres",driver-class-name=org.postgresql.Driver)
To list instaled drivers enter:
/subsystem=datasources:installed-drivers-list
With driver creating datasources will be easy.
Please use 9.0 Final version. In CR are bugs.
Regards,
Pawel M
You don't mention your java/jdbc version. I've just experienced the same issue and it was due to a driver vs Java 1.8 mismatch. With the wildfly 9 upgrade did you also upgrade Java?
The ".jdbc41." driver version is built for Java 1.7. Postgres has a matrix showing the combinations of Java/JDBC and Postgres driver versions that are compatible. Perhaps you need: postgresql-9.4.1209.jar (which is for 1.8/jdbc42)
Then in the CLI (assuming domain mode and profile=full)
module add --name=org.postgresql.Driver --resources=/tmp/postgresql-9.4.1209.jar
connect
/profile=full/subsystem=datasources/jdbc-driver=postgres:add(driver-name="postgres",driver-module-name="org.postgresql.Driver",driver-class-name=org.postgresql.Driver)
exit
Just a note: I have tested the CLI commands, as taken from the tutorial mentioned, against WildFly 10 and it works correctly in creating the JDBC Driver and the datasource.
Besides it, I can see that the error log reported contains a mispelling of the module name ("org.portgres"):
{
"outcome" => "failed",
"failure-description" => "WFLYJCA0041: Failed to load module for driver [org.portgres]",
"rolled-back" => true
}
Disclaimer: I'm the owner of mastertheboss.com

Flyway-maven-plugin how to read the settings from spring boot application.yml

In my Spring Boot project I want to use flyway-maven-plugin.
My pom:
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.1</version>
<configuration>
<url>jdbc:mysql://localhost:3306/my_database?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&connectionCollation=utf8_unicode_ci&characterSetResults=UTF-8</url>
<user>root</user>
<password>${spring.datasource.password}</password>
</configuration>
</plugin>
And here is my application.yml
spring:
profiles.active: default
---
spring:
profiles: default
spring.datasource:
password: root
As I understood to use mvn flyway:info I need some plugin which will read my application.yml. Or maybe there is another way?
I have the following in my src/main/resources/application.properties
flyway.url=jdbc:sqlserver://localhost:1433
flyway.user=james_hetfield
flyway.password=MetaLLic#
spring.datasource.url=${flyway.url}
spring.datasource.user=${flyway.user}
spring.datasource.password=${flyway.password}
And then I run the migrations from the command-line as follows
mvn -Dflyway.configFiles=src/main/resources/application.properties flyway:migrate
My understanding is that if you are using Flyway with Spring Boot you do not use the flyway-maven plugin.
EDIT: Using Flyway CLI has a place when you need to work with the DB directly to make modifications to Flyway's schema table. An example would be to init an existing DB or clean a test project.
These are the current settings that can be performed out of the box by setting properties within the application.properties for Flyway.
# FLYWAY (FlywayProperties)
flyway.check-location=false # check that migration scripts location exists
flyway.locations=classpath:db/migration # locations of migrations scripts
flyway.schemas= # schemas to update
flyway.init-version= 1 # version to start migration
flyway.init-sqls= # SQL statements to execute to initialize a connection immediately after obtaining it
flyway.sql-migration-prefix=V
flyway.sql-migration-suffix=.sql
flyway.enabled=true
flyway.url= # JDBC url if you want Flyway to create its own DataSource
flyway.user= # JDBC username if you want Flyway to create its own DataSource
flyway.password= # JDBC password if you want Flyway to create its own DataSource
YAML version:
flyway:
check-location: false
locations: classpath:db/migration
....
If you need further customization you may need to write a #Bean to further customize how Flyway interacts with your application.
To read *.yml properties from pom you should use https://github.com/aadnk/yaml-properties-plugin

Categories

Resources