If I run Liquibase by Maven then I use "mvn liquibase:updateSQL", but how to save statement using SpringLiquibase?
You can save the updateSQL script to an external file using the parameter -Dliquibase.migrationSqlOutputFile=xxxx while invoking the command. See more info at : https://docs.liquibase.com/tools-integrations/maven/commands/maven-updatesql.html
Or you could specify the file name in your maven pom.xml using in your Liquibase configuration and adding a maven goal.
If you are asking to use a Spring property for Liquibase like spring.liquibase.change-log then I'm not sure if one exists for migrationSqlOutputFile like spring.liquibase.migrationSqlOutputFile, but you could try it.
Related
`I have already running project and trying to introduce liquibase in middle of project.
This is spring boot maven based project.
I am following steps.
1.liquibase.properties
url=jdbc:postgresql://localhost:5432/cc
username=postgres
password=deadline123
driver=org.postgresql.Driver
outputChangeLogFile=src/main/resources/db/changelog/db.changelog-master.yaml
runOnChange=true
referenceUrl=jdbc:postgresql://localhost:5432/cc
referenceUsername=postgres
referencePassword=deadline123
changelogFile=src/main/resources/db/changelog/db.changelog-master.yaml
diffChangeLogFile=src/main/resources/diff.yaml
Run command to generate changelog for current DB state
mvn liquibase:generateChangeLog
Then run command to sync changelog and create entry in DB
mvn liquibase:changelogSync
Starting my application but it's throwing error relation already exist. I can't figure out why liquibase is executing already executed changeset? I am using liquibase 4.6.3
Liquibase doesn't compare changes against your actual tables, it only keeps track of what changesets have already been executed (it creates another table for that called databasechangelog).
That being said, you have to make sure liquibase executes your changesets with a precondition that says 'if anything fails, mark it as executed'. It can be achieved like so:
<preconditions onFail="MARK_RAN">
<not>
<tableExists tableName="person"/>
</not>
</preconditions>
This example as well as much more information could be found on https://www.liquibase.com/blog/adding-liquibase-on-an-existing-project
Issue was with file path src/main/resources/db/changelog/db.changelog-master.yaml
liquibase:changelogSync adds file path as above in log sync and liquibae on start actually takes classpath classpath:/db/changelog/db.changelog-master.yaml
I just started right now a new project in Intellij using Spring Boot ver 2.1.3 and Flyway 5.2.4 with Java 11.
After try to start my project i got :
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'flyway' threw exception; nested exception is java.lang.IllegalStateException: Cannot find migrations location in: [classpath:db/migration] (please add migrations or check your Flyway configuration)
I have the following folders:
As you can see i have "db/migration" but without any migration, i just started right now. Debugging the class FlywayAutoConfiguration i got the following:
So, i tried to return all files in "classpath:", see:
Note that i just have "application.properties" file.
It is not that much useful or accurate answer.
But This issue make you frustrated so that i give this solution.
Note: Strange but it's true, Sometime it's not allow copy paste because your folder created db.migration and it expact db->migration(It's not same in this scenario). So whenever you start from scratch. Go to the resource folder -> Create DB folder -> Create migration folder -> Create database file with Version_SubVersion__Name(As defined below).
Normally this happens in following cases,
Path is not proper try using set locations param value.
db.migrate folder not contain any file.
Check name of file : V1_1__(short_desc)
Try to run using, mvn compile flyway:migrate
In my case i already place sql file over there but still it gives same error,
Basically i place this sql file using copy paste from somewhere.
When i try to add one new file on same place using IDE (Intellij : Right click on migration folder -> new -> Flyway migration -> versioned migration), then it ask me(warning) about some delicate allowance(normally we mention in database configuration i also place there still), and it start working.
Flyway requires at-least one script, disable it until u need it by using following command in application.properties file
spring.flyway.enabled=false
I believe that Flyway requires at least one migration script to initialize. Try adding a simple sql creation script into your migration folder and give it another try. Alternatively you can disable the flyway dependency until you need it.
I had a similar error, and solved it as follows: I added these commands
spring.flyway.baselineOnMigrate=true
spring.flyway.check-location=true
spring.flyway.locations=classpath:db/migration
spring.flyway.schemas=public
spring.flyway.enabled=true
to application.properties
I had the same issue. When I created the directory, I simply typed db.migration - the same way one would do with package names. InteliJ will display both db.migration and db/migration directories as db.migration, so while it may look correct in IntelliJ, flyway requires the latter.
Even when having your migration files in the db/migration folder, flyway won't detect it.
Then you will have to fix this by explicitly setting the locations in your application.properties (or appliocation.yml) by adding:
spring.flyway.locations=classpath:db/migration
N.B: Also you need to have at least one script to initialize flyway, you can even put an empty one. But you need to have at least one script
As #Guy suggested in his answer, I created an SQL file in the migration folder and left it empty. Named v1_0__mock_flyway.sql in the migration folder classpath:db/migration. Error solved.
If you have tried everything above and it still does not apply the migrations.
Be sure you have followed the steps in the previous answers
A db.migration folder exists in your resources folder
The sql scripts are named with respect to the correct naming convention, such as V1__init.sql
The following config exists in your application.properties/application.yml
spring.flyway.locations=classpath:db/migration
Do a clean build
-> mvn clean
Then restart your spring boot app, this worked for me.
I have edited, for example, application.properties from
spring.mail.host=stmp.test.com
to
spring.mail.host=${server.mail.host}
and I override at starttime these properties to the correct values. This works fine until I want to run maven to build my application.
I receive the following Exception
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class
I think the problem is that maven also needs these values but how and where can I insert them? I dont want to run mvn on the cli.
I think #Nicholas K was making the point that you can pass in those values in you maven command. For example with the argument mvn spring-boot:run "-Dserver.mail.host=mailhost".
You can also set an environment variable and that should be injected:
export SERVER_MAIL_HOST=mailhost
Or if you sometimes don't want to set them you could set a default for the property in your properties file:
spring.mail.host=${server.mail.host:defaultmailhost}
Or default it to an empty string
I'm writing a Maven plugin which I'd like to prompt for a simple user input and decide whether to halt the plugin's execution.
I'd like to do something like this:
$> mvn myplugin:run
[MAVEN] would you like to continue? [default value: y] _
I've tried using maven-antrun-plugin as described here, but in this case Maven gets user input when I build my plugin. Instead, I'd like to retrieve input when user is running my plugin from within some other app that has declared my plugin (confusing?)
Use a Prompter component and have it injected in your plugin (assuming you are using Maven plugin annotations, if not use the equivalent javadoc tags):
#Component
private Prompter prompter;
And to use it:
String name = prompter.prompt("Please enter your name");
Pull in this dependency in your plugin's POM:
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-interactivity-api</artifactId>
<version>1.0-alpha-6</version>
</dependency>
The prompter component is used by the release plugin for prompting the user for tags and versions and the archetype plugin as well.
Don't do this. If you need to supply data to a mojo, do it via configuration.
The behavior of your Maven build should be entirely predictable based on your POM and the goals & options supplied to the Maven command line. If you allow a user to feed in additional information during the build, your POM no longer completely describes your project.
Also, it would prevent any automated build server doing its job.
as variant, to not break maven way as wool.in.silver wrote, you can use shell script that will prompt values and then call maven with gathered values as parameters
Java Maven Spring Junit with webapplication
I am using following code to load property file into spring context placer holder.
<context:property-placeholder location="file:${RESOURCE_PATH}/jdbc.properties" />
in eclipse Junit run time configuration i have defined "RESOURCE_PATH" so it runs fine when i execute my junit tests from GUI but when i run from maven they fail.
Can we define variable and pass in pom file at run time ?
You should either supply the property RESOURCE_PATH using -D switch when running maven or put it into pom.xml into section <properties>; something like this:
<properties>
<RESOURCE_PATH>put your path here</RESOURCE_PATH>
</properties>