Maven打包,把配置文件引用,打到外部

26 篇文章 0 订阅
24 篇文章 0 订阅
在SIT 环境测试的时候,因为服务器环境和本地环境可能不同,会造成不少问题,所以会进行多次打包。而且配置文件不同,所以每次打包都要修改配置文件,十分麻烦。
maven打包可以把配置文件引用打成外部的,这样就可以把sit环境的配置丢到服务器,然后每次打包动态选择配置文件引用地址,就很爽了。

1.maven,pom的配置,例如:

<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>
	<parent>
		<artifactId>666</artifactId>
		<groupId>com.xxx</groupId>
		<version>0.1.0</version>
	</parent>
	<artifactId>xxx</artifactId>
	<packaging>war</packaging>

	<name>xxx</name>
	<description>xxxxxx</description>
	<build>
	    <finalName>xxx</finalName>
	    <resources>
	        <resource>
	            <directory>src/main/java</directory>
	            <includes>
	                <include>**/*.properties</include>
	                <include>**/*.xml</include>
	            </includes>
	            <filtering>true</filtering>
	        </resource>
	        <resource>
	            <directory>src/main/resources</directory>
	            <includes>
	                 <include>**</include>
	            </includes>
	            <filtering>true</filtering>
	        </resource>
	    </resources>
	</build>
	
	<dependencies>
		<dependency>
			<groupId>ojdbc</groupId>
			<artifactId>ojdbc</artifactId>
			<version>12.1.0.1-atlassian-hosted</version>
		</dependency>
	</dependencies>
	
    <properties>
        <file.config.path-sit>这里是sit配置路径,例如:/project-sit/config/xxx/</file.config.path-sit>
        <file.config.path-prod>这里是生产配置路径,例如:/project-prod/config/xxx/</file.config.path-prod>
    </properties>
    
	<profiles>
        <!-- 开发环境 -->
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
				<some.config.path>
					classpath:config/some.properties
				<jdbc.config.path>
					classpath:config/jdbc.properties
				</jdbc.config.path>
            </properties>
        </profile>
        
        <!-- Sit测试环境 -->
        <profile>
            <id>sit</id>
            <properties>
	            <some.config.path>
					file:${file.config.path-sit}some.properties
				</some.config.path>	
				<jdbc.config.path>
					file:${file.config.path-sit}jdbc.properties
				</jdbc.config.path>
            </properties>
        </profile>
        <!-- 生产环境 -->
        <profile>
            <id>prod</id>
            <properties>
	            <some.config.path>
		            file:${file.config.path-prod}some.properties
	            </some.config.path>
	            <jdbc.config.path>
		            file:${file.config.path-prod}jdbc.properties 
	            </jdbc.config.path>
            </properties>
        </profile>
    </profiles>
</project>

2.引用配置文件的xml的配置,例如:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 引入属性配置文件 -->
	<bean class="com.xxx.xxx.xxx.xxx">
		<property name="locations">
			<list>
				<value>${some.config.path}</value>
                <value>${jdbc.config.path}</value>
			</list>
		</property>
	</bean>
	
</beans>

3.在打包的时候。用maven进行install,命令如下:

mvn package -P sit

这里的sit就是sit环境的配置路径(如果在eclipse里打包,则去掉mvn这个命令头 ),如果使用这个打包命令,则打出来的war包里xml引用配置文件路径改变,如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd">
                        
	<!-- 引入属性配置文件 -->
	<bean class="com.xxx.xxx.xxx.xxx">
		<property name="locations">
			<list>
				<value>
					file:/project-sit/config/xxx/some.properties
				</value> 
				<value>
					file:/project-sit/config/xxx/jdbc.properties
				</value>
			</list>
		</property>
	</bean>
	
</beans>

4.这样就可以把打好的war包,直接丢到服务器上,然后把sit的配置文件也丢到配置的文件夹下,这样每次都不用修改配置文件,直接打包,重新部署就ok。copy代码要注意。这里是例子,删减了很多东西。希望不要误导同道们,相互学习。

----------------自动化的路还有很长,假如全自动需要100步,那这里只能算是第一步。路漫漫其修远兮,吾将上下而不停的,撸、抄袭、copy。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值