0

I have a pretty simple Springboot project that taps into the DAO with a local in-memory H2 database if running JUnit. I have 2 configuration files, application-local.properties and application-TEST.properties. There is a DataSourceProvider class that has property holders such as:

@Value("${spring.datasource.schema}")
String dbSchema;

@Value("${spring.datasource.url}")
String dbUrl;

...

These properties get loaded perfectly fine when running the SpringBoot main class and hitting an endpoint and when running the DAO layer JUnit test, however when I do mvn clean install, it's telling me:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.datasource.schema' in value "${spring.datasource.schema}"

I've spent awhile now and I can't figure out why this is happening?

stackerstack
  • 243
  • 4
  • 16
  • Hard to say without the full picture. But you probably need to tie your profiles (local, test) to your build config in pom.xml. – AlgorithmFromHell Jul 19 '21 at 20:41
  • did you specify the desired profile when running the mvn command ? something like `mvn .... -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=local"` – Abdelghani Roussi Jul 19 '21 at 20:43
  • you could also remove the profiles all together, and change application-TEST.properties to application.properties and put it under src/test/resources... Or like @AbdelghaniRoussi call your profile on build. – JCompetence Jul 19 '21 at 20:44
  • @AbdelghaniRoussi there is another repo i worked on that had two different propertie file names just like the ones mentioned above, and i did not need to pass in any arguments for the spring profiles. – stackerstack Jul 19 '21 at 20:56

1 Answers1

1

usually people create properties file like this pattern application-{profile}.properties for different profiles and then they tie profiles (local, test) to their build config in pom.xml as @AlgorithmFromHell said. you still can have different properties files in your spring boot but you have to load them manually in order to use them like this:

@PropertySource({
        "classpath:application-TEST.properties", 
        "classpath:application-local.properties",
        "classpath:application.properties"
})

update

I add this part because of the comment section

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>-Dvar1=value1 -Dvar2=value2</argLine>
    </configuration>
</plugin>
badger
  • 2,908
  • 1
  • 13
  • 32
  • So this worked for the most part, but there is a value called ```dbPass``` and inside my application-local file, i have it set as dbPass=${oraclePass}, where i have defined oraclePass in my environments tab in my run configs. I plan on having this pass as a key in a config map for deployment use, anyway I can have mvn clean install working with this in mind? – stackerstack Jul 20 '21 at 01:02
  • 1
    @stackerstack if you define `oraclePass` as a vm option like `-DoraclePass=mypassword` you are able to access its value in `application-local.properties` for example like `dbpass=${oraclePass}` and then inject `dbpass` value(=mypassword) as usual. I think this link be useful for you https://maven.apache.org/configure.html – badger Jul 20 '21 at 07:04
  • Yeah, so I have oraclePass defined in the "Environment" tab of the runconfigs, which I believe is the same thing as manually passing it in as a vm option. And then I have a ```dbPass=${oraclePass}``` in my application properties file and then inject it via @Value. However running a mvn clean install catches an error on this password: ``` Could not resolve placeholder "oraclePassword" in value "${oraclePassword}" ``` – stackerstack Jul 20 '21 at 13:43
  • @stackerstack I think you are using Eclipse for development. I don't know what environment means in Eclipse take [a look at this](https://stackoverflow.com/questions/10639322/how-can-i-specify-the-default-jvm-arguments-for-programs-i-run-from-eclipse) . for deployment you only have Maven and there are several approaches that allow you to set vm options for Maven one we use usually is setting/exporting `MAVEN_OPTS` variable – badger Jul 20 '21 at 14:13
  • @stackerstack do `mvn clean install` like usual if you want to run your spring boot from command line do this `mvn -Dspring-boot.run.jvmArguments="-Ddbpass=password -Dvar1=value1 -Dvar2=value2" spring-boot:run` – badger Jul 20 '21 at 14:40
  • yeah i know about running springboot frmo command line, im saying that this error is getting caught when i do ```mvn clean install``` – stackerstack Jul 20 '21 at 14:51
  • no idea mate! my application give the above error when I do `mvn clean install -DskipTests` (clean and install are successful) and then `mvn spring-boot:run`(here give me the `could not resolve place holder` error) i.e. give me error in run time. and in order to solve that I have to run spring boot using jvmarguments – badger Jul 20 '21 at 15:20
  • So when i do ```mvn clean install -DskipTests```, it builds fine, but if I do not exclude tests, then it fails – stackerstack Jul 20 '21 at 15:54
  • @stackerstack I update the answer and provide a work around for this problem, but I don't know it is best practice or not. hope it helps you – badger Jul 20 '21 at 16:52