0

I explain my problem;

I have a web app developed using Vue.js and Spring Boot, this application working a PDF sheet and saves the file that is generated by Java, I use two lines of code to separate my development part from the production part (I leave you the 2 lines of code like this you understand the concept well)

FileReader leggoFile = new FileReader(System.getProperty("user.dir") + "/temp/webapps/foolder/foolder/file.pdf");

// FileReader leggoFile = new FileReader(System.getProperty("catalina.base") + "/temp/webapps/foolder/foolder/file.pdf");

This whole application is built using the "bootWar gradle plugin" which returns me a .war which I will then upload to a Tomcat server;

My goal is this: I would like to set a single environment variable so that if I want to build the project I don't have to comment/uncomment that line for example:

FileReader leggoFile = new FileReader({{variableEnvironment}} + "/temp/webapps/foolder/foolder/file.pdf")

my question is this: How dp Gradle and Spring Boot handle environments? Is there a way to separate environments? Is this possible or should I start thinking differently?

I tried to search on something but unfortunately I was tied to the problem that I don't understand how the .war file is generated through the BootWar Gradle plugin, also searching on the internet I understood that environment Gradle and environment Spring are two separate things but in general even if I know the line of code is wrong in the beginning my question is always the same:

How are environment variables handled in Spring and Gradle?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • The term you're looking for, for Spring and Spring Boot, is profiles. – Mark Rotteveel Feb 17 '23 at 08:48
  • Does this answer your question? [Maven profiles equivalent of Gradle](https://stackoverflow.com/questions/40659986/maven-profiles-equivalent-of-gradle) – life888888 Feb 17 '23 at 09:00
  • Ok, I tried to use the yaml option but it doesn't work, I saw the various comments on this post and no, I expressed myself badly sorry for wasting your time delete this post thanks again – lorenzo cavalieri Feb 17 '23 at 09:24

1 Answers1

1

With Spring Boot, you can add properties to your application by adding an file named application.yaml to your resources folder (src/resources/). In addition you can add properties through application-{profile}.yaml to add properties only for given Spring profiles. For instance application-test.yaml would only be read if "test" is an active profile. When booting up the application, Spring will first read application.yaml, then any profile-specific YAML-files, such that any overlapping properties are replaced.

There are several approaches to injecting the property. A simple solution is to add a field to your component annotated with @Value("${PATH}) and replace PATH with the property's path in the YAML.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ODDminus1
  • 544
  • 1
  • 3
  • 11