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

GoCD执行mvn clean install报错:mvn未被识别,但CMD可正常构建

Fix: 'mvn' is not recognized in GoCD but works in regular CMD

I’ve run into this exact issue a few times—here’s what’s going on and how to fix it:

The core problem is that GoCD Agent runs in a different environment context than your regular CMD session, especially if GoCD is set up as a Windows service. It doesn’t automatically inherit the user-specific environment variables you’ve set up for Maven.

Here are the most effective solutions:

  • Check the Agent’s PATH environment variable
    Add a preliminary task in your GoCD pipeline to run echo %PATH% (for Windows) or echo $PATH (for Linux/macOS). Compare this output to the PATH you see when running echo %PATH% in your regular CMD. If Maven’s bin directory isn’t listed here, that’s the root cause.

  • Use Maven’s absolute path directly in your task
    Instead of relying on the mvn alias, call the executable using its full path. For example:

    C:\path\to\apache-maven-3.x.x\bin\mvn clean install
    

    This skips any environment variable issues entirely.

  • Add Maven to the GoCD Agent’s environment
    If GoCD Agent is running as a Windows service:

    1. Open the Services manager, find the GoCD Agent service
    2. Right-click > Properties > Environment tab
    3. Add a new variable MAVEN_HOME pointing to your Maven installation directory
    4. Edit the PATH variable to append ;%MAVEN_HOME%\bin
    5. Restart the GoCD Agent service

    For non-service setups, edit the GoCD Agent’s startup script to set these environment variables before launching the agent.

  • Ensure Maven’s path is set at the system level
    If you only added Maven to your user-specific PATH, the system account running GoCD won’t see it. Go to System Properties > Advanced > Environment Variables, add Maven’s bin directory to the System PATH, then restart your machine (or just the GoCD Agent) to apply changes.

Quick troubleshooting tip: Add a task to run where mvn (Windows) or which mvn (Unix-like) in your pipeline. If it returns nothing, you know the agent can’t locate the Maven executable at all.

内容的提问来源于stack exchange,提问作者Praveen Kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:05:06