aws-cdk 中属性文件的 FileNotFoundException
FileNotFoundException for properties file in aws-cdk
我一直在尝试读取属性文件并希望它是动态的,我在 aws-cdk 中这样做。
我的项目布局:
- 主要项目
- 资源
- config.properties
- 来源
- main/java/com/myorg
- xxxstage.java
classxxxstage.java有如下代码:
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());
}
}
我尝试打印 System.out.println("inputStream is -> "+inputStream);
的行显示为空,因此 FileNotFoundException
.
值得注意的是,当我 运行 它在本地 java-project 上时它工作正常,但是它在 aws-codepipeline 的构建阶段失败了。
在管道构建阶段,我得到:
- inputStream is -> null
- Exception: java.io.FileNotFoundException: property file 'resources/config.properties' not found in the classpath
- Exception: java.lang.NullPointerException
有人可以帮忙吗?
编辑 1 - 添加环境变量:
环境变量
- 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=真
- 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-亚马逊-corretto
- CDK_DEFAULT_REGION=us-east-1
- JAVA_11_HOME=/usr/lib/jvm/java-11-亚马逊-corretto
- PHP_74_VERSION=7.4.13
- CODEBUILD_SOURCE_VERSION=arn:aws:s3:::pipelinexxxstack-pipelinexxxartifacts-1cacuj92rramf/ServiceDeploymentPip/(用户)/c1BGqPX
- RUBY_BUILD_SRC_DIR=/usr/local/rbenv/plugins/ruby-build
- JDK_HOME=/usr/lib/jvm/java-11-亚马逊-corretto
- PWD=/codebuild/output/src12345/src
- CODEBUILD_CONTAINER_NAME=默认值
- 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=地区
- DOTNET_ROOT=/root/.dotnet
- _PROJECT_CONFIG_HASH=xxx-xxx-xxx
- CODEBUILD_AGENT_ENDPOINT=http://127.0.0.1:端口
- 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:端口
- JAVA_HOME=/usr/lib/jvm/java-11-亚马逊-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 编译 exec:java
- NUGET_XMLDOC_MODE=跳过
- 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-亚马逊-corretto
- RUBY_27_VERSION=2.7.2
- 主机名=12345
- JDK_11_HOME=/usr/lib/jvm/java-11-亚马逊-corretto
- CODEBUILD_PROJECT_UUID=xxx-xxx-xxx
- PHP_80_VERSION=8.0.0
- CODEBUILD_KMS_KEY_ID=arn:aws:kms:us-east-1:(账号):alias/aws/s3
- HOME=/root
我找到了答案 ,它对我有用。属性文件的位置很重要。
我一直在尝试读取属性文件并希望它是动态的,我在 aws-cdk 中这样做。
我的项目布局:
- 主要项目
- 资源
- config.properties
- 来源
- main/java/com/myorg
- xxxstage.java
- main/java/com/myorg
- 资源
classxxxstage.java有如下代码:
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());
}
}
我尝试打印 System.out.println("inputStream is -> "+inputStream);
的行显示为空,因此 FileNotFoundException
.
值得注意的是,当我 运行 它在本地 java-project 上时它工作正常,但是它在 aws-codepipeline 的构建阶段失败了。 在管道构建阶段,我得到:
- inputStream is -> null
- Exception: java.io.FileNotFoundException: property file 'resources/config.properties' not found in the classpath
- Exception: java.lang.NullPointerException
有人可以帮忙吗?
编辑 1 - 添加环境变量:
环境变量
- 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=真
- 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-亚马逊-corretto
- CDK_DEFAULT_REGION=us-east-1
- JAVA_11_HOME=/usr/lib/jvm/java-11-亚马逊-corretto
- PHP_74_VERSION=7.4.13
- CODEBUILD_SOURCE_VERSION=arn:aws:s3:::pipelinexxxstack-pipelinexxxartifacts-1cacuj92rramf/ServiceDeploymentPip/(用户)/c1BGqPX
- RUBY_BUILD_SRC_DIR=/usr/local/rbenv/plugins/ruby-build
- JDK_HOME=/usr/lib/jvm/java-11-亚马逊-corretto
- PWD=/codebuild/output/src12345/src
- CODEBUILD_CONTAINER_NAME=默认值
- 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=地区
- DOTNET_ROOT=/root/.dotnet
- _PROJECT_CONFIG_HASH=xxx-xxx-xxx
- CODEBUILD_AGENT_ENDPOINT=http://127.0.0.1:端口
- 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:端口
- JAVA_HOME=/usr/lib/jvm/java-11-亚马逊-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 编译 exec:java
- NUGET_XMLDOC_MODE=跳过
- 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-亚马逊-corretto
- RUBY_27_VERSION=2.7.2
- 主机名=12345
- JDK_11_HOME=/usr/lib/jvm/java-11-亚马逊-corretto
- CODEBUILD_PROJECT_UUID=xxx-xxx-xxx
- PHP_80_VERSION=8.0.0
- CODEBUILD_KMS_KEY_ID=arn:aws:kms:us-east-1:(账号):alias/aws/s3
- HOME=/root
我找到了答案