如何在AWS Device Farm中运行Maven命令及TestNG问题排查
Let's break down your two issues one by one and fix them step by step:
1. Maven Command Fails with "No POM in this directory" Error
Why this happens
AWS Device Farm extracts your uploaded test package into a temporary directory (like /tmp/scratchncneoU.scratch/test-packageO6o8s7 as shown in your error). When you run mvn test directly, Maven looks for the pom.xml in the current working directory—which isn’t the root of your extracted test package by default. Additionally, your original command has incorrect nested quoting around the cucumber.options parameter, which causes parsing issues for Maven.
Fix Steps
- Navigate to the correct directory first: Use the
$DEVICEFARM_TEST_PACKAGE_PATHenvironment variable (Device Farm automatically sets this to the root of your extracted test package wherepom.xmlresides). - Fix the quoting for cucumber.options: Avoid nested double quotes; use single quotes or format the system property without unnecessary outer quotes.
Your corrected YAML command should look like this:
- echo "Start Appium TestNG test" - cd $DEVICEFARM_TEST_PACKAGE_PATH - mvn test -Dcucumber.options='src/test/java/cucumber/features --glue cucumber.steps --tags @InstallApp'
Where is the POM file in Device Farm?
It lives at the root of your extracted test package, which is exactly what $DEVICEFARM_TEST_PACKAGE_PATH points to. Running cd $DEVICEFARM_TEST_PACKAGE_PATH ensures Maven can locate it immediately.
2. "Couldn't find the testng.xml in the jar file" Error
Why this happens
Even with your POM’s testResource configuration, two common issues are at play:
- Your
testng.xmlisn’t in a directory that Maven automatically packages into the test jar. By default, Maven only copies files fromsrc/test/resourcesto the classpath; if yourtestng.xmlis insrc/test/java/cucumber, it might not be included in the jar unless you explicitly configure this. - When using TestNG’s
-testjarflag, it expectstestng.xmlto be at the jar’s root—or you need to specify its exact path inside the jar.
Fix Steps
Option 1: Ensure testng.xml is packaged in the jar
Either move your testng.xml to src/test/resources, or update your POM to explicitly include the file from its current location:
<build> <testResources> <testResource> <directory>${project.basedir}/src/test/java/cucumber</directory> <includes> <include>testng.xml</include> </includes> </testResource> <!-- Keep any other existing test resource configurations here --> </testResources> </build>
Repackage your test jar after making this change.
Option 2: Specify the testng.xml path directly in your command
Instead of relying on the jar to contain testng.xml, point directly to the file in the extracted test package using the $DEVICEFARM_TEST_PACKAGE_PATH variable:
- java -Dappium.screenshots.dir=$DEVICEFARM_SCREENSHOT_PATH -Dcucumber.options='classpath:features --glue cucumber.steps --tags @InstallApp' org.testng.TestNG -testngxml $DEVICEFARM_TEST_PACKAGE_PATH/src/test/java/cucumber/testng.xml -d $DEVICEFARM_LOG_DIR/test-output -verbose 10
If you do keep testng.xml inside the jar, add -xmlpathinjar cucumber/testng.xml to the command to tell TestNG exactly where to find it.
内容的提问来源于stack exchange,提问作者kidulf

