Graal本机映像中缺少属性文件

问题描述

我的应用程序打包在Graal本机映像中。
我正在使用以下属性加载

InputStream resourceAsstream = MainApplication.class.getResourceAsstream("/application.properties");

但是,当我尝试执行二进制文件时,出现以下错误

Exception in thread "main" java.lang.NullPointerException: inStream parameter is null

事实证明,将项目打包到本机映像中后,Graal不会附加我的application.properties文件

我正在使用Gradle和com.palantir.graal进行以下设置:

graal {
    mainClass '<path-to-main-class>'
    outputName '<output-name>'
    javaVersion '11'
}

是否可以使用application.properties中的build/resources

解决方法

要使资源包含在本机映像可执行文件中,需要对其进行配置。静态分析不能自动包括它们。

docs中描述了资源的配置。

简而言之,它是一个描述要包含的资源的json文件,例如:

 {
  "type": "service_account","project_id": "project-name...","private_key_id": "12f.....................","private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANB...........=\n-----END PRIVATE KEY-----\n","client_email": "firebase-adminsdk-...@domain.iam.gserviceaccount.com","client_id": "10651....","auth_uri": "https://accounts.google.com/o/oauth2/auth","token_uri": "https://oauth2.googleapis.com/token","auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-...w%40project-name.iam.gserviceaccount.com"
}

创建这样的配置的最佳方法可能是使用Javaagent在JVM上运行应用程序,该Javaagent会跟踪需要配置的所有内容并输出可包含在本机映像构建过程中的配置。

the docs中描述了辅助配置Javaagent,但是简而言之,您可以像这样运行应用程序:

{
  "resources": {
    "includes": [
      {"pattern": "<Java regexp that matches resource(s) to be included in the image>"},{"pattern": "<another regexp>"},...
    ],"excludes": [
      {"pattern": "<Java regexp that matches resource(s) to be excluded from the image>"},...
    ]
  } 
}

然后使用该应用执行以编写代理将跟踪的路径的代码。该代理不会静态分析应用程序,因此需要使用它来提供有意义的配置。