Windows下如何在IntelliJ中用Git Bash执行Maven测试?
解决方案
方法1:全局配置Maven使用Git Bash执行命令
打开IntelliJ的设置:File > Settings > Build, Execution, Deployment > Build Tools > Maven > Runner,在Shell path字段中填入Git Bash的可执行文件路径,比如:
C:\Program Files\Git\bin\bash.exe
然后在Shell parameters中添加参数:
-l -c
该参数会让bash加载用户配置文件,模拟Linux环境的Shell行为。设置完成后,IntelliJ的Maven工具窗口执行所有命令时都会使用Git Bash,包括test生命周期阶段。
方法2:针对单个模块配置Shell环境
如果不想全局修改Maven的Shell设置,可以在包含脚本的模块的pom.xml中,给执行测试的插件指定Git Bash作为运行环境。比如用exec-maven-plugin执行Python测试时,配置如下:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>run-python-tests</id> <phase>test</phase> <goals> <goal>exec</goal> </goals> <configuration> <!-- 指定Git Bash路径 --> <executable>C:\Program Files\Git\bin\bash.exe</executable> <arguments> <argument>-c</argument> <!-- 这里写要执行的Linux兼容命令 --> <argument>python3 tests/unit_test.py</argument> </arguments> </configuration> </execution> </executions> </plugin>
这样该模块在执行test阶段时,会调用Git Bash来运行Python测试脚本,避免PowerShell的兼容性问题。
方法3:临时替代方案
既然已经配置了IntelliJ的Terminal使用Git Bash,直接在Terminal中执行mvn test命令即可,这个环境和外部Git Bash完全一致,能正常运行测试脚本。虽然不是通过Maven工具窗口点击执行,但无需修改任何配置,可快速解决问题。
内容的提问来源于stack exchange,提问作者Juh_
相关产品推荐
相关产品推荐

