zoukankan      html  css  js  c++  java
  • 【maven】之配置开发,测试,正式环境pom.xml文件

    在进行web程序开发,如果项目组没有使用自动化发布工具(jenkins + maven + svn + tomcat ),我们一般会使用maven的热部署来完成发布,在部署的过程中我们开发,测试,生产环境的配置文件都是不一样的,如果是手动改动配置文件,存在很大的隐患,本文使用profile来处理这个问题!

    关于maven的profile配置的描述,

    profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。比如说,我们可以通过profile定义在jdk1.5以上使用一套配置信息,在jdk1.5以下使用另外一套配置信息;或者有时候我们可以通过操作系统的不同来使用不同的配置信息,比如windows下是一套信息,linux下又是另外一套信息,等等。具体的激活条件有哪些我在后文会讲到。

    首先我们在/src/main/resources下建立三个配置文件

    /src/main/resources/dev

    /src/main/resources/test

    /src/main/resources/pro

    在pom.xml中

    <profiles>
            <!-- 开发 默认激活 -->
            <profile>
                <id>dev</id>
                <properties>
                    <env>dev</env>
                </properties>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
            </profile>
            <!-- 测试服务器 -->
            <profile>
                <id>test</id>
                <properties>
                    <env>test</env>
                </properties>
                
            </profile>
            <!-- 生产服务器 -->
            <profile>
                <id>pro</id>
                <properties>
                    <env>pro</env>
                </properties>
            </profile>
        </profiles>

    接着在build里面写上resources配置

    <resources>
                <resource>
                    <directory>${project.basedir}/src/main/resources/${env}</directory>
                    <includes>
                        <include>*.*</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
                <resource>
                     <directory>src/main/resource</directory>  
                </resource>
                <resource>  
                    <directory>src/main/java</directory>  
                    <includes>  
                        <include>**/*.properties</include>  
                    </includes>  
                    <filtering>false</filtering>  
                   </resource>  
            </resources>

    tomcat7热部署配置

               <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                         <port>8088</port>
                         <path>/maven</path>
                         <uriEncoding>UTF-8</uriEncoding>
                         <!-- 启动tomcat热部署 -->
                         <url>http://127.0.0.1:8081/manager/text</url>
                          <username>admin</username>
                           <password>admin</password>
                    </configuration>
                </plugin>

    tomcat的tomcat-user.xml配置

     <role rolename="admin-gui"/> 
      <role rolename="manager-script"/>
      <role rolename="manager-gui"/>
      <user username="admin" password="admin" roles=" admin-gui , manager-gui,manager-script "/>

    直接在maven界面的Goals

    执行clean package -Ptest tomcat7:redeploy 

    即可以将应用发布到测试服务器,引用的配置文件是test下的配置文件

    发布到正式环境只需要更改热部署tomcat的url

    执行clean package -Ppro tomcat7:redeploy 

     本地开发,默认加载执行的就是dev下配置文件

  • 相关阅读:
    软件开发项目失败原因分析
    浅析ASP.NET三层架构(原创)
    ASP.NET中常用的26个优化性能方法
    软件设计原则
    垃圾回收(GC,Garbage Collection)机制
    值类型和引用类型的区别
    在OpenWrt中使用socket通信
    在windows下通过samba的共享编辑过的openwrt的样式文件无法访问的解决办法
    在OpenWrt中保存数据到config
    将openwrt的系统同步时间的显示修改为19700312 14:20的格式
  • 原文地址:https://www.cnblogs.com/gyjx2016/p/6170709.html
Copyright © 2011-2022 走看看