Azure Pipeline+Maven构建报错:无法创建test-classes目录求助
我正在使用Azure Pipeline结合Maven(apache-maven-3.6.3)构建Java项目,开发工具为IntelliJ。
当前Pipeline配置
Maven pom.xml: Display name: Maven job Agent pool : DockerBuildAgent Goal(s): test "-Dargline=-Dcucumber.filter.tags='@smoke'" Test results files:**/surefire-reports/TEST-*.xml Copy files to: $(build.artifactstagingdirectory) Display name : Copy Files to: $(build.artifactstagingdirectory) Source Folder : $(system.defaultworkingdirectory) Contents : **/*.jar Target Folder : $(build.artifactstagingdirectory) Public Artifact:drop Display name:Publish Artifact: drop Path to publish : $(build.artifactstagingdirectory) Artifact name: drop Artifact publish location_Azure Pipelines Publish Cucumber Report: Display name Publish Cucumber Report Input Directory:$(System.DefaultWorkingDirectory) Output Directory:$(System.DefaultWorkingDirectory) Report Theme: Bootstrap
构建报错信息
[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Netrex --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /root/myagent/_work/638/s/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Netrex --- [INFO] No sources to compile [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Netrex --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.034 s [INFO] Finished at: 2024-01-09T09:26:49+03:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:2.6:testResources (default-testResources) on project Netrex: Cannot create resource output directory: /root/myagent/_work/638/s/target/test-classes -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException The process '/usr/bin/mvn' failed with exit code 1 Could not retrieve code analysis results - Maven run failed. No test result files matching /root/myagent/_work/638/s/**/surefire-reports/TEST-*.xml were found, so publishing JUnit test results is being skipped. ##[error]Build failed. Finishing: Maven
项目pom.xml的build插件配置
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <parallel>methods</parallel> <threadCount>2</threadCount> <perCoreThreadCount>false</perCoreThreadCount> <testFailureIgnore>true</testFailureIgnore> <runOrder>Alphabetical</runOrder> <includes> <include>**/*CukesRunner.java</include> <include>**/*FailedTestRunner.java</include> </includes> </configuration> </plugin> <plugin> <groupId>net.masterthought</groupId> <artifactId>maven-cucumber-reporting</artifactId> <version>5.7.4</version> <executions> <execution> <id>execution</id> <phase>test</phase> <goals> <goal>generate</goal> </goals> <configuration> <projectName>Cucumber HTML Reports</projectName> <outputDirectory>${project.build.directory}</outputDirectory> <inputDirectory>${project.build.directory}</inputDirectory> <jsonFiles> <param>target/cucumber.json</param> <param>failedTests*.json</param> </jsonFiles> <checkBuildResult>false</checkBuildResult> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
请问该如何解决这个报错,正常运行测试套件?
核心报错是Cannot create resource output directory: /root/myagent/_work/638/s/target/test-classes,针对这个问题按以下步骤处理:
修复Docker代理目录权限
构建代理是Docker容器,工作目录可能缺少写入权限。在Pipeline的Maven任务前添加脚本步骤,手动创建目录并赋予权限:mkdir -p $(system.defaultworkingdirectory)/target/test-classes chmod -R 777 $(system.defaultworkingdirectory)/target或者修改DockerBuildAgent配置,确保运行Maven的用户对工作目录有读写权限。
修正Maven命令参数格式
当前Goals里的参数写法有误,把test "-Dargline=-Dcucumber.filter.tags='@smoke'"改为:test -Dcucumber.filter.tags="@smoke"无需嵌套
-Dargline,直接传递标签参数即可避免解析异常。完善项目目录结构
日志显示src/main/resources不存在,若项目不需要该目录,要么创建空目录,要么在pom.xml中显式配置资源路径;同时确认src/test/resources是否存在,不存在则创建空目录,避免testResources插件执行报错。升级Maven资源插件版本
当前使用的maven-resources-plugin是2.6版本,与Maven 3.6.3可能存在兼容性问题,在pom.xml中指定更高版本:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.3.1</version> </plugin>调整Cucumber报告发布路径
把Publish Cucumber Report任务的目录改为:- Input Directory:
$(System.DefaultWorkingDirectory)/target - Output Directory:
$(build.artifactstagingdirectory)/cucumber-reports
确保能正确读取报告文件,同时将报告作为构件发布。
- Input Directory:
内容的提问来源于stack exchange,提问作者Mehmet Usta

