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

huangapple go评论113阅读模式
英文:

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 &lt;appSettings&gt; 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:

       &lt;plugin&gt;
                &lt;groupId&gt;com.microsoft.azure&lt;/groupId&gt;
                &lt;artifactId&gt;azure-functions-maven-plugin&lt;/artifactId&gt;
                &lt;version&gt;${azure.functions.maven.plugin.version}&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;!-- function app name --&gt;
                    &lt;appName&gt;${functionAppName}&lt;/appName&gt;
                    &lt;!-- function app resource group --&gt;
                    &lt;resourceGroup&gt;${resourceGroupName}&lt;/resourceGroup&gt;
                    &lt;!-- function app service plan name --&gt;
                    &lt;appServicePlanName&gt;${appServicePlanName}&lt;/appServicePlanName&gt;
                    &lt;!-- function app region--&gt;
                    &lt;!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supported-regions for all valid values --&gt;
                    &lt;region&gt;${azure.region}&lt;/region&gt;
                    &lt;!-- function pricingTier, default to be consumption if not specified --&gt;
                    &lt;!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supported-pricing-tiers for all valid values --&gt;
                    &lt;!-- &lt;pricingTier&gt;&lt;/pricingTier&gt; --&gt;

                    &lt;!-- Whether to disable application insights, default is false --&gt;
                    &lt;!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details for all valid configurations for application insights--&gt;
                    &lt;!-- &lt;disableAppInsights&gt;&lt;/disableAppInsights&gt; --&gt;
                    &lt;runtime&gt;
                        &lt;!-- runtime os, could be windows, linux or docker--&gt;
                        &lt;os&gt;Linux&lt;/os&gt;
                        &lt;javaVersion&gt;11&lt;/javaVersion&gt;
                        &lt;!-- for docker function, please set the following parameters --&gt;
                        &lt;!-- &lt;image&gt;[hub-user/]repo-name[:tag]&lt;/image&gt; --&gt;
                        &lt;!-- &lt;serverId&gt;&lt;/serverId&gt; --&gt;
                        &lt;!-- &lt;registryUrl&gt;&lt;/registryUrl&gt;  --&gt;
                    &lt;/runtime&gt;
                    &lt;appSettings&gt;
                        &lt;property&gt;
                            &lt;name&gt;FUNCTIONS_EXTENSION_VERSION&lt;/name&gt;
                            &lt;value&gt;~4&lt;/value&gt;
                        &lt;/property&gt;
                    &lt;/appSettings&gt;
                &lt;/configuration&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;package-functions&lt;/id&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;package&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;

答案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(&quot;FUNCTIONS_EXTENSION_VERSION&quot;);
System.out.println(&quot;FUNCTIONS_EXTENSION_VERSION: &quot; + 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

huangapple
  • 本文由 发表于 2023年8月9日 03:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定