Maven Profile多环境打包

在日常开发中,通常不止一套环境,如开发环境、测试环境、预发环境、生成环境,而每个环境的配置项可能都不一样,这就需要用到多环境打包来解决这个问题。

  • 1.在项目的resouces目录中新建conf目录,再在其中按照环境创建相应目录,这里创建开发环境dev、测试环境test,再将原本的application.properties文件分别拷贝一份到两个目录中,根据环境修改其中的配置项,最后删除原本的配置文件。得到目录结构如下:
   |-- resources
      |-- conf
        |-- dev
        |   |-- application.properties
        |-- test
            |-- application.properties
  • 2.往项目的pom.xml配置文件中添加prifile标签
<profiles>
      <profile>
          <id>dev</id>
          <properties>
              <profile.env>dev</profile.env>
          </properties>
          <activation>
              <activeByDefault>true</activeByDefault>    //表示默认的意思
          </activation>
      </profile>
      <profile>
          <id>test</id>
          <properties>
              <profile.env>test</profile.env>
          </properties>
      </profile>
</profiles>

注:其中dev为默认激活的profile,如要增加其他环境按照上述步骤操作即可。

  • 3.设置打包时资源文件路径。
<build>
      <finalName>demo</finalName>
      <resources>
          <resource>
              <directory>${basedir}/src/main/resources</directory>
              <excludes>
                  <exclude>conf/**</exclude>
              </excludes>
          </resource>
          <resource>
             <directory>src/main/resources/conf/${profile.env}</directory>
		  </resource>
      </resources>
</build>

注:${basedir}为当前子模块的根目录。

  • 4.打包时通过P参数指定profile
    mvn clean install -P test

猜你喜欢

转载自blog.csdn.net/xiaokanfuchen86/article/details/113785192