如何配置Maven profile实现仅激活指定profile时deploy到私有Maven仓库
实现方案
这个需求完全可以实现,常用的配置方案有两种,具体配置方式如下:
方案1:在指定profile内单独配置部署规则
该方案是最常用的实现方式,将私有仓库地址、deploy插件的执行配置都放到指定profile中,未激活该profile时没有对应部署配置,自然不会执行私有仓库部署操作。
配置示例如下:
<profiles> <profile> <!-- 自定义profile id --> <id>private-repo-deploy</id> <!-- 可选:配置profile自动激活规则,也可选择手动触发激活 --> <activation> <!-- 示例规则:传入-Ddeploy.allow=true参数时自动激活该profile --> <property> <name>deploy.allow</name> <value>true</value> </property> </activation> <!-- 私有仓库地址配置 --> <distributionManagement> <repository> <id>private-release</id> <url>http://你的私有服务地址/release</url> </repository> <snapshotRepository> <id>private-snapshot</id> <url>http://你的私有服务地址/snapshot</url> </snapshotRepository> </distributionManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>3.1.1</version> <executions> <execution> <phase>deploy</phase> <goals> <goal>deploy</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>
使用方式:
- 手动激活profile执行部署:执行命令
mvn clean deploy -Pprivate-repo-deploy - 配置了自动激活规则的场景,执行对应触发命令即可:
mvn clean deploy -Ddeploy.allow=true - 未激活该profile时,即使执行
mvn deploy也会因缺少部署仓库配置终止部署操作。
方案2:全局配置部署规则,通过profile控制是否跳过部署
如果已经在全局配置了私有仓库地址,仅需要控制deploy操作的执行开关,可以选择该方案:
配置示例如下:
<!-- 全局默认配置:默认跳过deploy操作 --> <properties> <maven.deploy.skip>true</maven.deploy.skip> </properties> <profiles> <profile> <id>deploy-enable</id> <properties> <!-- 激活该profile时,关闭deploy跳过配置 --> <maven.deploy.skip>false</maven.deploy.skip> </properties> </profile> </profiles>
使用方式:
- 需要执行部署时,执行命令
mvn clean deploy -Pdeploy-enable即可 - 默认状态下执行
mvn deploy会自动跳过部署步骤,不会上传制品到私有仓库。
注意:私有仓库的账号密码认证信息需要配置在Maven安装目录下的conf/settings.xml文件的
节点中,配置的id需要和pom.xml中repository的id保持一致,否则会触发权限报错。
内容的提问来源于stack exchange,提问作者omaration
相关产品推荐
相关产品推荐

