Maven多环境配置

1.pom.xml中在dependencies和build之间加入以下内容。

<profiles>
		<profile>
			<!-- 本地开发环境 -->
			<id>develop</id>
			<properties>
				<profiles.active>develop</profiles.active>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<!-- 生产环境 -->
			<id>production</id>
			<properties>
				<profiles.active>production</profiles.active>
			</properties>
		</profile>
	</profiles>

2.resourcecs中添加以下内容。

			<resource>
				<directory>src/main/resources</directory>
				<excludes>
					<exclude>config-develop/*</exclude>
					<exclude>config-production/*</exclude>
				</excludes>
			</resource>
			<resource>
				<!-- 根据参数指定资源目录 -->
				<directory>src/main/resources/config-${profiles.active}</directory>
				<!-- 指定编译后的目录即生成文件位置(默认为WEB-INF/class) -->
				<!--<targetPath>config</targetPath>-->
			</resource>

3.项目中的resources文件夹下创建两个文件夹,并将用于不同生产环境的配置文件加入其中。

4.打包时使用以下命令进行打包,打包后config-develop或config-production中的文件会出现在第2步中配置的资源目录中。

mvn clean package -P develop
mvn clean package -P production

猜你喜欢

转载自my.oschina.net/kalnkaya/blog/1813886