Home > Back-end >  In local, environment variable passed with IntelliJ is not working in spring boot batch
In local, environment variable passed with IntelliJ is not working in spring boot batch

Time:08-24

I am using Spring Boot Gradle plugin and developing in Intellij on Mac,

I'm trying to get value passed with environment variable but can not able to get it, (Passing 3 variables, among them only one systemDate is not able to access).

Environment variable settings in Intellij are as below. enter image description here

I had also tried to set it as a Override configuration properties in IntelliJ as below, enter image description here

Code where I am accessing it is as below.

String systemDate = Objects
        .toString(chunkContext.getStepContext().getJobParameters().get("systemDate"), "");

※Same code is working in staging environment. In staging I am passing systemDate like below,

java -Dspring.profiles.active=stg -Dspring.batch.job.names=abcPaymentJob -jar /var/lib/jenkins/deploy/stg/newAbcBatch.jar systemDate=2022-10-20T10:15:30

CodePudding user response:

You are setting those key/value pairs as environment variables but you are trying to get them as job parameters here:

chunkContext.getStepContext().getJobParameters()

Job parameters and environment variables are two different things. You either need to:

  • pass those key/value pairs as job parameters. On the command line those should be passed like jar -jar myjob.jar systemDate=2022-10-20T10:15:30. In IntelliJ, the equivalent is to pass them as "Program arguments" under the "Run/Debug Configuration -> Build and run" section
  • or update your code to get values from the environment with System.getenv("systemDate"); or via Spring's environment support
  • Related