解决方法1:
- profile配置:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
- resource配置:
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources.${env}</directory>
</resource>
</resources>
- 工程目录结构:
- war包目录结构:
解决方法2:
- profile配置:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<filters>
<filter>src/main/resources/config/db-dev.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>test</id>
<build>
<filters>
<filter>src/main/resources/config/db-dev.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<filters>
<filter>src/main/resources/config/db-prod.properties</filter>
</filters>
</build>
</profile>
</profiles>
- 原理:
filtering功能主要用来替换项目中的资源文件(.xml、.properties)当中的${...}
,比如${jdbc.username}
,那么如果配置了jdbc.username=root,在项目编译的时候,就会自动的把${jdbc.username}
替换为root。
解决方法3:
- profile配置:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<packaging.excludes>**/logback.xml</packaging.excludes>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<packaging.excludes>**/logback-test.xml</packaging.excludes>
</properties>
</profile>
</profiles>
- maven-war-plugin配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<packagingExcludes>${packaging.excludes}</packagingExcludes>
</configuration>
</plugin>
- 原理
maven-war-plugin插件warSourceExcludes和packagingExcludes参数:
warSourceExcludes:The comma separated list of tokens to exclude when copying the content of the warSourceDirectory.
解释:warSourceDirectory是maven工程的src\main\webapp目录,warSourceExcludes参数决定不将哪些src\main\webapp目录下的文件copy到webappDirectory(一般是工程的target\${warName}目录)。
packagingExcludes:The comma separated list of tokens to exclude from the WAR before packaging. This option may be used to implement the skinny WAR use case. Note that you can use the Java Regular Expressions engine to include and exclude specific pattern using the expression %regex[]. Hint: read the about (?!Pattern).
解释:在生成webappDirectory(一般是工程的target\${warName}目录)后,要将该目录打包成**.war后缀的压缩文件,packagingExcludes参数决定不将哪些webappDirectory**目录中的文件打到war包中。
两个参数是在不同阶段起作用的,需要注意的是warSourceExcludes只能排除src\main\webapp目录下的文件。