0

I'm using gradle credentials plugin to pass user and password database to liquibase plugin. Also, I'm using spring data repositories and hibernate for data layer and i want to pass it the same configuration of liquibase plugin. There is a way to pass the credentials through gradle (I don't want to create application.properties file because the credentials are already stored on the credentials plugin)?

I'm using the following code to pass credentials to liquibase:

def changelog = "$projectDir/src/main/resources/db_version/changelog-master.xml"
def urlDatabase = ...
def schema = ...

liquibase {
    activities{
        main {
            changeLogFile changelog
            url urlDatabase
            username credentials.devUsr
            password credentials.devPass
            defaultSchemaName schema
        }
    }
}

and i want to do something like:

datasource{
    username credentials.devUsr
    password credentials.devPass
}

or maybe writing the properies:

spring.datasource.username credentials.devUsr
spring.datasource.password credentials.devPass
Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Cristian García
  • 125
  • 1
  • 1
  • 8

3 Answers3

0

If you are using Spring Boot which automatically accepts environment variables you can pass db credentials through this mechanism. Here's corresponding documentation

If you have no Spring Boot, you still can pass variables through environment but you need to implement acceptance in your application. Here's comprehensive overview

And here is an answer regarding passing env variables in Gradle.

webdizz
  • 828
  • 7
  • 10
0

Spring offers various ways to use properties inside the configuration and ways to provide them, including passing them as command line arguments or system properties. See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html for details.

With that set up in your Spring configuration, you just need to instruct Gradle to pass on the values from the plugin as command line arguments or System properties.

This describes a way to set System properties with Gradle: https://discuss.gradle.org/t/how-to-set-a-system-property-before-calling-task-in-gradle/6060/4

Passing command line arguments seems to be covered here: Gradle task - pass arguments to Java application

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
0

You are right @Jens Schauder and @webdizz, but here is example:

bootRun{
   systemProperty "spring.datasource.username", credentials.devUsr
   systemProperty "spring.datasource.password", credentials.devPass
}

with this code I pass the database credentials as system properties when start the spring boot application.

Cristian García
  • 125
  • 1
  • 1
  • 8