Spring非Boot项目中通过属性文件选择依赖Profile构建方案问询
我来给你捋清楚这个问题的解决思路哈——核心是把Maven构建时的Profile和Spring运行时的Profile通过属性文件联动起来,让你只需要修改属性文件里的配置,就能自动切换对应环境的依赖和Spring配置。下面是具体的实现步骤:
1. 准备构建属性文件
先在项目根目录创建一个build.properties文件,用来指定你要激活的环境Profile,比如:
# build.properties - 这里指定要激活的环境 active.profile=dev
你只需要修改这个值(比如改成prod),就能切换到对应环境的依赖配置。
2. 配置Maven,让它读取属性并激活对应Profile
接下来修改pom.xml,先让Maven读取上面的属性文件,再定义不同环境的Maven Profile,每个Profile对应不同的依赖:
2.1 开启Maven资源过滤
让Maven把属性文件里的值替换到项目的配置文件中:
<build> <!-- 引入构建属性文件 --> <filters> <filter>build.properties</filter> </filters> <!-- 开启资源过滤,让Maven替换resources目录下文件的占位符 --> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>
2.2 定义Maven Profile,关联对应依赖
在pom.xml里添加不同环境的Profile,每个Profile引入对应版本的外部依赖和自研依赖:
<profiles> <!-- 开发环境Profile --> <profile> <id>dev</id> <!-- 当build.properties里的active.profile=dev时自动激活这个Profile --> <activation> <property> <name>active.profile</name> <value>dev</value> </property> </activation> <dependencies> <!-- 外部带Spring Profile的依赖:假设外部依赖用classifier区分环境版本 --> <dependency> <groupId>com.external</groupId> <artifactId>external-lib</artifactId> <version>1.0.0</version> <classifier>dev</classifier> </dependency> <!-- 自研依赖:引入其dev环境的版本(自研依赖需要提前用Maven Profile构建带classifier的jar) --> <dependency> <groupId>com.custom</groupId> <artifactId>custom-lib</artifactId> <version>2.0.0</version> <classifier>dev</classifier> </dependency> </dependencies> </profile> <!-- 生产环境Profile --> <profile> <id>prod</id> <activation> <property> <name>active.profile</name> <value>prod</value> </property> </activation> <dependencies> <dependency> <groupId>com.external</groupId> <artifactId>external-lib</artifactId> <version>1.0.0</version> <classifier>prod</classifier> </dependency> <dependency> <groupId>com.custom</groupId> <artifactId>custom-lib</artifactId> <version>2.0.0</version> <classifier>prod</classifier> </dependency> </dependencies> </profile> </profiles>
注意:自研依赖需要提前配置Maven Profile,打包时生成带classifier的jar包。比如在自研依赖的
pom.xml里添加:<profiles> <profile> <id>dev</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <classifier>dev</classifier> <!-- 给dev环境的jar加后缀 --> </configuration> </plugin> </plugins> </build> </profile> <!-- prod环境同理 --> </profiles>
3. 配置Spring非Boot项目,关联Profile配置
在src/main/resources下创建Spring配置文件,让Spring根据激活的Profile加载对应配置:
3.1 主配置文件application.xml
这个文件作为入口,会根据属性值引入对应环境的配置:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 开启属性占位符,读取Maven过滤后的属性 --> <context:property-placeholder location="classpath:application.properties"/> <!-- 根据active.profile的值,引入对应环境的Spring配置文件 --> <import resource="application-${active.profile}.xml"/> </beans>
3.2 环境属性文件application.properties
这个文件里的占位符会被Maven替换成build.properties里的实际值:
# application.properties - 由Maven自动替换active.profile的值 active.profile=${active.profile}
3.3 分环境的Spring配置文件
创建application-dev.xml和application-prod.xml,分别对应不同环境的Spring bean配置,比如:application-dev.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 开发环境特有的bean配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 开发环境数据库配置 --> </bean> </beans>
4. 执行构建
现在只需要运行Maven构建命令,它会自动读取build.properties里的active.profile值,激活对应Profile并引入依赖:
mvn clean install
如果想临时切换环境,也可以直接在命令行覆盖属性值:
mvn clean install -Dactive.profile=prod
这样就实现了通过属性文件指定构建时使用对应Profile的依赖,同时Spring也会加载对应环境的配置~
内容的提问来源于stack exchange,提问作者Kamil W

