maven根据不一样环境 不一样配置打包

    1.正确打包

        项目有三种环境:web

        1.本地开发环境(local)数据库

        2.开发测试环境(dev)apache

        3.线上生产环境(product)api

        不一样的环境有不一样的配置,好比数据库链接什么的....maven打包时默认去resources文件夹下打包这些配置文件,放在WEB-INF/classes下,而后再打成war包,就能用了...如今经过修改pom.xml文件,增长三种配置,让maven打包时选择打包不一样文件夹下的配置文件到WEB-INF/classes下,这样就省事儿了....服务器

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

下面是pom.xml主要修改:maven

<!--配置参数-->
<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 三个参数。ide

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

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

到此就配置好了:

下面是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就能够了,多选参数时,会出现覆盖的状况。

    如图

相关文章
相关标签/搜索