英文:
How do you read appSettings variables from azure-maven-plugin in Java code?
问题
有没有办法在Java代码中读取<appSettings>
属性值?
我尝试使用System.getProperty
,但它没有设置。
这不是一个Spring Boot项目。
有没有办法在azure-maven-plugin中设置环境变量?
我的azure-maven-plugin配置如下:
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<version>${azure.functions.maven.plugin.version}</version>
<configuration>
<!-- function app name -->
<appName>${functionAppName}</appName>
<!-- function app resource group -->
<resourceGroup>${resourceGroupName}</resourceGroup>
<!-- function app service plan name -->
<appServicePlanName>${appServicePlanName}</appServicePlanName>
<!-- function app region-->
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supported-regions for all valid values -->
<region>${azure.region}</region>
<!-- function pricingTier, default to be consumption if not specified -->
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supported-pricing-tiers for all valid values -->
<!-- <pricingTier></pricingTier> -->
<!-- Whether to disable application insights, default is false -->
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details for all valid configurations for application insights-->
<!-- <disableAppInsights></disableAppInsights> -->
<runtime>
<!-- runtime os, could be windows, linux or docker-->
<os>Linux</os>
<javaVersion>11</javaVersion>
<!-- for docker function, please set the following parameters -->
<!-- <image>[hub-user/]repo-name[:tag]</image> -->
<!-- <serverId></serverId> -->
<!-- <registryUrl></registryUrl> -->
</runtime>
<appSettings>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>~4</value>
</property>
</appSettings>
</configuration>
<executions>
<execution>
<id>package-functions</id>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
英文:
Is there a way to read the <appSettings>
property values in java code ?
https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details
I have tried to use System.getProperty
but it is not set.
This is not a Spring Boot project.
Is there a way to set environment variables in the azure-maven-plugin ?
My azure-maven-plugin is the following:
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<version>${azure.functions.maven.plugin.version}</version>
<configuration>
<!-- function app name -->
<appName>${functionAppName}</appName>
<!-- function app resource group -->
<resourceGroup>${resourceGroupName}</resourceGroup>
<!-- function app service plan name -->
<appServicePlanName>${appServicePlanName}</appServicePlanName>
<!-- function app region-->
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supported-regions for all valid values -->
<region>${azure.region}</region>
<!-- function pricingTier, default to be consumption if not specified -->
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supported-pricing-tiers for all valid values -->
<!-- <pricingTier></pricingTier> -->
<!-- Whether to disable application insights, default is false -->
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details for all valid configurations for application insights-->
<!-- <disableAppInsights></disableAppInsights> -->
<runtime>
<!-- runtime os, could be windows, linux or docker-->
<os>Linux</os>
<javaVersion>11</javaVersion>
<!-- for docker function, please set the following parameters -->
<!-- <image>[hub-user/]repo-name[:tag]</image> -->
<!-- <serverId></serverId> -->
<!-- <registryUrl></registryUrl> -->
</runtime>
<appSettings>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>~4</value>
</property>
</appSettings>
</configuration>
<executions>
<execution>
<id>package-functions</id>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
答案1
得分: 1
在Azure Function App中,这些应用程序设置被设置为环境变量。因此,在Java中,您可以使用System.getenv()而不是System.getProperty()来访问这些设置。
下面是如何为FUNCTIONS_EXTENSION_VERSION设置执行此操作的示例代码:
String functionsExtensionVersion = System.getenv("FUNCTIONS_EXTENSION_VERSION");
System.out.println("FUNCTIONS_EXTENSION_VERSION: " + functionsExtensionVersion);
当您在azure-functions-maven-plugin中定义设置时,这些设置将在部署过程中应用到Azure。因此,当您在本地运行函数时,您在Maven配置中定义的设置不会自动应用到您的本地环境。您可以在本地计算机上手动设置环境变量,或者如果您使用Azure Functions Core Tools或Visual Studio/VS Code Azure Functions扩展,您可以手动将所需的应用程序设置(环境变量)添加到local.settings.json文件中。
您可以使用Azure CLI在部署Azure函数后设置应用程序配置值。
az functionapp config appsettings set --name acmefunction2 --resource-group acmeapp --settings FUNCTIONS_WORKER_RUNTIME=node WEBSITE_RUN_FROM_PACKAGE=1 SCM_DO_BUILD_DURING_DEPLOYMENT=true
英文:
In an Azure Function App, these application settings are set as environment variables. Therefore, in Java, you can access these settings using System.getenv() rather than System.getProperty().
Here's how you would do it for the FUNCTIONS_EXTENSION_VERSION setting:
String functionsExtensionVersion = System.getenv("FUNCTIONS_EXTENSION_VERSION");
System.out.println("FUNCTIONS_EXTENSION_VERSION: " + functionsExtensionVersion);
When you define settings in the azure-functions-maven-plugin, those settings are applied during the deployment process to Azure. Therefore, when you're running the function locally, the settings you've defined in the Maven configuration won't automatically apply to your local environment. You can either manually set the environment variables on your local machine or If you're using the Azure Functions Core Tools or the Visual Studio/VS Code Azure Functions extension, you can manually add the required application settings (environment variables) to the local.settings.json file
You can use azure cli to set the app configuration value after deploying azure function.
az functionapp config appsettings set --name acmefunction2 --resource-group acmeapp --settings FUNCTIONS_WORKER_RUNTIME=node WEBSITE_RUN_FROM_PACKAGE=1 SCM_DO_BUILD_DURING_DEPLOYMENT=true
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论