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

来源:互联网 发布:逆战磁暴矩阵 编辑:程序博客网 时间:2024/05/16 17:40

在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/contexthttp://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。

原创粉丝点击