Maven <Profiles>定义不同环境的参数变量

本文介绍如何使用Maven的profiles特性在不同环境下配置变量,通过pom.xml和application.properties文件实现开发、测试和生产环境的参数切换。利用EL表达式调用变量,执行时指定参数组,实现灵活的环境配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

##应用场景

  • 我们在开发的时候会遇到需要区分正式环境、测试环境、开发环境使用不同的参数,如数据库的用户名及密码等。这时可以用Spring 的PropertyPlaceholderConfigurer 来配置受环境影响的变量,这种使用多个配置文件的方式,但是我觉得这样不够灵活,所以就使用了maven的profiles 来实现,在打包的时候maven就会根据指定的配置参数写入文件。
  • 配置方式分为两种,一种是全局配置即在.m2/conf/setting.xml 里面配置实现全局,一种是在项目中的pom.xml配置,这里我没有用到全局方式,就不详细介绍,我们主要介绍项目中配置方式。 ##实现

###1.pom.xml配置

	<build>
		<!-- 配置使用变量的配置文件-->
		<filters>
			<filter>src/main/resources/application.properties</filter>
		</filters>
		<resources>
			<!--配置文件路径-->
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
		</resources>
	</build>
	<profiles>
		<profile>
			<id>local</id>
			<properties>
				<pom.env>dev</pom.env>
				<pom.ver>1.2.4</pom.ver>
			</properties>
			<activation>
				<!--默认生效的配置组-->
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<id>dev</id>
			<properties>
				<pom.env>dev</pom.env>
				<pom.ver>1.3.3</pom.ver>
			</properties>
		</profile>
		<profile>
			<id>pro</id>
			<properties>
				<pom.env>pro</pom.env>
				<pom.ver>1.3.5</pom.ver>
			</properties>
		</profile>
	</profiles>

###2.application.properties配置

env=${pom.env}
ver=${pom.ver}

###3.执行 执行的时候加上-P ${profile.id},使用默认的可以不加,我用的IDE自带的工具选择生效参数组,在Profiles里面选择参数组

mvn clean install -P local

###4.总结

步骤如下:

  • pom.xml中添加<bulid>、<profiles>参数
  • EL表达式在配置文件中调用变量
  • 执行是-P 制定参数

转载于:https://my.oschina.net/zemochen/blog/676879

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.7</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>takeoutsystem</artifactId> <version>0.0.1-SNAPSHOT</version> <name>takeoutsystem</name> <description>takeoutsystem</description> <properties> <java.version>17</java.version> <!-- 修复版本冲突 --> <mybatis-plus.version>3.5.7</mybatis-plus.version> <mybatis.version>3.5.15</mybatis.version> <!-- 关键修复:升级到兼容版本 --> <mybatis-spring.version>3.0.3</mybatis-spring.version> <jjwt.version>0.11.5</jjwt.version> <springdoc-openapi.version>2.8.5</springdoc-openapi.version> <!-- 环境配置变量 --> <env>dev</env> </properties> <repositories> <repository> <id>aliyun-maven</id> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <dependencies> <!-- Spring Boot Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Thymeleaf 模板引擎 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- MyBatis-Plus 依赖 - 关键修复 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> <!-- 强化排除项解决冲突 --> <exclusions> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </exclusion> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </exclusion> <exclusion> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> </exclusion> </exclusions> </dependency> <!-- 添加明确的 MyBatis 依赖确保版本兼容 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency> <!-- 显式添加兼容的 MyBatis-Plus 核心 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>${mybatis-plus.version}</version> </dependency> <!-- MySQL 驱动 --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <!-- H2 内存数据库 (仅测试使用) --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> </dependency> <!-- JJWT 认证 --> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-api</artifactId> <version>${jjwt.version}</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-impl</artifactId> <version>${jjwt.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-jackson</artifactId> <version>${jjwt.version}</version> <scope>runtime</scope> </dependency> <!-- Spring Security --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity6</artifactId> </dependency> <!-- SpringDoc OpenAPI --> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>${springdoc-openapi.version}</version> </dependency> <!-- 测试依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter-test</artifactId> <version>${mybatis-plus.version}</version> <scope>test</scope> <!-- 同样需要排除冲突 --> <exclusions> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </exclusion> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <!-- Lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application*.yml</include> <include>application*.properties</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <excludes> <exclude>application*.yml</exclude> <exclude>application*.properties</exclude> </excludes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> <!-- 环境配置Profiles --> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <env>dev</env> </properties> <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application.yml</include> <include>application-dev.yml</include> </includes> </resource> </resources> </build> </profile> <profile> <id>prod</id> <properties> <env>prod</env> </properties> <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application.yml</include> <include>application-prod.yml</include> </includes> </resource> </resources> </build> </profile> </profiles> </project>这是对应的POM.xml文件,请分析错误在哪里
07-15
有两个yml文件,第一个业务层的:<?xml version="1.0"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.mw</groupId> <artifactId>ky-app-server</artifactId> <version>5.7.0</version> </parent> <artifactId>ky-app-server-biz</artifactId> <dependencies> <!--必备: undertow容器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> <!--必备: spring boot web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--必备: 注册中心客户端--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--必备: 配置中心客户端--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!--必备: 操作数据源相关--> <dependency> <groupId>com.mw</groupId> <artifactId>ky-common-data</artifactId> </dependency> <!--必备:ky安全模块--> <dependency> <groupId>com.mw</groupId> <artifactId>ky-common-security</artifactId> </dependency> <!--必备:xss 过滤模块--> <dependency> <groupId>com.mw</groupId> <artifactId>ky-common-xss</artifactId> </dependency> <!--必备: sentinel 依赖--> <dependency> <groupId>com.mw</groupId> <artifactId>ky-common-sentinel</artifactId> </dependency> <!--必备: feign 依赖--> <dependency> <groupId>com.mw</groupId> <artifactId>ky-common-feign</artifactId> </dependency> <!--必备: 依赖api模块--> <dependency> <groupId>com.mw</groupId> <artifactId>ky-app-server-api</artifactId> <version>5.7.0</version> </dependency> <!--必备: log 依赖--> <dependency> <groupId>com.mw</groupId> <artifactId>ky-common-log</artifactId> </dependency> <!--选配: mybatis 依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-spring-boot3-starter</artifactId> </dependency> <!--选配: druid 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-3-starter</artifactId> </dependency> <!--选配: mysql 数据库驱动 --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> <!--选配: swagger文档--> <dependency> <groupId>com.mw</groupId> <artifactId>ky-common-swagger</artifactId> </dependency> </dependencies> <profiles> <profile> <id>cloud</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <loaderImplementation>CLASSIC</loaderImplementation> </configuration> </execution> </executions> </plugin> <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <configuration> <skip>false</skip> </configuration> </plugin> </plugins> </build> </profile> <profile> <id>boot</id> </profile> </profiles> </project> 第二个总的:<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.mw</groupId> <artifactId>ky</artifactId> <version>5.7.0</version> </parent> <artifactId>ky-app-server</artifactId> <packaging>pom</packaging> <!--项目子模块--> <modules> <module>ky-app-server-api</module> <module>ky-app-server-biz</module> </modules> </project> 我该如何改
最新发布
07-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值