0

I am developping a blueprint for JHipster. For now, I prompt the user for the database URL, username and password. I want to then take the user's answers and add it to application-dev.yml.

What I want :

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    url: userURL
    username: userUsername
    password: userPassword
    hikari:
      poolName: Hikari
      auto-commit: false

I am already able to get the intended behavior, but that implies that I override the whole JHipster template which is at https://github.com/jhipster/generator-jhipster/blob/main/generators/server/templates/src/main/resources/config/application-dev.yml.ejs

I saw on the JHipster website that you can add application properties by the ApplicationProperties class. Though my supervisor (I am an intern) told me not to go that route.

My thinking is that JHipster is changing constantly and the template for application-dev.yml is prone to change often. I would rather use a function or an algorithm so that when they update JHipster, I get the changes too.

Any ideas or solutions to this particular issue?

Gaël Marziou
  • 16,028
  • 4
  • 38
  • 49

2 Answers2

1

I would define a new Spring profile (e.g. mycompany) and then add all the application properties that I want to override to an application-mycompany.yml file. Then, you must pass the additional profile to the app either from your java IDE or your build tool (maven or gradle).

This is more the Spring way of doing and is completely independent from JHipster.

https://www.baeldung.com/spring-profiles

Gaël Marziou
  • 16,028
  • 4
  • 38
  • 49
0

There might be better solutions out there, but this is the algorithm I came up with in order to solve it:

injectDBInfos() {
        const buffer = fs.readFileSync('src/main/resources/config/application-dev.yml'); 
        const data = buffer.toString(); 
        let startIndex = data.indexOf('url:'); 
        let endIndex = data.indexOf('hikari:', startIndex); 
        let toBeReplaced = '';
        for(let i = startIndex; i < endIndex; i++) {
            toBeReplaced += data[i];
        }
        let replacement = 'url: ' + this.devDatabaseURL + '\n\t\tusername: ' + this.devDatabaseUsername + '\n\t\tpassword: ' +  this.devDatabasePassword + '\n\t\t'; 
        const newData = data.replace(toBeReplaced, replacement); // String voulu
        fs.writeFile('src/main/resources/config/application-dev.yml', 
                      newData, 
                      'utf-8', 
                      (err) => { 
                      if (err) throw err;
                      this.log('The file has been saved!');
                      }
        )
}