Maven为不一样环境配置打包

1 应用场景

在开发过程当中常常要遇到为不一样的环境打包,这里面最主要的问题在于,不一样环境的配置是不同的,若是为不一样环境打包每次都手工修改配置,那不但工做量大,并且很容易出错。apache

2 配置pom文件

<profiles>
        <profile>
            <!-- 开发环境 -->
            <id>dev</id>
            <properties>
                <!-- 经过env设置当前工做环境(重要) -->
                <env>dev</env>
            </properties>
            <build>
                <finalName>${project.name}</finalName>
                <!--  -->
                <resources>
                    <resource>
                        <!-- 定义resource所在的文件夹 -->
                        <directory>src/main/resources</directory>
                        <includes>
                            <include>**/*.properties</include>
                            <include>**/*.xml</include>
                        </includes>
                        <filtering>true</filtering>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <!-- tomcat7-maven-plugin -->
                            <groupId>org.apache.tomcat.maven</groupId>
                            <artifactId>tomcat7-maven-plugin</artifactId>
                            <version>2.2</version>
                            <executions>
                                <execution>
                                    <id>run-war-only</id>
                                    <phase>pre-integration-test</phase>
                                    <goals>
                                        <goal>run-war-only</goal>
                                    </goals>
                                </execution>
                            </executions>
                            <configuration>
                                <warDirectory>target/${project.name}</warDirectory>
                                <path>/</path>
                                <contextReloadable>true</contextReloadable>
                                <uriEncoding>UTF-8</uriEncoding>
                                <!-- 指定启动端口 -->
                                <port>${server.port}</port>
                            </configuration>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>            
        </profile>
</profiles>

3 修改xml文件读取配置文件的路径

4 Tomcat打包热部署测试

1.在tomcat的安装目录下,修改conf / tomcat-user.xml文件,在<tomcat-users> 节点下面增长以下配置:tomcat

  1. <role rolename="manager-gui" />  
  2. <role rolename="manager-script" />  
  3. <user username="tomcat" password="tomcat" roles="manager-gui, manager-script" /> 

2.在maven中添加server,配置tomcat的管理员账号密码maven

如今tomcat开启了权限,maven既然要操做tomcat,那么maven就要拿到tomcat的管理员账号和密码才可以使用。测试

在maven的安装目录下,修改conf / setting.xml文件.在<server> 节点下面增长以下配置:ui

  1. <server>    
  2.        <id>admin</id>    
  3.        <username>tomcat</username>    
  4.        <password>tomcat</password>    
  5. </server>  

3.在project中添加插件,以及maven中配置的server,url

如今maven已经拥有操做tomcat的权限了,可是这二者之间想要通讯的话还须要一个桥梁,那就是在maven中配置tomcat插件.spa

修改项目的pom.xml文件,在<build> 节点下面增长以下配置:插件

  1. <plugins>  
  2.             <!-- 第一种方式: apache官方tomcat插件,支持deploy -->  
  3.             <plugin>  
  4.                 <groupId>org.apache.tomcat.maven</groupId>  
  5.                 <artifactId>tomcat7-maven-plugin</artifactId>  
  6.                 <version>2.0-SNAPSHOT</version>  
  7.                 <configuration>  
  8.                     <url>http://localhost:8080/manager/text</url>  
  9.                     <server>admin</server>  
  10.                 </configuration>  
  11.             </plugin>  
  12.             <!-- 第二种方式: 第三方tomcat插件,支持redeploy -->  
  13.             <plugin>  
  14.                 <groupId>org.codehaus.mojo</groupId>  
  15.                 <artifactId>tomcat-maven-plugin</artifactId>  
  16.                 <version>1.1</version>  
  17.                 <configuration>  
  18.                     <url>http://localhost:8080/manager/text</url>  
  19.                     <server>admin</server>  
  20.                     <ignorePackaging>true</ignorePackaging>  
  21.                 </configuration>  
  22.             </plugin>  
  23. </plugins>  

4.打包命令code

clean:clean package -P dev tomcat7:run-war-only -f pom.xml

说明:server

  • dev为当前工做环境
  • 使用tomcat7打包
相关文章
相关标签/搜索