Home > database >  Running 'mvn clean install' on Spring Boot application using env variables in application.
Running 'mvn clean install' on Spring Boot application using env variables in application.

Time:09-02

Hello I am trying to package my Spring Boot app into a jar. I want to deploy this app to AWS Beanstalk and so I will be injecting some variables into application.properties using Environment variables.

spring.data.mongodb.uri=${MONGODB_URI}
spring.data.mongodb.auto-index-creation=true
spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1
CLOUDINARY_URL=${CLOUDINARY_URL}
jwt-secret=${JWT_SECRET}
server.port=5000

However when I run the maven command (mvn clean install), during the package process the code is executed and it is failing stating that

BeanCreationException: Error creating bean with name 'customBeansConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'CLOUDINARY_URL' in value "${CLOUDINARY_URL}"

I have a class CustomBeansConfig:

@Configuration
public class CustomBeansConfig {

    @Value("${CLOUDINARY_URL}")
    private String cloudinaryUrl;

    @Bean
    public Cloudinary cloudinary(){
        System.out.println("cloudinaryUrl = "   cloudinaryUrl);
        return new Cloudinary(cloudinaryUrl);
    }
}

Please help me to create the jar file

CodePudding user response:

If I have understood you correctly, one approach may be to use different application.properties files for different environments. For example application-dev.properties for the Dev environment and application-prod.properties for the Prod environment. Then your CLOUDINARY_URL may be assigned different literal values appropriate to each. Then when deploying to each environment, bundle your JAR with the -Denv option, as in

mvn -Denv=dev clean install

OR

mvn -Denv=prod clean install

... and upload the resulting JAR file to the corresponding AWS environment.

CodePudding user response:

Running the Spring Boot application with a such config property, got me the following error:

Caused by: java.lang.IllegalArgumentException: Circular placeholder reference 'CLOUDINARY_URL' in property definitions

Changing the name of your Spring property from CLOUDINARY_URL to, for example, cloudinary.service.url will resolve the issue.

In such case, your config file should look like this:

spring.data.mongodb.uri=${MONGODB_URI}
spring.data.mongodb.auto-index-creation=true
spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1
cloudinary.service.url=${CLOUDINARY_URL}
jwt-secret=${JWT_SECRET}
server.port=5000

And your configuration file like this:

@Configuration
public class CustomBeansConfig {

    @Value("${cloudinary.service.url}")
    private String cloudinaryUrl;

    @Bean
    public Cloudinary cloudinary(){
        System.out.println("cloudinaryUrl = "   cloudinaryUrl);
        return new Cloudinary(cloudinaryUrl);
    }
}

Also, I would advise you to avoid creating Spring configuration properties using the underscore format, since it usually used for the environment variables, maybe be confusing and may cause such interesting issues.

  • Related