李锋镝的博客

  • 首页
  • 时间轴
  • 说说
  • 每日心情
  • Now
  • 系列文章
  • 论坛
  • 左邻右舍
    • 左邻右舍
    • 博友圈
  • 留言
    • 留言
    • 走心评论
  • 关于
    • 关于本站
    • 网站地图
    • 网站统计
    • 另一个网站
    • 我的导航站
    • 赞助
  • 🚇开往
!Destiny
惟坚韧者始能遂其志
  1. 首页
  2. 代码人生
  3. 正文

解决idea中可以成功执行maven命令但是通过Terminal命令行执行失败的问题

2025年7月23日 约 1,663 字6 分钟 205 0 1
本文最后更新于 2025年11月20日,距今已 306 天,其中的信息可能已经发生变化,请注意甄别。

问题

通过Terminal命令行执行maven命令报错:

 mvn clean
[INFO] Scanning for projects...
[INFO] Artifact com.example:example-bom:pom:1.2.2 is present in the local repository, but cached from a remote repository ID that is unavailable in current build context, verifying that is downloadable from [central (https://repo.maven.apache.org/maven2, default, releases)]
[INFO] Artifact com.example:example-bom:pom:1.2.2 is present in the local repository, but cached from a remote repository ID that is unavailable in current build context, verifying that is downloadable from [central (https://repo.maven.apache.org/maven2, default, releases)]
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[FATAL] Non-resolvable parent POM for com.example:example-common:1.2.3-SNAPSHOT: The following artifacts could not be resolved: com.example:example-bom:pom:1.2.2 (present, but unavailable): com.example:example-bom:pom:1.2.2 was not found in https://repo.maven.apache.org/maven2 during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of central has elapsed or updates are forced and 'parent.relativePath' points at no local POM @ line 4, column 13
 @ 
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]   
[ERROR]   The project com.example:example-common:1.2.3-SNAPSHOT (/Users/bjsttlp176/WorkSpace/Public/example-common/pom.xml) has 1 error
[ERROR]     Non-resolvable parent POM for com.example:example-common:1.2.3-SNAPSHOT: The following artifacts could not be resolved: com.example:example-bom:pom:1.2.2 (present, but unavailable): com.example:example-bom:pom:1.2.2 was not found in https://repo.maven.apache.org/maven2 during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of central has elapsed or updates are forced and 'parent.relativePath' points at no local POM @ line 4, column 13 -> [Help 2]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException

不止是mvn clean,执行其他的maven命令也会失败,报错原因大同小异。

但是通过idea中的maven面板执行就能成功:

[INFO] Reactor Summary for example-common 1.2.3-SNAPSHOT:
[INFO] 
[INFO] example-common ...................................... SUCCESS [  0.054 s]
[INFO] example-common-constants ............................ SUCCESS [  0.001 s]
[INFO] example-common-swagger .............................. SUCCESS [  0.001 s]
[INFO] example-common-utils ................................ SUCCESS [  0.001 s]
[INFO] example-common-redis-tools .......................... SUCCESS [  0.001 s]
[INFO] redis-common-core .................................. SUCCESS [  0.001 s]
[INFO] redis-common-starter ............................... SUCCESS [  0.001 s]
[INFO] example-common-regular .............................. SUCCESS [  0.002 s]
[INFO] example-common-security ............................. SUCCESS [  0.002 s]
[INFO] example-common-web .................................. SUCCESS [  0.001 s]
[INFO] spring-common-util ................................. SUCCESS [  0.001 s]
[INFO] example-common-datasource ........................... SUCCESS [  0.002 s]
[INFO] example-common-dtp .................................. SUCCESS [  0.024 s]
[INFO] example-common-lb ................................... SUCCESS [  0.000 s]
[INFO] example-common-center-config ........................ SUCCESS [  0.001 s]
[INFO] example-apollo-config-starter ....................... SUCCESS [  0.001 s]
[INFO] example-common-trace ................................ SUCCESS [  0.001 s]
[INFO] web-common-config .................................. SUCCESS [  0.001 s]
[INFO] example-common-http ................................. SUCCESS [  0.002 s]
[INFO] example-common-mq ................................... SUCCESS [  0.000 s]
[INFO] kafka-swim-line .................................... SUCCESS [  0.001 s]
[INFO] example-kafka-starter ............................... SUCCESS [  0.001 s]
[INFO] example-common-mybatis .............................. SUCCESS [  0.001 s]
[INFO] example-rocketmq-starter ............................ SUCCESS [  0.003 s]
[INFO] kafka-swim-line-2.8.x .............................. SUCCESS [  0.001 s]
[INFO] example-common-auth ................................. SUCCESS [  0.001 s]
[INFO] example-auth-client ................................. SUCCESS [  0.001 s]
[INFO] example-common-transport ............................ SUCCESS [  0.001 s]
[INFO] example-transport-auth .............................. SUCCESS [  0.002 s]
[INFO] example-transport-auth-client-starter ............... SUCCESS [  0.002 s]
[INFO] example-transport-auth-client-javax-starter ......... SUCCESS [  0.001 s]
[INFO] example-collect ..................................... SUCCESS [  0.002 s]
[INFO] context-collect-javax .............................. SUCCESS [  0.002 s]
[INFO] example-transport-starter ........................... SUCCESS [  0.001 s]
[INFO] example-transport-javax-starter ..................... SUCCESS [  0.002 s]
[INFO] example-transport-jakarta-starter ................... SUCCESS [  0.001 s]
[INFO] example-common-log .................................. SUCCESS [  0.001 s]
[INFO] example-common-sentinel ............................. SUCCESS [  0.001 s]
[INFO] sentinel-common-core ............................... SUCCESS [  0.001 s]
[INFO] sentinel-app-starter ............................... SUCCESS [  0.001 s]
[INFO] sentinel-gateway-starter ........................... SUCCESS [  0.000 s]
[INFO] example-common-job .................................. SUCCESS [  0.001 s]
[INFO] example-auth-client-starter ......................... SUCCESS [  0.001 s]
[INFO] example-auth-client-javax-starter ................... SUCCESS [  0.001 s]
[INFO] example-common-liquibase ............................ SUCCESS [  0.001 s]
[INFO] web-common-util .................................... SUCCESS [  0.002 s]
[INFO] example-common-graceful-shutdown .................... SUCCESS [  0.001 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.400 s
[INFO] Finished at: 2025-07-23T15:33:32+08:00
[INFO] ------------------------------------------------------------------------

问题排查

一开始以为是maven版本不一致导致的。

IDEA 使用的是内置 Maven 3.9.9(路径:/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3)

系统环境使用的是 Homebrew 安装的 Maven 3.9.9(路径:/opt/homebrew/Cellar/maven/3.9.9/libexec)

虽然版本号相同,但路径不同可能导致配置冲突。

所以修改了idea的maven路径,和系统环境的一致,结果问题依旧。

后来通过比较idea的maven命令参数,发现其实idea执行maven命令的时候,通过命令行参数(-s)指定了项目的配置文件,这个配置文件中的仓库地址是私服的仓库地址。

而系统环境执行maven则使用的是maven默认的配置文件,导致Maven 在本地仓库中找到了依赖包,但由于某些原因无法验证其有效性,因此尝试从中央仓库重新下载。然而中央仓库并没有我自己的依赖包,导致下载失败。

通过Terminal命令行加-s参数的方式也验证了这个问题:

 mvn clean -s /WorkSoft/maven/common/settings.xml

解决办法

解决办法有两种,一种是使用-s指定配置文件,不过这样太麻烦,每次执行命令都需要加;第二个办法就是修改Maven的默认配置文件。

下面说几个修改maven配置文件的办法:

Maven 默认配置文件的设置方法

Maven 支持两种配置文件,并且会按照特定的优先级加载它们。要设置默认配置文件而不使用 -s 参数,可以通过以下方式实现:

1. 全局配置文件(System-wide Settings)

Maven 会自动读取其安装目录下的 conf/settings.xml 文件:

${MAVEN_HOME}/conf/settings.xml

优点:所有项目默认使用此配置,无需额外配置。
缺点:修改需要管理员权限,不适合多环境切换。

2. 用户配置文件(User-specific Settings)

Maven 会自动读取用户主目录下的 settings.xml 文件:

${user.home}/.m2/settings.xml

配置步骤:

  1. 在用户主目录下创建 .m2 文件夹(如果不存在):
    mkdir -p ~/.m2
  2. 在 .m2 目录下创建或编辑 settings.xml 文件:
    touch ~/.m2/settings.xml
  3. 添加你的配置(如镜像、仓库、认证等):
    <settings>
    <mirrors>
        <mirror>
            <id>private-repo</id>
            <mirrorOf>*</mirrorOf>
            <url>http://your-private-repo.com/repository/maven-public/</url>
        </mirror>
    </mirrors>
    </settings>

3. 环境变量配置(高级方法)

通过设置 MAVEN_OPTS 或 MAVEN_SKIP_RC 环境变量,可以覆盖默认行为:

方法 1:设置 MAVEN_OPTS

在 shell 配置文件(如 ~/.bashrc 或 ~/.zshrc)中添加:

export MAVEN_OPTS="-s /path/to/your/settings.xml"

效果:所有 Maven 命令都会自动使用指定的配置文件。

方法 2:使用 mavenrc 文件
  1. 在用户主目录下创建 .mavenrc 文件:
    touch ~/.mavenrc
  2. 添加以下内容:
    MAVEN_SKIP_RC=true  # 跳过默认配置
    MAVEN_OPTS="-s /path/to/your/settings.xml"

4. IDE 中的配置

在 IntelliJ IDEA 中设置默认配置文件:

  1. 打开 File > Settings > Build, Execution, Deployment > Build Tools > Maven。
  2. 在 User settings file 字段中指定路径(如 ~/.m2/settings.xml)。
  3. 勾选 Override 选项,确保 IDEA 使用此配置。

优先级顺序

Maven 加载配置的优先级从高到低为:

  1. 命令行参数 -s /path/to/settings.xml
  2. MAVEN_OPTS 环境变量中指定的 -s 参数
  3. 用户主目录下的 ~/.m2/settings.xml
  4. Maven 安装目录下的 ${MAVEN_HOME}/conf/settings.xml

验证方法

使用以下命令查看 Maven 实际使用的配置:

mvn help:effective-settings

输出会显示当前生效的配置文件路径和内容。

注意事项

  • 如果同时存在全局和用户配置文件,Maven 会合并它们,用户配置优先级更高。
  • 修改配置后,可能需要重启 IDE 或终端会话才能生效。
  • 对于企业环境,推荐使用用户配置文件,避免修改全局配置。
除非注明,否则均为李锋镝的博客原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.lifengdi.com/dai-ma-ren-sheng/4493

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
分享到

解决idea中可以成功执行maven命令但是通过Terminal命令行执行失败的问题

也可使用浏览器菜单中的「分享」功能

微信扫一扫分享

标签: IDEA maven Terminal
最后更新:2025年11月20日

岁月同一天 9 月 23 日

回望过去的今天,你在写什么

  • 代码人生
    1 年前 2025年9月23日
    Consul开启transparent-proxy访问外部资源的时候,请求偶发Connection reset问题分析

    在Consul中启用透明代理(Transparent Proxy)后,访问外部资源出现偶发Connection rese…

  • 其他
    7 年前 2019年9月23日
    Idea激活码

    2020.4.21到期 亲测有效 812LFWMRSH-eyJsaWNlbnNlSWQiOiI4MTJMRldNUlNI…

相关文章
  • Maven发布本地项目的jar包和源码到私有仓库(maven-source-plugin的简单使用)2020年5月25日
  • 8条经验轻松上手IDEA插件开发2020年11月6日
  • IDEA无限试用方法【2020.3最新亲测有效】2021年4月12日
  • 使用shell脚本统一修改maven项目的版本2025年7月10日
  • Idea激活码2019年9月23日

李锋镝

既然选择了远方,便只顾风雨兼程。

打赏 点赞
< 上一篇
下一篇 >
1234567891112131415161718192021222324252627282930313233343536373839404142434446474849505152535455575859606162636465666769727476777879808182858687909293949596979899
取消回复
…

文章评论

  • 信息发布Lv 1

    学到了

    WindowsWindowsChrome 109.0.0.0Chrome 109.0.0.0中国中国-江苏-苏州
    2025年7月30日
    00 回复
  • 爆竹声中一岁除,春风送暖入屠苏。
    千门万户曈曈日,总把新桃换旧符。

    听点儿音乐吧 朋友~
    文章目录
    最新 热点 随机
    最新 热点 随机
    Redis7.x&8.x 全新进阶系列(01):划时代升级总览——从6.x到7.x/8.x全版本变革全景 让WordPress静态化之Rocket‑Nginx WordPress下一代默认主题Ipsum预览 C++之父重磅发声:AI编程正在毁掉一代程序员 支撑全网40%网站的WordPress正在重新拥抱PHP生态 Kratos-plus v1.1.24版本更新说明
    关于主题加载速度优化的一点儿小演进给主题增加了Now、每日心情、年度回顾、岁月同一天、随机漫步等功能WordPress缓存插件WP Fastest Cache、WP Rocket 、FlyingPress对比关于使用AI的一些思考WordPress下一代默认主题Ipsum预览Kratos+ v1.1.16版本更新说明
    技术人员转团队管理该怎么做 Java设计支持千万级别的短链 从SQL规范性检查、表结构索引检查着手分析如何优化SQL 部署consul配置中心 妹妹的画【2019.07.13】 IDEA版本2020.*全局MAVEN配置
    最近评论
    李锋镝 发布于 17 小时前(09月22日) 是的,换风格了,不过我觉得之前的年份命名挺好的,一看就知道哪一年的
    obaby 发布于 20 小时前(09月22日) 我现在是个假的wp了,哈哈哈 wp终于改了主题的命名风格了
    李锋镝 发布于 3 天前(09月20日) 静态博客我之前也用过,但是感觉不是很方便,后来就一直用的WordPress
    Sheep5 发布于 3 天前(09月20日) 我直接用静态博客,天然有速度优势。
    不凡 发布于 3 天前(09月20日) 主要是wordpress插件丰富,需要什么功能插件,插件市场应有尽有,typecho是性能更好、更轻...
    标签聚合
    架构 多线程 Redis JAVA IDEA SpringBoot Theme 日常 分布式 数据库 SQL WordPress ElasticSearch AI编程 AI Spring MySQL Claude K8s JVM
    友情链接
    • Mr.Sun的博客
    • 彬红茶日记
    • lijie blog
    • 老张博客
    • 韩小韩博客
    • 志文工作室
    • 蜗牛工作室
    • 九仞之行
    • sssr7844的博客
    • 搬砖日记
    • 若梦博客
    • 林羽凡
    • Serendipity
    • 瓦匠个人小站
    • 懋和道人
    • 韩情脉脉
    • 临窗旋墨
    • Honesty
    • 皮皮社
    • 知向前端

    COPYRIGHT © 2016-2026 lifengdi.com. ALL RIGHTS RESERVED.

    lifengdi.com

    Domain age badge for lifengdi.com

    Theme Kratos-plus By Dylan Li

    津ICP备2024022503号-3

    京公网安备11011502039375号