多环境部署打包-maven篇

项目上线之前,往往要经过多套环境(开发环境,测试环境,生产环境)的部署,测试,运行.

每个环境的配置信息都不尽相同,最笨的方法就是手动替换.不过这样效率比较低,而且容易出错.

maven提供了一些插件可以帮助我们针对不同的环境,进行快速打包程序.

目录结构比较

打包后

.
|____index.jsp
|____WEB-INF
| |____classes
| | |____com 字节码文件目录
| | |____dev.properties 配置文件
| | |____log4j2.xml
| | |____spring
| | | |____applicationContext.xml 
| |____lib
| |____web.xml

上面是项目打包后的目录结构,其中dev.properties放在classes目录中,是因为applicationContext.xml中会引用此文件

<beans>
	<context:property-placeholder location="classpath:dev.properties"/>
	...
</beans>

打包前

.
|____main
| |____java
| | |____com
| |____resources
| | |____dev 开发环境
| | | |____dev.properties
| | |____log4j2.xml
| | |____pro 生产环境
| | | |____dev.properties
| | |____spring
| | | |____applicationContext.xml
| | |____test 测试环境
| | | |____dev.properties
| |____webapp
| | |____index.jsp
| | |____WEB-INF
| | | |____web.xml

可以看到最终的dev.properties文件,最初是放在resources的dev,pro,test目录下.它们文件名相同,但是其中的内容分别对应开发环境,线上环境和测试环境的配置.

POM插件配置

<project><!--因篇幅原因,省略了pom文件中的一些配置-->
	<modelVersion>4.0.0</modelVersion>
	<artifactId>dev-demo</artifactId>
	<packaging>war</packaging>
	<version>1.0.0</version>
	
    <!-- 配置文件 -->
    <profiles>
        <profile><!-- 研发环境 -->
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile><!-- 测试环境 -->
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
        <profile><!-- 线上环境 -->
            <id>pro</id>
            <properties>
                <profiles.active>pro</profiles.active>
            </properties>
        </profile>
    </profiles>

	<build>
        <finalName>${project.artifactId}-${project.version}.${project.packaging}</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>
                <excludes>
                    <exclude>test/*</exclude>
                    <exclude>pro/*</exclude>
                    <exclude>dev/*</exclude>
                </excludes>
            </resource>

            <!-- 指定环境配置文件导入 -->
            <resource>
                <directory>src/main/resources/${profiles.active}</directory>
            </resource>
        </resources>

        <plugins>
            <!-- 编译 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- 打包 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.2</version>
            </plugin>
        </plugins>
    </build>
</project>

打包命令

CMD

mvn clean compile war:exploded -Pdev

IDEA

140714_W6Rm_2253738.png

ECLIPSE

140518_RRz8_2253738.png

转载于:https://my.oschina.net/cnarthurs/blog/731134

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值