java - 如何在 Java 代码中从 azure-maven-plugin 读取 appSettings 变量?

标签 java azure maven azure-functions

有没有办法读取 <appSettings> java 代码中的属性值?

https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details

我尝试使用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>

最佳答案

在 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 核心工具或 Visual Studio/VS Code Azure Functions 扩展,则可以手动将所需的应用程序设置(环境变量)添加到本地。 settings.json 文件

部署azure函数后,您可以使用azure cli设置应用配置值。

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

关于java - 如何在 Java 代码中从 azure-maven-plugin 读取 appSettings 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76862805/

相关文章:

c# - Azure Function 服务总线触发器在部署后不起作用

java - 无法找到或加载主类: Executable JAR from Maven

java - 为什么 java.nio.Paths.get(..) 不将 java.nio.Path 对象作为输入?

java - 分割图像并获取坐标

authentication - 移动客户端上的 Aspnet Web API 身份验证

java - 示例 "mavenproject"netbeans 7.4 的包 javafx 不存在

java - 在Docker中构建Maven项目时,所有目标文件都是root的

java - 在NoSQL系统中,例如每个Lesson有很多Exercise,在插入Exercise之前我们需要检查Lesson是否存在两次吗?

java - 如何更改线程执行顺序,使最后创建的线程先运行?

sql-server - 在 Azure 自动化 powershell 脚本中从 SQL 打印消息不起作用