You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 22:15:01