maven环境配置
在正常项目中一般都会有多个环境,而不同的环境一些配置和流程是不一样的,可以使用maven来进行环境的区分,取不同的配置,不过spring也是支持环境区分的,我一般还是习惯于用spring boot的环境配置
maven支持在settings.xml中配置,也支持在pom.xml中配置
pom.xml中配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
<profiles> <profile> <id>local</id> <properties> <profileActive>local</profileActive> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>dev</id> <properties> <profileActive>dev</profileActive> </properties> <activation> <activeByDefault>false</activeByDefault> </activation> </profile> <profile> <id>test</id> <properties> <profileActive>test</profileActive> </properties> <activation> <activeByDefault>false</activeByDefault> </activation> </profile> <profile> <id>prod</id> <properties> <profileActive>prod</profileActive> </properties> <activation> <activeByDefault>false</activeByDefault> </activation> </profile> </profiles>
|
settings.xml配置
在settings.xml中也可以配置环境,将pom.xml中的profiles抽离出来,写在settings.xml中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| <settings> <profiles> <profile> <id>local</id>
<activation> <activeByDefault>false</activeByDefault> <jdk>1.8</jdk> <os> <name>macOS</name> <family>macOS</family> <arch>Catalina</arch> <version>10.15.7</version> </os>
<file> <exists>${basedir}/dev.properties</exists> <missing>${basedir}/pro.properties</missing> </file> <property> <name>projectVersion</name> <value>0.0.1</value> </property> </activation> <properties/> <repositories> <repository> <id>apache.snapshots</id> <name>Apache Development Snapshot Repository</name> <url>https://repository.apache.org/content/repositories/snapshots/</url> <releases> <enabled>true</enabled> <updatePolicy>daily</updatePolicy> <checksumPolicy>fail</checksumPolicy> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> </repository> </repositories> <pluginRepositories/> </profile> </profiles> <activeProfiles> <activeProfile>profile-id</activeProfile> </activeProfiles> </settings>
|
激活方式
命令行激活
mvn命令使用-P来指定profile
1 2
| # 使用test环境 mvn clean install -Ptest
|
settings激活
在上述settings.xml配置中也看到了,可以使用activeProfiles来指定激活的环境
1 2 3 4
| <activeProfiles> <activeProfile>profile-id</activeProfile> </activeProfiles>
|
条件判断激活
也是在settings.xml中配置activation,来根据系统属性判断是否激活