How to get custom environment variable using the .properties file in Spring 4.1.9 (not Spring Boot)? - spring

I have created a custom environment variable as below in my environment-develop.properties file. I have set the value of
aws.access-key-id=${AWS_ACCESS_KEY_ID}
Trying to get the value as below:
System.out.println(environment.getProperty("aws.access-key-id"));
But this is not working. Error is:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'AWS_ACCESS_KEY_ID' in string value "${AWS_ACCESS_KEY_ID}"
How to obtain this value (set to abcd in Intellij's environment variable configuration)?

Related

gradle pass environment variable by file by console/terminal on macOS

I was running my Acceptance test using IntelliJ
Unfortunately sometimes I get this Exception in IntelliJ executing specific task as this:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'SOME_VARIABLE' in value "classpath:/${SOME_VARIABLE}/acceptance-test.properties"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
at org.springframework.core.env.AbstractEnvironment.resolveRequiredPlaceholders(AbstractEnvironment.java:630)
at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:461)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:280)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:207)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:175)
... 67 more
But, I would like to execute it using my terminal in macOS.
I know that, I can't to do it passing all variables as this manner!
SPRING_PROFILES_ACTIVE=xyz SPRING_CLOUD_CONFIG_URI=http://config-server EUREKA_SERVICE_URL=http=//eureka-server VAULT_PORT=8200 VAULT_AUTHENTICATION=TOKEN VAULT_TRUST_STORE_PWD=xxxxx VAULT_TRUST_STORE=file:/Users/joseluisbz/blabla.ts gradle app:acceptanceTest --stacktrace
Is it possible passing the same .env file in terminal?
Passing ENV values to gradle test is 2 layered
using ./gradle.properties or ~/.gradle/gradle.properties to setup the basic ENV variables. see https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties
in the test { } block setup map of env values using either systemProperties or environment as per your need. see https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html#org.gradle.api.tasks.testing.Test

Does packagesbuild support environment variable defined in the .pkgproj file?

I am trying to use packagesbuild to create packages in MacOS. I want to use generic template with environment variable so that I don't require to create separate pkgproj file for each app but packagesbuild is not able to recognize the environment variable defined in the template file.
After setting the environment variable in the terminal
export APP_PATH='/Users/sachin/Documents/Test/Client/Example.app'
and then using the same environment variable in example.pkgproj
...
<key>PATH</key>
<string>${APP_PATH}</string>
...
then triggering the command packagesbuild using the projectfile
packagesbuild example.pkgproj
getting below eror
ERROR:
Description:
Unable to copy item at path 'APP_PATH' to
'/private/tmp/S2EyTxCb/502/ExampleClient' because the item could not be
found
http://s.sudre.free.fr/Software/Packages/about.html

How to get Env specific yaml file inside java class in Mule 4

Hi I Have enviornment specific yaml files in my Mule Application . I need to read these file in my Java Class . My Yaml File name will be formed as
properties/${mule.env}/config-${mule.env}.yaml
So for each env it will load speicfic file. I need to get the respective env file in my java class . How i can do that . I tried like below in Java Class but its coming as null
#Value("${rixml.VersionID}")
private String version;
You can form the YAML filepath in your mule flow and then pass it as an argument to your java class.In your java class you can read the YAML file(from the file path passed to it), parse it and extract the value that you want.

logging.level.root doesn't work in springboot

I want to setting the default logging level to error on springboot
enter image description here
But the console still has the dubug and info output. It seems that logging.level.root=error doesn't work.
Be carefull if you are using Spring Boot Devtools, the properties defined in $HOME/.config/spring-boot folder will override all other properties as specified in Spring Boot documentation : https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html
I found a environment variable named debug,even though its value is a string not true,which caused the problem.Actually,I tried to remove the variable before,but I didn't restart the eclipse.Now,I remove the varibale named DEBUG and restart the eclipse,and it success.

Unable to create tempDir, java.io.tmpdir is set to C:\Windows\

I'm using Spring Boot with embedded tomcat, everything worked fine and suddenly I got the error :
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to create tempDir. java.io.tmpdir is set to C:\Windows\
at org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory.createTempDir(AbstractEmbeddedServletContainerFactory.java:183)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:165)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:164)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134)
... 11 common frames omitted
Caused by: java.io.IOException: Access is denied
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createTempFile(File.java:2024)
at java.io.File.createTempFile(File.java:2070)
at org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory.createTempDir(AbstractEmbeddedServletContainerFactory.java:174)
... 14 common frames omitted
I didn't do any manipulations with user or system variables.
My TEMP user variable is looking on C:/Users/me/AppData/Local/Temp , and I guess tomcat has to use this value insted of system one, which is actually C:/Windows/Temp
If you use IDEA check "Include parent environment variables" in Environment Variables window in the Run/Debug Configuration.
On Windows GetTempPathA is used to locate temp directory. Algorithm:
1. The path specified by the TMP environment variable.
2. The path specified by the TEMP environment variable.
3. The path specified by the USERPROFILE environment variable.
4. The Windows directory.
So if your app is started without TMP & TEMP & USERPROFILE defined you'll get java.io.tmpdir == c:\Windows (https://learn.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getwindowsdirectorya).
Typically applications set java.io.tmpdir inside app-run.bat (via -D...=...) or app.properties.
I hit this problem because Gradle Test task won't pass environment variables if environment properties aren't passed but replaced:
test {
environment = ["A": "1", "B": "2"] // won't work, because it replaces envs
}
test {
environment( ["A": "1", "B": "2"] ) // will work, because it appends to existing envs
}
I observed the following behaviour
changed all out of a sudden
works if run from commandline as self-contained jar
fails when run from IntelliJ (2018.1)
As a quick workaround i explicitly added -Djava.io.tmpdir=$EXISING_DIR_WITH_WRITE_ACCESS as JVM parameter in run configurations.
If you use eclipse check "Append to environment variables" in Environment Variables window in the Run/Debug Configuration.
Thanks to #max answer above
I was using JAVA EE eclipse - photon
In my case the problem occurred when I changed my default workspace library from [jre.1.8.0_121] to [jdk.1.8.0_121]. Setting it back to jre seems to have fixed the problem.

Resources