maven dev pro test 环境打包

本文介绍如何使用Maven根据不同环境自动选择配置文件进行打包。通过定义profiles和激活条件,可以轻松实现开发、测试及生产环境的配置切换。此外,还展示了如何通过编译命令指定环境变量来触发特定配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId></groupId>
	<artifactId></artifactId>
	<version></version>
	<packaging>pom</packaging>
	<modules>
		<module>Common</module>
		<module>Service</module>
		<module>Api</module>
		<module>Web</module>
	</modules>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<mysql.version>5.1.31</mysql.version>
		<mybatis.version>3.2.7</mybatis.version>
		<spring.version>4.0.6.RELEASE</spring.version>
		<aspectj.version>1.7.0</aspectj.version>
	</properties>
	<dependencies>
		......
	</dependencies>

	<!-- profiles for dev pro test package plugin by command params ? -->
 	<profiles>
		<profile>
			<id>dev</id>
			<activation>
				<activeByDefault>true</activeByDefault>
				<property>
					<name>env</name>
					<value>dev</value>
				</property>
			</activation>
			<properties>
				<env.profile>dev</env.profile>
			</properties>
		</profile>
		<profile>
			<id>beta</id>
			<activation>
				<property>
					<name>env</name>
					<value>beta</value>
				</property>
			</activation>
			<properties>
				<env.profile>beta</env.profile>
			</properties>
		</profile>
		<profile>
			<id>pro</id>
			<activation>
				<property>
					<name>env</name>
					<value>pro</value>
				</property>
			</activation>
			<properties>
				<env.profile>pro</env.profile>
			</properties>
		</profile>
	</profiles>

	<build>
		<finalName></finalName>
		<plugins>
			<plugin> 
			   <groupId>org.mybatis.generator</groupId> 
			   <artifactId>mybatis-generator-maven-plugin</artifactId> 
			   <version>1.3.2</version> 
	           <configuration>  
	                 <verbose>true</verbose>  
	                 <overwrite>true</overwrite>  
	           </configuration>  
			</plugin>
			
			<!-- dev pro test package plugin -->
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.1-alpha-1</version>  
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>

		<!-- resources for dev pro test package plugin -->
		<resources>
			<!-- remove props**/** when package -->
			<resource>
				<directory>src/main/resources</directory>
				<excludes>
					<exclude>props**/**</exclude>
				</excludes>
				<filtering>false</filtering>
			</resource>
			<!-- add props to props/ -->
			<resource>
				<directory>src/main/resources/props/${env.profile}</directory>
				<targetPath>props/dev/</targetPath>
				<filtering>true</filtering>
			</resource>
		</resources>
	</build>
</project>

以上pom.xml

 	<beans profile="dev">   
		<context:property-placeholder location="classpath*:props/dev/*.properties"/>
	</beans>   
	<beans profile="pro">
		<context:property-placeholder location="classpath*:props/pro/*.properties"/>
	</beans>   
	<beans profile="test">
		<context:property-placeholder location="classpath*:props/test/*.properties"/>
	</beans>
	
以上spring.xml

	<context-param>
		<param-name>spring.profiles.active</param-name>
		<param-value>dev</param-value>
	</context-param>
以上web.xml


编译命令如下

mvn clean package -Denv=dev -DskipTests=true

mvn clean package -Denv=pro -DskipTests=true


以上为MAVEN 根据不同环境打包,复制不同配置文件的功能。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钩子水手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值