news 2026/11/3 9:10:25

【maven】通过profiles实现:怎样激活某个仓库、同时加载多个profile、不同环境加载不同依赖jar

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【maven】通过profiles实现:怎样激活某个仓库、同时加载多个profile、不同环境加载不同依赖jar

文章目录

  • 一. 基本用法
  • 二. 仓库激活方式
    • 1. 使用activeProfile激活
    • 2. 使用-P参数激活
    • 3. 使用-P参数不激活
  • 三. 查看激活的仓库
  • 四. 不同环境依赖不同版本的jar

Maven中的profile是一组可选的配置,可以用来设置或者覆盖配置默认值。有了profile,你就可以为不同的环境定制构建。

一. 基本用法

profile可以在pom.xml中和maven的setting.xml文件中配置,如下:

<settings><profiles><profile><id>nexus</id><repositories><repository><id>my-repo</id><url>https://example.com/maven-repo</url></repository></repositories></profile></profiles><activeProfiles><activeProfile>nexus</activeProfile></activeProfiles></settings>

在上述示例中,我们定义了一个名为"nexus"的profile,并在其中设置了一个名为"my-repo"的Maven仓库。该仓库的URL为"https://example.com/maven-repo"。

仓库激活:

通过将activeProfile元素设置为"nexus",我们激活了这个profile,这意味着Maven会在解析和下载依赖项时搜索并使用该仓库。

二. 仓库激活方式

1. 使用activeProfile激活

如上,通过activeProfile标签进行激活,比如在使用idea进行项目打包时,会使用对应id的仓库进行依赖下载

<activeProfiles><activeProfile>nexus</activeProfile></activeProfiles>

如下图:idea中也能看到我们添加和激活的profile,

当然我们可以激活多个profile,对于idea来说同时选中你想要的profile就行

2. 使用-P参数激活

可以通过使用-P参数显示的指定当前激活的profile。

同时也可以指定多个profile,profile之间用逗号隔开

mvn clean install-Pnexus mvn clean install-P nexus mvn clean install-Pnexus,rat

3. 使用-P参数不激活

当项目使用settings.xml中激活的profile,但是在某些场景下又不想它处于激活状态。

mvn clean install-P!rat

三. 查看激活的仓库

在某一个项目下执行,比如我在linkis这个项目的父级目录下执行

mvn help:active-profiles

得到如下结果

每个模块都会展示激活的profile,这里看到的是linkis-dist模块下激活的profile

。。。 Active ProfilesforProject 'org.apache.linkis:linkis-dist:pom:1.3.2':The following profiles are active:-nexus(source:external)

四. 不同环境依赖不同版本的jar

通过profile可以解决,在项目开发中例如:生产环境依赖的hadoop版本是2.7.2U1,poc环境依赖的hadoop版本是官方的2.7.2版本。

<project...><properties><hadoop.version>1.0.0.RELEASE</hadoop.version></properties><dependencies><groupId>org.apache.hadoop</groupId><artifactId>hadoop-mapreduce-client-core</artifactId><version>2.7.2U1</version></dependencies><profiles><profile><id>test</id><properties><hadoop.version>2.7.2</hadoop.version></properties><!--激活这个profile 会额外加载这个插件><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><version>2.2.1</version><executions><execution><phase>package</phase><goals><goal>jar-no-fork</goal></goals></execution></executions></plugin></plugins></profile></profiles></project>


注意:

如果你使用了镜像设置,Maven会首先尝试从镜像仓库下载依赖项。如果镜像仓库中没有所需的依赖项,则会根据配置的repositories元素查找其他仓库。

通过配置profiles和repositories元素,你可以根据项目的要求加载特定的仓库,并控制Maven从哪里获取依赖项。

参考:
https://blog.csdn.net/Mr_rain/article/details/100138017
chat-gpt3.5

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/11/3 9:10:08

【wordcount】flink 代码快速入门

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/11/3 9:08:17

STM32G071RB隔离SPI实战:从地环路到数字隔离器调试

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/11/3 9:08:16

基于ESP32与电子墨水屏的冰箱购物清单同步方案

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/11/3 9:08:03

基于MAX14431与PIC18LF46K40的隔离型SPI通信链路设计与实现

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/11/3 9:07:11

工业级SPI隔离通信实战:MAX14431与K60的10MHz链路设计与踩坑

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华