Composer archive命令根本不存在,执行必报错“Command 'archive' is not defined”;官方从未实现该命令,所有成功案例均源于混淆git archive或使用已废弃的第三方插件;真正可靠的方式是用git archive配合.gitattributes精确排除文件。

composer archive 命令根本不存在
你在终端执行 composer archive 时一定会看到错误:Command "archive" is not defined。这不是配置问题,也不是版本太低——Composer 官方二进制从没实现过这个命令。所有“成功运行”的案例,要么是旧版第三方插件(如 composer-archive-plugin,早已停止维护且不兼容 Composer 2.x),要么是把 git archive 命令记成了 Composer 命令。
.gitattributes 的 export-ignore 才是真正生效的排除机制
GitHub/GitLab 点「Download ZIP」、Packagist 构建 dist 包、CI 中调用 git archive,全部依赖 Git 原生的 .gitattributes 文件。它比 archive.exclude 更底层、更通用、更可靠。
-
/tests export-ignore—— 开头带/表示精确匹配根目录下的tests/目录,不会误删src/MyTests -
/.env export-ignore—— 防止敏感文件随 ZIP 泄露 -
/phpunit.xml export-ignore—— 排除配置文件,而非仅靠 autoload-dev - 路径不区分大小写(Windows 下可能不稳定),但推荐统一小写写法
- 该文件必须提交到 Git,否则任何基于 git archive 的分发都无视它
archive.exclude 只对 Packagist 和 composer archive(极少数情况)有效
如果你控制包发布流程(比如自己维护私有 Satis 或用 Packagist),可以在 composer.json 根级写:
{
"archive": {
"exclude": ["/tests", "/docs", "/.editorconfig", "/phpunit.xml"]
}
}
但注意:
- 这个字段只在 Packagist 自动构建或你手动运行
composer archive(需确认当前 Composer 版本是否意外暴露了该命令)时起作用 - 它对 GitHub ZIP 下载完全无效——因为那走的是
git archive,只读.gitattributes - 路径必须以
/开头,写成"tests/"或"tests"都不匹配 - 不支持通配符(
**、*),"/tests/**"是无效写法
真正想导出可部署包,别碰 archive 命令
生产环境需要的是含 vendor/、autoload.php、优化映射表的完整结构,而 composer archive 连 vendor/ 目录是否存在都不检查。正确链路是:
- 先跑:
composer install --no-dev --optimize-autoloader --prefer-dist - 再用:
git archive --format=zip --output=release.zip HEAD(前提是.gitattributes已配好) - 确保输出包里有
composer.lock,否则目标机无法复现依赖 - 敏感文件(如
.env)必须靠.gitattributes排除,archive.exclude在这里兜不了底
最容易被忽略的一点:很多人以为改了 composer.json 就万事大吉,结果 GitHub ZIP 里还是有 tests/——因为没意识到,那个 ZIP 不看你 JSON,只认 Git 的 .gitattributes。


















