MAVEN打包各个环境不同变量应用

Spring配置文件引用:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>${env.dir}jdbc.properties</value>
        </list>
    </property>
</bean>

web.xml配置文件引用:

<context-param>
	<param-name>spring.profiles.active</param-name>
	<param-value>${profiles.active}</param-value>
</context-param>

<filter>
	<filter-name>CASFilter</filter-name>
	<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
	<init-param>
		<param-name>casServerLoginUrl</param-name>
		<!-- 下面的URL是Cas服务器的登录地址 -->
		<param-value>https://cas.oa.vipshop.com:8443/login</param-value>
	</init-param>
	<init-param>
		<param-name>serverName</param-name>
		<!-- 下面的URL是具体某一个应用的访问地址 -->
		<param-value>${cas.url}</param-value>
	</init-param>
</filter>


Pom.xml中代码:

<build>
		<finalName>****</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<warName>****</warName>
					<webResources>
						<resource>
							<filtering>true</filtering>
							<directory>src/main/webapp</directory>
							<includes>
								<include>**/web.xml</include>
							</includes>
						</resource>
					</webResources>
					<warSourceDirectory>src/main/webapp</warSourceDirectory>
					<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skipTests>true</skipTests>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
		<resources>
			<resource>
				<directory>src/main/resources/</directory>
				<filtering>true</filtering>
			</resource>
			<resource>
				<directory>src/main/webapp/WEB-INF/</directory>
				<filtering>true</filtering>
			</resource>
		</resources>
	</build>
	<profiles>
		<!-- 开发环境,默认激活 。 执行指令 mvn clean install/package -->
		<profile>
			<id>development</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<properties>
				<env.dir>classpath:</env.dir>
				<cas.url>http://localhost</cas.url>
				<profiles.active>development</profiles.active>
			</properties>
		</profile>
		<!-- 测试环境 ,执行指令 mvn clean install/package -P test -->
		<profile>
			<id>test</id>
			<properties>
				<env.dir>file:/apps/</env.dir>
				<cas.url>http://localhost</cas.url>
				<profiles.active>test</profiles.active>
			</properties>
		</profile>
		<!-- 生产环境 ,执行指令 mvn clean install/package -P production -->
		<profile>
			<id>production</id>
			<properties>
				<env.dir>file:/apps/</env.dir>
				<cas.url>http://ip:port</cas.url>
				<profiles.active>production</profiles.active>
			</properties>
		</profile>
	</profiles>

pom配置中

<resources>
			<resource>
				<directory>src/main/resources/</directory>
				<filtering>true</filtering>
			</resource>
			<resource>
				<directory>src/main/webapp/WEB-INF/</directory>
				<filtering>true</filtering>
			</resource>
		</resources>

这段为需要扫描替换的路径

<profiles>中的配置为每一种install -P profile 的变量需要替换的值

Maven是一个构建和管理项目的工具,能够帮助开发人员自动化地处理项目的编译、测试、打包等任务。在使用Maven时,可以使用多环境节点来配置不同环境信息。 多环境节点主要是指在pom.xml文件中,可以使用Maven的profiles功能来定义不同环境配置。通过profiles,可以根据不同环境需求来配置项目的依赖、插件等信息。 首先,需要在pom.xml文件的顶层标签中添加profiles节点,然后在其中定义各个环境。 例如,在profiles节点中可以定义两个环境节点:开发环境和生产环境。在每个环境节点中可以定义相应的环境变量、插件、依赖等。 开发环境节点可以配置一些开发所需的插件、依赖等,例如可以配置一个用于自动重启应用的插件,并设置一些开发阶段需要的依赖。 生产环境节点可以配置一些与生产环境相关的插件、依赖等,例如可以配置一个用于优化代码的插件,并设置一些生产阶段需要的依赖。 在使用Maven构建项目时,可以通过命令行参数选择相应的环境。例如可以使用命令"mvn clean install -Pdev"来选择开发环境。这样,在构建项目时,Maven会根据选择的环境来加载相应的配置信息。 使用Maven的多环境节点能够方便地管理不同环境的配置信息,提高了项目开发和部署的效率。同时也能够避免因配置不同而导致的一些问题,提高了项目的可靠性和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值