如何使用 Intelij Idea + Maven 实现区分环境并快速编译生成不同环境 war 包

来源:互联网 发布:初中生编程比赛语言 编辑:程序博客网 时间:2024/06/02 03:55
1.需要在 pom.xml 文件中增加以下配置文件  <project></project>中的 <packaging>war</packaging>  其中resources_dev,resources_test,resources_pro 文件夹需要创建在与resources同级的目录下,这三个文件夹可以存放不同环境的jdbc.properties等配置文件    <profiles><!-- 系统环境切换 dev为开发环境配置, test为测试环境配置,pro为生产环境配置 --><profile><id>dev</id><properties><env>resources_dev</env></properties><activation><activeByDefault>true</activeByDefault></activation></profile><profile><id>test</id><properties><env>resources_test</env></properties><activation><activeByDefault>false</activeByDefault></activation></profile><profile><id>pro</id><properties><env>resources_pro</env></properties><activation><activeByDefault>false</activeByDefault></activation></profile></profiles>2.需要在 pom.xml 的 <build></build> 节点中增加以下配置文件        <!-- war包的名称,全局 -->    <finalName>SnailCore</finalName>        <!-- 指定项目中class对应的java目录 -->    <sourceDirectory>src/main/java</sourceDirectory>        <!-- 指定需要编译的资源文件目录 -->    <resources>        <!-- 指定通用的资源文件目录 -->        <resource>            <directory>${basedir}/src/main/resources/</directory>                        <!-- 指定通用的,编译的资源文件目录中所包含的文件类型 -->            <includes>                <include>*.*</include>                <include>**/*.xml</include>                <include>**/*.properties</include>            </includes>            <filtering>true</filtering>        </resource>                <!-- 指定需要区分环境的,编译的资源文件目录中所包含的文件类型 -->        <resource>            <directory>${basedir}/src/main/${env}</directory>            <includes>                <include>*.*</include>                <include>**/*.xml</include>                <include>**/*.properties</include>            </includes>            <filtering>true</filtering>        </resource>    </resources>        如果 mapping.xml,或者其他文件类型文件没有存放在resources通用目录下还需要配置一个resource标签使class输出目录中包含该类文件,配置方法请参考:    <resource>        <directory>src/main/java</directory>        <includes>            <include>**/*.xml</include>            <include>**/*.ttf</include>            <include>**/*.properties</include>        </includes>    </resource>     3.需要在 pom.xml 的 <build></build> 节点中增加以下配置文件     <plugins>        <!--maven打包插件 begin-->        <plugin>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-compiler-plugin</artifactId>            <version>3.3</version>            <configuration>                <source>1.7</source>                <target>1.7</target>                <encoding>utf8</encoding>                <compilerArguments>                    <verbose />                    <!--Windows 系统请使用此配置-->                    <!--<bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>-->                    <!--Mac 系统请使用此配置-->                    <bootclasspath>${java.home}/lib/rt.jar:${java.home}/lib/jce.jar</bootclasspath>                </compilerArguments>            </configuration>        </plugin>        <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-surefire-plugin</artifactId>        <version>2.12.4</version>        <configuration>        <skip>true</skip>        <forkMode>once</forkMode>        <argLine>-Dfile.encoding=UTF-8</argLine>        </configuration>        </plugin>        <plugin>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-war-plugin</artifactId>            <configuration>                <!-- 此处指定需要打包项目的静态资源文件的目录,包括jsp,js,static,css,images等等 -->                <warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory>                <failOnMissingWebXml>true</failOnMissingWebXml>            </configuration>            <version>2.2</version>        </plugin>        <!--maven打包插件 end-->    </plugins>      4.配置不同环境生成war包的配置项      1). 将鼠标移动到idea左下角,出现窗口菜单    2). 选择Maven Projects, 则在idea的右侧会出现 Maven Projects 面板        在面板中的目录有        [Profiles] --> 包含了[1]中所配置的环境切换开关        [项目名称] --> [Lifecycle],[Plugins],[Dependencies],我们配置好了以后还会多出来一个[Run Configurations]    3). 展开[Lifecycle]会看到我们将要用到的工具[clean],[validate],[compile],[test],[package],[verify],[install],[site],[deploy]    4). 在[package]上点击右键,选择 Create '项目名称 [package...]'    5). 在弹出的窗口中需要填写一下信息        Name : 创建的配置名称,比如要创建生成开发环境的war包,可以写成 war_dev (这里可以自定义)        在Parameter选项卡中需要填写一下信息            Working Directory : 项目的路径 (这里一般会自动带出)            Command line : clean package -Dmaven.test.skip-true (clean 清理项目,会删除老的编译后的文件; package 生成war包; -Dmaven.test.skip-true 不打包不变异测试代码)            Profiles (separated with space) : dev  (这里填写的就是 profiles 中对应的需要生成环境的值 dev/test/pro)        点击右下角确定完成即可        生成 开发,测试,生产的war包配置项方法相同    6). 在上一步配置好后,会在 2) 中的[Run Configurations] 中出现已经配置的 生成war包的配置项       如果需要生成对应环境的war包双击该配置项即可完成,在控制台可以看到整个生成war包的日志,如果有报错请自行调整        5.配置不同环境生成的编译配置项      1). 将鼠标移动到idea左下角,出现窗口菜单    2). 选择Maven Projects, 则在idea的右侧会出现 Maven Projects 面板        在面板中的目录有        [Profiles] --> 包含了(1)中所配置的环境切换开关        [项目名称] --> [Lifecycle],[Plugins],[Dependencies],我们配置好了以后还会多出来一个[Run Configurations]    3). 展开[Lifecycle]会看到我们将要用到的工具[clean],[validate],[compile],[test],[package],[verify],[install],[site],[deploy]    4). 在[install]上点击右键,选择 Create '项目名称 [install...]'    5). 在弹出的窗口中需要填写一下信息        Name : 创建的配置名称,比如要创建编译开发环境的配置项,可以写成 build_dev (这里可以自定义)        在Parameter选项卡中需要填写一下信息            Working Directory : 项目的路径 (这里一般会自动带出)            Command line : clean install -Dmaven.test.skip-true (clean 清理项目,会删除老的编译后的文件; install 编译项目; -Dmaven.test.skip-true 不打包不变异测试代码)            Profiles (separated with space) : dev  (这里填写的就是 profiles 中对应的需要生成环境的值 dev/test/pro)        点击右下角确定完成即可        生成 开发,测试,生产的编译配置项方法相同    6). 在上一步配置好后,会在 2) 中的[Run Configurations] 中出现已经配置的 编译项目的配置项        如果需要编译对应环境文件双击该配置项即可完成,在控制台可以看到整个编译项目的日志,如果有报错请自行调整          6.针对 [4] [5] 的说明: 为了避免编译或者生产war包出错,建议将所有外部jar包全部配置在 pom.xml 文件中        在 pom.xml 配置文件中配置本地lib下的外部jar包方法如下:                    <!--以下是外部jar包路径-->            <dependency>                <!-- 这里填写war包的name -->                <groupId>apache-ant-zip</groupId>                <!-- 这里填写artifactId 如果不知道如何填写可与groupId相同 -->                <artifactId>apache-ant-zip</artifactId>                <!-- 这里填写version版本号 请对应自己lib下的jar包填写 -->                <version>2.3</version>                <!-- 这里必须这样写 -->                <scope>system</scope>                <!--                    这里配置lib的目录                     ${lib.path}需要在 pom.xml的 <properties></properties> 中增加以下代码(<lib.path>这里是你自己的lib目录路径</lib.path>)                    <lib.path>${basedir}/src/main/webapp/WEB-INF/lib</lib.path>                -->                <systemPath>${lib.path}/apache-ant-zip-2.3.jar</systemPath>            </dependency>                .                .                .   7.配置不同环境的tomcat运行环境     1). 创建tomcat(这里不做说明,不会请百度或google)    2). 点击idea 右上角倒三角,选择[Edit Configurations...]    3). 在弹出的窗口中需完成以下配置        tomcat的基本配置(这里不做说明,不会请百度或google),name可以填写[project_dev] (这里可以自定义)        在tomcat配置的[Server]选项卡中的最下方找到 [Before Launch] 面板并展开        在展开的面板中点击"+"按钮在弹出的窗口中选择[Run Anther Configuration]        在弹出的窗口中选择 [5] 中配置好的编译配置项 例如: build_dev                创建 开发,测试,生产的创建tomcat配置项方法相同        如果不需要本地测试某个环境的话,尽量不要使用这里配置tomcat,原因是这里配置的tomcat会重新编译项目,时间会比较长(建议使用基本的tomcat配置即可)
原创粉丝点击