pom.xml 配置
<!-- Maven动态切换多profiles编译环境 -->
<profiles>
<!-- 开发环境 -->
<profile>
<id>dev</id>
<!-- 默认激活 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- 设置变量, 标签名称自定义, ***.properties 中可根据 @activeProfile@ 来取值 -->
<activeProfile>dev</activeProfile>
</properties>
</profile>
<!-- 测试环境 -->
<profile>
<id>test</id>
<properties>
<activeProfile>test</activeProfile>
</properties>
</profile>
<!-- 生产环境 -->
<profile>
<id>prod</id>
<properties>
<activeProfile>prod</activeProfile>
</properties>
</profile>
</profiles>
application.properties 配置
spring.profiles.active=@activeProfile@
# 列出当前所有的 profile
mvn help:all-profiles
# 查看当前激活的 profile
mvn help:active-profiles
# 打包 dev 环境的
mvn clean package -Dmaven.test.skip=true -P dev
pom 属性
-
${project.build.sourceDirectory}:
项目的主源码目录,默认为src/main/java -
${project.build.testSourceDirectory}:
项目的测试源码目录,默认为src/test/java -
${project.build.directory}:
项目构件输出目录,默认为target/ -
${project.outputDirectory}:
项目主代码编译输出目录,默认为target/classes/ -
${project.testOutputDirectory}:
项目测试代码编译输出目录,默认为target/test-classes/ -
${project.groupId}:
项目的groupId -
${project.artifactId}:
项目的artifactId -
${project.version}:
项目的version,与${version}等价 -
${project.build.fianlName}:
项目打包输出文件的名称。默认为${project.artifactId}-${project.version}
资源管理标签
<build>
<!-- 项目打包输出文件的名称, 默认: ${project.artifactId}-${project.version} -->
<finalName>${project.artifactId}</finalName>
<!-- 资源管理 -->
<resources>
<resource>
<!-- 指定 resources 插件处理哪个目录下的资源文件 -->
<directory>src/main/resources</directory>
<!-- 打包后放在什么位置 -->
<targetPath>${project.build.directory}/classes</targetPath>
<!-- 是否为此资源启用筛选 -->
<filtering>true</filtering>
<!-- 包含的文件 -->
<includes>
<include>application.properties</include>
<include>application-${activeProfile}.properties</include>
<include>**.xml</include>
<include>mybatis/**/**.xml</include>
</includes>
<!-- 排除的文件 -->
<excludes>
<exclude>**.log</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/env</directory>
<targetPath>${project.build.directory}/classes/env</targetPath>
<includes>
<include>${activeProfile}/application.properties</include>
</includes>
</resource>
</resources>
</build>
Reference
- 官方使用文档