Marco Lackovic
Marco Lackovic

Reputation: 6517

How to load environment variables from a dotenv file in Flyway?

Flyway supports environment variables in config files.

Is there a way to make Flyway load these variables from a file, similarly to what Docker and Node.js with dotenv do?

The content of the .env file is for example:

DB_URL=jdbc:postgresql://localhost:5432/db_name

And flyway.conf:

flyway.url=${DB_URL}

Upvotes: 2

Views: 946

Answers (1)

Abhinaba Chakraborty
Abhinaba Chakraborty

Reputation: 3671

If you are using flyway-maven-plugin, you have 3 ways currently:

  1. Defining flyway properties in POM.xml

eg.

  <properties>
    <flyway.url>jdbc:h2:mem:public;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MySQL;INIT=CREATE SCHEMA IF NOT EXISTS "public";</flyway.url>
    <flyway.user>root</flyway.user>
    <flyway.password></flyway.password>
  </properties>
  1. Defining your flyway properties in some .env or a .conf file.
mvn -Dflyway.configFiles=src/main/resources/some-env-file.env flyway:migrate

Contents of some-env-file.env:

flyway.url=jdbc:h2:mem:public;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MySQL;INIT=CREATE SCHEMA IF NOT EXISTS "public";
flyway.user=root
flyway.password=
  1. Injecting the environment variables directly during maven goal execution:
 mvn -Dflyway.url="jdbc:h2:mem:public;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MySQL;INIT=CREATE SCHEMA IF NOT EXISTS public;" -Dflyway.user=root -Dflyway.password=root flyway:migrate

But if you want to load properties from some file using properties-maven-plugin and make them available as enviroment variables, to be used by your flyway-maven-plugin , then unfortunately that is not working.

Here is the github issue tracking this.

Upvotes: 1

Related Questions