码迷,mamicode.com
首页 > 其他好文 > 详细

配置开发环境测试环境线上生产环境

时间:2017-05-24 22:48:05      阅读:382      评论:0      收藏:0      [点我收藏+]

标签:加载   resources   height   web   top   成功   png   get   artifact   

    1.正确打包

        项目有三种环境:

        1.本地开发环境(local)

        2.开发测试环境(dev)

        3.线上生产环境(product)

        不同的环境有不同的配置,比如数据库连接什么的....maven打包时默认去resources文件夹下打包这些配置文件,放在WEB-INF/classes下,然后再打成war包,就能用了...现在通过修改pom.xml文件,增加三种配置,让maven打包时选择打包不同文件夹下的配置文件到WEB-INF/classes下,这样就省事儿了....

        如图所示,resources下dev,local,product三个文件夹,分别对应三种环境。

技术分享

下面是pom.xml主要修改:

<!--配置参数-->
<profiles>
    <profile>
        <id>local</id>
        <properties>
            <package.environment>local</package.environment>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>product</id>
        <properties>
            <package.environment>product</package.environment>
        </properties>
    </profile>
    <profile>
        <id>dev</id>
        <properties>
            <package.environment>dev</package.environment>
        </properties>
    </profile>
</profiles> 
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>local/*</exclude>
                <exclude>product/*</exclude>
                <exclude>dev/*</exclude>
            </excludes>
        </resource>
    </resources>
  <!-- war打包插件, 设定war包名称不带版本号 -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
            <warSourceExcludes>*/lib/jsp-api-2.2.jar</warSourceExcludes>
            <warName>gcTaobao</warName>
            <webResources>
                <!--动态指定文件-->
                <resource>
                    <directory>src/main/resources/${package.environment}</directory>
                    <targetPath>WEB-INF/classes</targetPath>
                    <filtering>true</filtering>
                </resource>
            </webResources>
        </configuration>
    </plugin></build>

1. <profiles>标签里的三个<profile>是分别指local , dev ,product 三个参数。

2.<resources>标签里<excludes>确认在打包时不打包对应三个文件夹以及里面的文件。

3.org.apache.maven.plugins插件<webResources>动态指定参数${package.environment}对应文件夹下的文件到WEB-INF/classes下。

到此就配置好了:

下面是maven命令

然后就可以用mvn -P local package 或者mvn -P install 就可以打包成功了。

2.jetty本地debug配置

    按照上面的配置,打出的war可以正确使用,但是在使用maven jetty插件时,jetty自动加载的时target/classes中的文件。你可以发现使用上面的配置编译出来的target/classes的文件里没有对应的jdbc等文件。这里的解决方案是更改jetty加载classes路径来解决。

    具体配置:

<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<properties>
    <artifactId>AAA</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</properties>
<build>
    <!-- 配置加入jetty服务器,开发时我们一般使用jetty服务器 -->
    <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <configuration>
            <!--jetty:run指定加载classes路径-->
            <classesDirectory>target/${artifactId}-${version}/WEB-INF/classes</classesDirectory>
            <!-- 设置扫描target/classes内部文件变化时间间隔 -->
            <scanIntervalSeconds>10</scanIntervalSeconds>
            <webApp>
                <contextPath>/</contextPath>
            </webApp>
            <stopKey/>
            <stopPort/>
        </configuration>
    </plugin>
</build>

    maven打包时生成 一个 artifactId-version的文件夹,里面有对应的WEB-INF/classes文件,<classesDirectory>标签指定jetty加载里面的classes下文件,里面有也有对应的配置文件,这样就可以正常本地启动了。

3.idea中的操作

    install直接选择具体参数,然后点击install就可以了,多选参数时,会出现覆盖的情况。

    如图

技术分享

配置开发环境测试环境线上生产环境

标签:加载   resources   height   web   top   成功   png   get   artifact   

原文地址:http://www.cnblogs.com/nunuAction/p/6901000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!