Azure Artifacts执行mvn deploy遇409冲突,如何部署缺失包?
问题
执行mvn deploy时,部分Maven包已存在于Azure Artifacts中,想仅部署缺失的包,但遇到以下409冲突错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:3.1.1:deploy (default-deploy) on project myproject:
Failed to deploy artifacts: Could not transfer artifact my.artifact:pom:1.3.4 from/to azure-dev (url.pom).
transfer failed for url.pom:
status: 409 Conflict - The feed 'dev' already contains file 'my.artifacts-1.3.4.pom' in package ....
查过Azure Artifacts文档和DevOps门户设置,没找到跳过已存在包的办法。
解决方法
1. 用Maven Deploy插件的跳过参数直接处理
Maven Deploy插件3.0.0及以上版本支持skipIfExists参数,可以自动跳过已存在于仓库的构件。
命令行临时生效:直接在执行deploy命令时追加参数:
mvn deploy -Dmaven.deploy.skipIfExists=true项目pom.xml持久化配置:如果需要长期生效,在项目的
pom.xml中添加插件配置:<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>3.1.1</version> <configuration> <skipIfExists>true</skipIfExists> <!-- 替换成你的Azure Artifacts仓库信息 --> <altDeploymentRepository>azure-dev::default::your-feed-url</altDeploymentRepository> </configuration> </plugin> </plugins> </build>
2. 开启Azure Artifacts的覆盖模式(谨慎操作)
如果允许覆盖已存在的包,可以在Feed设置中开启覆盖权限:
- 进入Azure DevOps项目的Artifacts模块,打开目标Feed
- 点击右上角的设置图标,进入「Feed settings」
- 找到「Allow packages to be overwritten」选项并启用
之后执行mvn deploy会直接覆盖已存在的包,但注意这会替换现有包的内容,可能影响依赖此包的其他项目,非必要不建议用。
3. 手动部署缺失的单个包
如果只有少量包缺失,可以用deploy:deploy-file命令逐个部署:
mvn deploy:deploy-file \ -Dfile=/path/to/your/artifact.jar \ -DgroupId=my.artifact \ -DartifactId=my.artifact \ -Dversion=1.3.5 \ -Dpackaging=jar \ -DrepositoryId=azure-dev \ -Durl=your-feed-url
需要提前确认哪些包是缺失的,按需替换命令中的参数。
内容的提问来源于stack exchange,提问作者Sacagaweja

