Maven区分环境打包

Maven 区分环境打包

方法一:使用juvenxu.portable-config-maven-plugin

使用方法

  1. pom文件中引入插件
          <plugin>
				<groupId>com.juvenxu.portable-config-maven-plugin</groupId>
				<artifactId>portable-config-maven-plugin</artifactId>
				<version>1.1.4</version>
				<executions>
					<execution>
						<goals>
							<goal>replace-package</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
  1. 建立如下的目录结构: 其中portable中对应的是不同环境替换配置文件

目录结构
4. 替换配置内容:

<?xml version="1.0" encoding="utf-8" ?>
<portable-config>

    <config-file path="WEB-INF/classes/properties/db.properties">
        <replace key="mysql.password">123456789</replace>
    </config-file>

</portable-config>

config-file path 标签对应的是替换配置的路径
replace key 对应的是 配置文件中key,里面的内容对应的是替换配置的内容

其它

打包的时候如果想要排除portable文件内容,则pom文件中可以加入如下配置内容:

          <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.2</version>
				<configuration>
					<packagingExcludes>
						WEB-INF/classes/portable/**
					</packagingExcludes>
					<includeEmptyDirectories>false</includeEmptyDirectories>
				</configuration>
			</plugin>

方法二:选择指定的目录文件进行打包

  1. 添加环境参数
<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<env>dev</env>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<id>test</id>
			<properties>
				<env>test</env>
			</properties>
		</profile>
	</profiles>
  1. 指定动态的打包替换,打成war包时,排除对应的目录
         <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.2</version>
				<configuration>
					<webResources>
						<!--动态指定文件-->
						<resource>
							<directory>src/main/resources/env/${env}</directory>
							<targetPath>WEB-INF/classes/properties</targetPath>
							<filtering>true</filtering>
						</resource>
					</webResources>
					<packagingExcludes>
						**/portable/**,
						**/env/**
					</packagingExcludes>
					<includeEmptyDirectories>false</includeEmptyDirectories>
				</configuration>

			</plugin>
  1. 建立对应的目录结构

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值