如何独立执行mvn test不触发clean/compile?求surefire插件POM模板
mvn surefire:test Without Auto Clean/Compile I’ve faced similar classloading conflicts when running repeated test commands in Selenium/Cucumber/TestNG setups—so I get exactly where you’re coming from. The key here is that mvn surefire:test executes only the test goal, skipping the full Maven lifecycle (which includes clean and compile). But to make it work reliably, you need to properly configure the Surefire plugin to target your test runners, and compile your project once upfront.
Here’s a complete pom.xml template tailored to your stack, with comments explaining each critical part:
<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.yourcompany</groupId> <artifactId>your-test-framework</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <maven.compiler.source>11</maven.compiler.source> <!-- Adjust to your Java version --> <maven.compiler.target>11</maven.compiler.target> <cucumber.version>7.14.0</cucumber.version> <testng.version>7.8.0</testng.version> <selenium.version>4.11.0</selenium.version> <surefire.version>3.1.2</surefire.version> </properties> <dependencies> <!-- Cucumber Core & TestNG Integration --> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>${cucumber.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-testng</artifactId> <version>${cucumber.version}</version> <scope>test</scope> </dependency> <!-- TestNG Dependency --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>${testng.version}</version> <scope>test</scope> </dependency> <!-- Selenium WebDriver --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>${selenium.version}</version> <scope>test</scope> </dependency> <!-- Add your OATS dependencies here (adjust groupId/artifactId/version as needed) --> <!-- <dependency> <groupId>com.oracle.oats</groupId> <artifactId>oats-library</artifactId> <version>x.x.x</version> <scope>test</scope> </dependency> --> </dependencies> <build> <plugins> <!-- Maven Surefire Plugin: Configured to run TestNG/Cucumber tests --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${surefire.version}</version> <configuration> <!-- Option 1: Use a TestNG suite XML file (recommended for complex setups) --> <suiteXmlFiles> <suiteXmlFile>src/test/resources/testng-suite.xml</suiteXmlFile> </suiteXmlFiles> <!-- Option 2: Directly specify your Cucumber TestNG runner class --> <!-- <includes> <include>com/your/package/runners/CucumberTestRunner.java</include> </includes> --> <!-- Disable auto-forking if needed (matches your requirement to avoid fork) --> <forkCount>0</forkCount> <reuseForks>true</reuseForks> </configuration> </plugin> <!-- Compiler Plugin: Ensures proper Java version compatibility --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> </plugins> </build> </project>
Step-by-Step Usage:
Compile your project once to generate all required class files (this avoids the
NoClassDefFoundErrorsince classes won’t be refreshed on subsequent runs):mvn test-compile(Or
mvn compileif you need main classes compiled too)Run your tests repeatedly without triggering clean/compile:
mvn surefire:test
Additional Tips:
- If you need to run specific feature files or tags, pass Cucumber options via command line:
mvn surefire:test -Dcucumber.options="--tags @SmokeTest --glue com.your.package.steps" - Ensure your
testng-suite.xml(if using Option 1) correctly references your Cucumber TestNG runner classes. - If you still run into classloading issues, verify that all dependencies are properly scoped (
testfor test-only libraries) and that no conflicting versions are present.
内容的提问来源于stack exchange,提问作者Vijay

