本地运行Azure Functions基础示例失败,提示Stage目录不存在
Azure Functions Maven运行报错:Stage directory not found
问题详情
在IntelliJ中创建Azure Functions基础示例,执行Maven命令:
azure-functions:package azure-functions:run
时出现如下错误:
Failed to execute goal com.microsoft.azure:azure-functions-maven-plugin:1.6.0:run (default-cli) on project azure-function-examples: Stage directory not found. Please run mvn package first。
函数代码
import com.microsoft.azure.functions.ExecutionContext; import com.microsoft.azure.functions.HttpMethod; import com.microsoft.azure.functions.HttpRequestMessage; import com.microsoft.azure.functions.HttpResponseMessage; import com.microsoft.azure.functions.HttpStatus; import com.microsoft.azure.functions.annotation.AuthorizationLevel; import com.microsoft.azure.functions.annotation.FunctionName; import com.microsoft.azure.functions.annotation.HttpTrigger; import java.util.Optional; /** * Azure Functions with HTTP Trigger. */ public class HttpTriggerFunction { /** * This function listens at endpoint "/api/HttpExample". Two ways to invoke it using "curl" command in bash: * 1. curl -d "HTTP Body" {your host}/api/HttpExample * 2. curl "{your host}/api/HttpExample?name=HTTP%20Query" */ @FunctionName("HttpExample") public HttpResponseMessage run( @HttpTrigger( name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, final ExecutionContext context) { context.getLogger().info("Java HTTP trigger processed a request."); // Parse query parameter final String query = request.getQueryParameters().get("name"); final String name = request.getBody().orElse(query); if (name == null) { return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build(); } else { return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build(); } } }
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>azure-function-examples</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <name>Azure Java Functions</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <azure.functions.maven.plugin.version>1.9.1</azure.functions.maven.plugin.version> <azure.functions.java.library.version>1.4.0</azure.functions.java.library.version> <functionAppName>logicapp-1617072939138</functionAppName> <stagingDirectory>${project.build.directory}/azure-functions/${functionAppName}</stagingDirectory> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.4.4</version> </dependency> <dependency> <groupId>com.microsoft.azure.functions</groupId> <artifactId>azure-functions-java-library</artifactId> <version>${azure.functions.java.library.version}</version> </dependency> <!-- Test --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.4.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>2.23.4</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>com.microsoft.azure</groupId> <artifactId>azure-functions-maven-plugin</artifactId> <version>1.6.0</version> <configuration> <!-- function app name --> <appName>${functionAppName}</appName> <!-- function app resource group --> <resourceGroup>java-functions-group</resourceGroup> <!-- function app service plan name --> <appServicePlanName>java-functions-app-service-plan</appServicePlanName> <!-- function app region--> <region>westus</region> <runtime> <!-- runtime os, could be windows, linux or docker--> <os>windows</os> <javaVersion>8</javaVersion> </runtime> <appSettings> <property> <name>FUNCTIONS_EXTENSION_VERSION</name> <value>~3</value> </property> </appSettings> </configuration> <executions> <execution> <id>package-functions</id> <goals> <goal>package</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <overwrite>true</overwrite> <outputDirectory>${stagingDirectory}</outputDirectory> <resources> <resource> <directory>${project.basedir}</directory> <includes> <include>host.json</include> <include>local.settings.json</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.1.1</version> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${stagingDirectory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> <includeScope>runtime</includeScope> <excludeArtifactIds>azure-functions-java-library</excludeArtifactIds> </configuration> </execution> </executions> </plugin> <!--Remove obj folder generated by .NET SDK in maven clean--> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> <configuration> <filesets> <fileset> <directory>obj</directory> </fileset> </filesets> </configuration> </plugin> </plugins> </build> </project>
问题原因及解决方案
原因1:命令执行顺序异常
虽然同时指定了打包和运行命令,但Maven可能未按预期顺序完成打包阶段就执行运行命令,导致临时目录未生成。
解决方案:拆分命令分步执行
先单独执行打包:
mvn azure-functions:package
待打包完成后,再执行运行命令:
mvn azure-functions:run
原因2:插件版本不一致
pom.xml的properties中定义的插件版本为1.9.1,但实际插件配置中使用的是1.6.0,版本不匹配可能导致打包逻辑异常。
解决方案:统一插件版本
修改Azure Functions Maven插件的版本,与properties中定义的保持一致:
<plugin> <groupId>com.microsoft.azure</groupId> <artifactId>azure-functions-maven-plugin</artifactId> <version>${azure.functions.maven.plugin.version}</version> <!-- 其他配置保持不变 --> </plugin>
原因3:临时目录生成失败
若上述方案无效,可手动检查${project.build.directory}/azure-functions/${functionAppName}目录是否存在,若不存在,尝试清理后重新打包:
mvn clean mvn azure-functions:package
内容的提问来源于stack exchange,提问作者daniel
相关产品推荐
相关产品推荐

