能否通过Maven单独执行测试阶段?构建测试分离流水线咨询
Hey there! Let's tackle your two Maven-related questions with clear, actionable steps:
1. Can I run just the test phase with Maven?
Absolutely! You can directly trigger the test phase using this command:
mvn test
Just keep in mind that Maven's build lifecycle is sequential—running the test phase will automatically execute all preceding phases in the default lifecycle (like validate, compile, test-compile) because tests need the compiled classes to run. That's standard behavior for lifecycle phases.
2. How to run only tests (skip compile/validate) in a pipeline step?
Your pipeline setup makes total sense: build first with tests skipped, then run tests separately. Since the test phase inherently depends on earlier lifecycle phases, you can't run the phase itself without triggering those steps. But there's a solid workaround—directly call the Surefire plugin's test goal instead of using the lifecycle phase.
Use this command for your second pipeline step:
mvn surefire:test
This skips all the preceding lifecycle phases (like compile and validate) because you're invoking the plugin goal directly, not going through the lifecycle chain. Just make sure your first step (mvn clean install -DskipTests) has already compiled all the necessary classes—this command will reuse those existing compiled files to run the tests.
If you're using the Failsafe plugin for integration tests, the same logic applies: use mvn failsafe:integration-test instead.
内容的提问来源于stack exchange,提问作者Frankenstein

