I’ve been trying to read a properties file and want it to be dynamic, I’m doing this in aws-cdk.
My project layout:
- Main Project
- resources
- config.properties
- src
- main/java/com/myorg
- xxxstage.java
- main/java/com/myorg
- resources
The class xxxstage.java has following code:
public class xxxstage extends Stage { public xxxstage(final Construct scope, final String id) { this(scope, id, null); } public xxxstage(final Construct scope, final String id, final StageProps props) { super(scope, id, props); String account = null; InputStream inputStream = null; try { Properties prop = new Properties(); String propFileName = "resources/config.properties"; inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName); System.out.println("inputStream is -> "+inputStream); if (inputStream != null) { prop.load(inputStream); } else { throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath"); } // get the property value and print it out account = prop.getProperty("account.id"); System.out.println("account id -> "+account); } catch (Exception e) { System.out.println("Exception: " + e); } finally { try{ inputStream.close(); } catch (Exception e){ System.out.println("Exception: " + e); } } new xxxStack(this, "xxxStack", StackProps.builder() .env(new Environment.Builder() .account(account) .region("us-east-1") .build()) .build()); } }
The line where I’m trying to print System.out.println("inputStream is -> "+inputStream);
is showing null, and hence the FileNotFoundException
.
It worth noting that this is working fine when I run it on local java-project, its however failing during build phase of the aws-codepipeline. In the pipeline build phase I’m getting:
- inputStream is -> null
- Exception: java.io.FileNotFoundException: property file ‘resources/config.properties’ not found in the classpath
- Exception: java.lang.NullPointerException
Can someone please help?
EDIT 1 – Adding environment variables:
Environment Vars
- PATH=/root/.npm/_npx/230/bin:/usr/local/bin/sbt/bin:/root/.phpenv/shims:/root/.phpenv/bin:/root/.goenv/shims:/root/.goenv/bin:/go/bin:/root/.phpenv/shims:/root/.phpenv/bin:/root/.pyenv/shims:/root/.pyenv/bin:/root/.rbenv/shims:/usr/local/rbenv/bin:/usr/local/rbenv/shims:/root/.dotnet/:/root/.dotnet/tools/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/tools:/codebuild/user/bin
- AWS_CONTAINER_CREDENTIALS_RELATIVE_URI=/v2/credentials/xxx-xxx-xxx
- CODEBUILD_CI=true
- CODEBUILD_AUTH_TOKEN=xxx-xxx-xxx
- JAVA_8_HOME=/usr/lib/jvm/java-1.8.0-amazon-corretto
- JDK_8_HOME=/usr/lib/jvm/java-1.8.0-amazon-corretto
- CODEBUILD_BUILD_ARN=arn:aws:codebuild:us-east-1:821518525729:build/pipelinePipelinexxx-C-xxx-xxx-xxx
- CODEBUILD_GOPATH=/codebuild/output/src123245
- GOLANG_15_VERSION=1.15.12
- CODEBUILD_BUILD_SUCCEEDING=1
- GOENV_DISABLE_GOPATH=1
- JRE_HOME=/usr/lib/jvm/java-11-amazon-corretto
- CDK_DEFAULT_REGION=us-east-1
- JAVA_11_HOME=/usr/lib/jvm/java-11-amazon-corretto
- PHP_74_VERSION=7.4.13
- CODEBUILD_SOURCE_VERSION=arn:aws:s3:::pipelinexxxstack-pipelinexxxartifacts-1cacuj92rramf/ServiceDeploymentPip/(user)/c1BGqPX
- RUBY_BUILD_SRC_DIR=/usr/local/rbenv/plugins/ruby-build
- JDK_HOME=/usr/lib/jvm/java-11-amazon-corretto
- PWD=/codebuild/output/src12345/src
- CODEBUILD_CONTAINER_NAME=default
- PYTHON_37_VERSION=3.7.10
- CODEBUILD_START_TIME=1642556874326
- CDK_DEFAULT_ACCOUNT=12345
- AWS_REGION=us-east-1
- PYTHON_38_VERSION=3.8.10
- CODEBUILD_BUILD_URL=https://us-east-1.console.aws.amazon.com/codebuild/home?region=us-east-1#/builds/pipelinePipelinexxxSynthC-ofdfmXGrWl5m:xxx-xxx-xxx/view/new
- CDK_OUTDIR=cdk.out
- DOTNET_31_SDK_VERSION=3.1.404
- CODEBUILD_BUILD_ID=pipelinePipelinexxxSynthC-ofdfmXGrWl5m:xxx-xxx-xxx GOPATH=/go:/codebuild/output/src12345
- CODEBUILD_RESOLVED_SOURCE_VERSION=xxx-xxx-xxx
- OLDPWD=/codebuild/output/src12345/src
- RUBY_26_VERSION=2.6.6
- AWS_STS_REGIONAL_ENDPOINTS=regional
- DOTNET_ROOT=/root/.dotnet
- _PROJECT_CONFIG_HASH=xxx-xxx-xxx
- CODEBUILD_AGENT_ENDPOINT=http://127.0.0.1:port
- LC_CTYPE=C.UTF-8
- JRE_8_HOME=/usr/lib/jvm/java-1.8.0-amazon-corretto/jre
- CODEBUILD_BUILD_IMAGE=aws/codebuild/standard:5.0
- PYYAML_VERSION=5.4.1
- FORCE_COLOR=0
- CODEBUILD_BMR_URL=https://CODEBUILD_AGENT:port
- JAVA_HOME=/usr/lib/jvm/java-11-amazon-corretto
- CODEBUILD_SRC_DIR=/codebuild/output/src12345/src
- AWS_DEFAULT_REGION=us-east-1
- AWS_EXECUTION_ENV=AWS_ECS_EC2
- ECS_CONTAINER_METADATA_URI=http://169.254.170.2/v3/xxx-xxx-xxx
- ECS_CONTAINER_METADATA_URI_V4=http://169.254.170.2/v4/xxx-xxx-xxx
- CODEBUILD_INITIATOR=codepipeline/ServicexxxPipeline
- MAVEN_OPTS= -Dmaven.wagon.httpconnectionManager.maxPerRoute=2
- CDK_CONTEXT_JSON={“@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId”:true,”@aws-cdk/core:enableStackNameDuplicates”:true,”aws-cdk:enableDiffNoFail”:true,”@aws-cdk/core:stackRelativeExports”:true,”@aws-cdk/aws-ecr-assets:dockerIgnoreSupport”:true,”@aws-cdk/aws-secretsmanager:parseOwnedSecretName”:true,”@aws-cdk/aws-kms:defaultKeyPolicies”:true,”@aws-cdk/aws-s3:grantWriteWithoutAcl”:true,”@aws-cdk/aws-ecs-patterns:removeDefaultDesiredCount”:true,”@aws-cdk/aws-rds:lowercaseDbIdentifier”:true,”@aws-cdk/aws-efs:defaultEncryptionAtRest”:true,”@aws-cdk/aws-lambda:recognizeVersionProps”:true,”@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021″:true,”@aws-cdk/core:newStyleStackSynthesis”:true,”aws:cdk:enable-path-metadata”:true,”aws:cdk:enable-asset-metadata”:true,”aws:cdk:version-reporting”:true,”aws:cdk:bundling-stacks”:[]}
- CODEBUILD_LOG_PATH=xxx-xxx-xxx
- CODEBUILD_EXECUTION_ROLE_BUILD=
- CODEBUILD_BUILD_NUMBER=31
- GOLANG_16_VERSION=1.16.4
- PHP_73_VERSION=7.3.25
- CODEBUILD_FE_REPORT_ENDPOINT=https://codebuild.us-east-1.amazonaws.com/
- CODEBUILD_LAST_EXIT=0
- AWS_NODEJS_CONNECTION_REUSE_ENABLED=1
- MAVEN_CMD_LINE_ARGS= -e -q compile exec:java
- NUGET_XMLDOC_MODE=skip
- DOTNET_5_SDK_VERSION=5.0.202
- NODE_12_VERSION=12.22.2
- PYTHON_39_VERSION=3.9.5
- CDK_CLI_VERSION=2.8.0
- NODE_14_VERSION=14.17.2
- MAVEN_PROJECTBASEDIR=/codebuild/output/src12345/src
- CDK_CLI_ASM_VERSION=16.0.0
- JRE_11_HOME=/usr/lib/jvm/java-11-amazon-corretto
- RUBY_27_VERSION=2.7.2
- HOSTNAME=12345
- JDK_11_HOME=/usr/lib/jvm/java-11-amazon-corretto
- CODEBUILD_PROJECT_UUID=xxx-xxx-xxx
- PHP_80_VERSION=8.0.0
- CODEBUILD_KMS_KEY_ID=arn:aws:kms:us-east-1:(account-num):alias/aws/s3
- HOME=/root
Advertisement
Answer
I found an answer here and it worked for me. The location of properties file matters.