0

I have vault integrated with my spring-boot application and I am facing I guess a very weird issue. When I run my application using

java -Dspring.profiles.active=dev -jar BatchProcessing-0.0.1-SNAPSHOT.jar 

runs successfully but when I use

mvn spring-boot:run 

I get the below exception.

Can anyone please give me a brief description of what can be the difference between the two? Thank You!

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'vaultTemplate' defined in class path resource [org/springframework/cloud/vault/config/VaultBootst
rapConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.vault.co
re.VaultTemplate]: Factory method 'vaultTemplate' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'vaultSession
Manager' defined in class path resource [org/springframework/cloud/vault/config/VaultBootstrapConfiguration.class]: Unsatisfied dependency expressed through method 'vaultSessionManager' parameter
0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientAuthentication' defined in class path resource [org/springframework/cloud/vault
/config/VaultBootstrapConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.sprin
gframework.vault.authentication.ClientAuthentication]: Factory method 'clientAuthentication' threw exception; nested exception is java.lang.IllegalArgumentException: Token (spring.cloud.vault.toke
n) must not be empty
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:484) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.5.RELEASE.jar
:5.2.5.RELEASE]
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
Niko2510
  • 25
  • 1
  • 5
  • How are you passing `dev` to be the active profile when using `mvn spring-boot:run`? I suppose you are _not_ using the `dev` profile correctly and hence _not_ specifying `spring.cloud.vault.token` ? – wjans Jun 05 '20 at 15:11
  • I am using -Dspring.profiles.active=dev when using mvn spring-boot:run command. I have specified spring.cloud.token in my bootstrap.yml file. It works completely fine when using Java command and I am able to get response for my services. – Niko2510 Jun 05 '20 at 15:15
  • Maybe add it to your question along with the version of Spring Boot you are using (I assume it's Spring Boot 2.X based on the versions from your stacktrace). – wjans Jun 05 '20 at 15:28

1 Answers1

1

If you want to activate a profile when running with the Maven Plugin you must pass the profile like this:

mvn spring-boot:run -Dspring-boot.run.profiles=dev
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • Thank you for your answer but it doesn't work. Gives the same error. Even if I dont set the profile I get the same error so I guess the issue is with the maven command. – Niko2510 Jun 05 '20 at 15:20
  • I've updated the answer because Spring chnaged the property. Can you try it with this command line? – Simon Martinelli Jun 05 '20 at 15:26
  • This time only one line of error saying "Parameter 0 of method restTemplate in com.optum.omms.OmmsProvApiApplication required a bean of type 'org.springframework.vault.client.RestTemplateBuilder' that could not be found." – Niko2510 Jun 05 '20 at 15:36
  • I created the Bean in the main class and it worked! Can you please give a short discription of what is the difference between the two commands? Java vs maven – Niko2510 Jun 05 '20 at 15:39
  • Can you show some code and the stacktrace – Simon Martinelli Jun 05 '20 at 15:40
  • I created the Bean in the main class and worked. I wanted to know if you can explain what is the difference between using java or maven command – Niko2510 Jun 05 '20 at 15:49
  • The difference is that spring-boot:run runs the compiled code and not the repackaged fat jar. Plus it is implemented as a Maven plugin so you have to set the profiles in a way that the Maven plugin gets this configuration – Simon Martinelli Jun 06 '20 at 09:17
  • @Niko2510 The actual difference lies in the fact that you don't pass the profile correctly when using maven. Specifying `-Dspring.profiles.active=dev` when using maven causes this property to be set for the maven java process rather than the forked spring boot process. As stated in this answer you should pass it differently so that it gets propagated to your actual spring boot java process. – wjans Jun 06 '20 at 13:10