roman_日积跬步-终至千里

roman_日积跬步-终至千里

一. 基本用法

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>

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

仓库激活:

 

二. 仓库激活方式

1. 使用activeProfile激活

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

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

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

 

2. 使用-P参数激活

可以通过使用-P参数显示的指定当前激活的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

得到如下结果

。。。
Active Profiles for Project '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>

 
 

注意:

 

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

08-12 07:09