美文网首页
Maven Profile 的应用

Maven Profile 的应用

作者: 聊聊其他 | 来源:发表于2019-11-07 09:58 被阅读0次

maven 的 profile 应用 或多或少都会出现在中大型的公司中。

如果你是第一次接触maven 这个profile, 可能你并不会关心它究竟是做什么用的,就像我一样。

即使见过很多次,但是不需要自己配置,能动就好。

但是问题出现之后,你就可能发现不了什么问题了。

基本概念

Profile ,跟基本翻译出来的意思差不多,在技术翻译中就是配置的意思。根据不同的profile,运行时替换掉定义好的变量值。

如果你用过spring的profile,大概就知道怎么样了。

不同类型的profile

  • 项目级别的profile
    定义POM文件中
  • 用户级别
    定义在 (%USER_HOME%/.m2/settings.xml)
  • 全局 定义在 (${maven.home}/conf/settings.xml).

如何激活profile?

  • Explicitly
  • Through Maven settings
  • Based on environment variables
  • OS settings
  • Present or missing files
在 setting 配置文件中,显式启动

在配置文件中,可以在<activeProfiles> 标签内,显式指定要启动的profile

如下:

<settings>
  ...
  <activeProfiles>
    <activeProfile>profile-1</activeProfile>
  </activeProfiles>
  ...
</settings>
基于build的环境变量来启动

下面的配置代表,当jdk 版本号在 1.3 到 1.5之间的情况下,profile就会被激活

<profiles>
  <profile>
    <activation>
      <jdk>[1.3,1.6)</jdk>
    </activation>
    ...
  </profile>
</profiles>

下面这个则是,基于运行时的变量来指定,如果 系统变量中有“environment” 且 值为 "test"的时候,下面的profile就或被激活。

mvn groupId:artifactId:goal -Denvironment=test

    <profiles>
  <profile>
    <activation>
      <property>
        <name>environment</name>
        <value>test</value>
      </property>
    </activation>
    ...
  </profile>
</profiles>
具体应用

那么实际中,可以利用profile来做什么东西?

通常在pom层面的profile的灵活性会高很多,因为这只是针对项目级别,修改profile的配置不会影响到其他的项目。

具体可以在pom中修改的内容项有如下:

<repositories>
<pluginRepositories>
<dependencies>
<plugins>
<properties> (not actually available in the main POM, but used behind the scenes)
<modules>
<reporting>
<dependencyManagement>
<distributionManagement>
a subset of the <build> element, which consists of:
<defaultGoal>
<resources>
<testResources>
<finalName>

这些都是可以被profile中定义的属性替换掉pom中的值。

具体例子

下面这个配置指定了一个appserver.home

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.myco.plugins</groupId>
        <artifactId>spiffy-integrationTest-plugin</artifactId>
        <version>1.0</version>
        <configuration>
          <appserverHome>${appserver.home}</appserverHome>
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

我们在POM中再加上如下的profile配置

<project>
  ...
  <profiles>
    <profile>
      <id>appserverConfig-dev</id>
      <activation>
        <property>
          <name>env</name>
          <value>dev</value>
        </property>
      </activation>
      <properties>
        <appserver.home>/path/to/dev/appserver</appserver.home>
      </properties>
    </profile>
 
    <profile>
      <id>appserverConfig-dev-2</id>
      <activation>
        <property>
          <name>env</name>
          <value>dev-2</value>
        </property>
      </activation>
      <properties>
        <appserver.home>/path/to/another/dev/appserver2</appserver.home>
      </properties>
    </profile>
  </profiles>
  ..
</project>

这个profile的配置被激活时,可以替换掉定义的变量值。

按照这个方法,其他的配置也可以相应的进行配置改变。

本文由博客一文多发平台 OpenWrite 发布!

相关文章

  • Maven Profile 的应用

    maven 的 profile 应用 或多或少都会出现在中大型的公司中。 如果你是第一次接触maven 这个pro...

  • Maven Profile 的应用

    maven 的 profile 应用 或多或少都会出现在中大型的公司中。 如果你是第一次接触maven 这个pro...

  • SpringBoot之条件注解

    背景 之前写过关于Spring和Maven的profile的区别 maven profile VS spring ...

  • maven---灵活构建(一)

    包含内容 maven属性 构建环境的差异 资源过滤 Maven Profile Web资源过滤 在profile中...

  • maven profile多环境配置

    使用maven profile实现多环境配置(代码) maven profile 实现多环境可移植构建 在开发过程...

  • centos7 安装 maven3.5

    下载maven 配置环境变量 vi /etc/profile source /etc/profile

  • Maven - Profile

    什么是profile profile是在maven xml中配置的,由 包围的一块配置 profile的作用 通...

  • Maven - profile

    版权所有,未经授权,禁止转载 章节 Maven – 简介 Maven – 工作原理 Maven – Reposit...

  • maven profile

    profile 能让maven项目在不同的环境下加载不同的配置,在pom文件中加入(如果是多模块项目,加在父POM...

  • 2020-03-19 springboot 打包运行

    打包 指定profile mvn clean package -Pbeta 指定profile以maven命令的方...

网友评论

      本文标题:Maven Profile 的应用

      本文链接:https://www.haomeiwen.com/subject/hnymbctx.html