Friday, July 27, 2018

Gradle Spring Boot Custom Configuration

Leave a Comment

I have an application which uses a mysql database to persist information. I would like to create a version of this application which uses an embedded database (mariaDB4j) and add it as a service to our CI environment, so when we launch this embedded version with our end-to-end test, the QA team gets a clean database.

I read a lot about this online, and it looks like gradle configurations are the way to go. The closest I found on this was:

sourceSets {     qaci {         java {             srcDir 'src/qa/java'         }         compileClasspath += sourceSets.main.runtimeClasspath         compileClasspath += sourceSets.main.resources     } }  configurations {     qaciCompile.extendsFrom compile }  bootRepackage {     customConfiguration = myCustomConfig } 

Unfortunately, bootRepackage was replaced by bootJar. I'm using gradle spring boot plugin 2.0.1.RELEASE, and when I try to use bootJar.customConfiguration I got an error saying that this is an unknown property.

Also, mariaDB4j requires a configuration class to work properly, I've included it at src/qa/java and created a new source set in order to it be added.

Does anyone knows how to tell the gradle spring boot plugin to use a custom configuration?

2 Answers

Answers 1

Have you thought about using spring profiles?

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

It will allow you to define a different set of values, beans or configuration sets for a handful of profiles (i.e. development, QA, production) and you can use them to run your app with different configurations or different libraries.

@Configuration @Profile("production") public class ProductionConfiguration {} 

This class will load all the values you set for that profile in your .properties or .yml file.

And the to run the app you can pass the profile you want to use as command line parameter

--spring.profiles.active=dev 

The configuration for your mariaDB will be under a particular profile.

For example (if you are using yml ):

spring:   profiles: QA   datasource:     url: jdbc:mariadb://qaserver:1234;databaseName=qaDatabase     username: USER     password: PASSWORD     driverClassName: org.mariadb.jdbc.Driver     //aditional mariadb properties, configs spring:   profiles: production   datasource:     url: jdbc:sqlserver://proddatabase:1433;databaseName=production     username: USER     password: PASSWORD     driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver 

Classes and properties belonging to other profiles will not be processed.

Answers 2

Automatically file system will be created in local path you have provided so you can restore the data while restarting/providing to qa.

-- Location of db files. delete this directory if you need to recreate from scratch

mariaDB4j.dataDir=./data/local 

Default is 3306, so using 3307 just in case it is already running on this machine

mariaDB4j.port=3307 app.mariaDB4j.databaseName=app_alpha spring.datasource.url=jdbc:mariadb://localhost:3307/ spring.datasource.username=root spring.datasource.password= spring.datasource.driver-class-name=org.mariadb.jdbc.Driver spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect 

you can refer this for more information https://objectpartners.com/2017/06/19/using-mariadb4j-for-a-spring-boot-embedded-database/

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment