欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Maven 打包实现生产环境与测试环境配置分离

程序员文章站 2024-03-12 08:29:38
...

在使用maven的过程中会发现一个问题,由于生产环境跟测试环境的配置不通,所以在分别打两个环境的包时,往往需要人工参与去修改配置文件,而人工参与的次数越大错误的几率就会越大,那maven能不能再不用人工修改配置文件的前提下来分别打两个环境的包呢,当然可以。

<!--也会供子项目继承-->
    <build>
        <!-- 插件管理,可供子项目继承 -->
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <resources>
            <resource>
                <directory>/Users/iss/sourceCode/gitProject/iss/maven-multi-module/core/src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
            <!--设置自动替换-->
            <resource>
                <directory>/Users/iss/sourceCode/gitProject/iss/maven-multi-module/core/src/main/resources</directory>
                <includes>
                    <include>jdbc.properties</include>
                </includes>
                <!--也可以用排除标签-->
                <!--<excludes></excludes>-->
                <!--开启过滤-->
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <!--<phase>clean</phase>-->
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>Fruit:${fruit}</echo>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


    <profiles>
        <profile>
            <id>product</id>
            <properties>
                <fruit>product</fruit>
            </properties>
            <build>
                <filters>
                    <filter>/Users/iss/sourceCode/gitProject/iss/maven-multi-module/filters/jdbc-product.properties</filter>
                </filters>
            </build>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <fruit>dev</fruit>
            </properties>
            <activation>
            <!-->默认是dev环境的<-->
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <filters>
                    <filter>/Users/iss/sourceCode/gitProject/iss/maven-multi-module/filters/jdbc-dev.properties</filter>
                </filters>
            </build>
        </profile>
    </profiles>

如果想要打生产环境的包

vn package  -Pproduct 

filter文件下包括2个配置文件
jdbc-dev.properties

jdbc.url=i am dev
jdbc.username=i am dev
jdbc.password=i am dev

jdbc-product.properties

jdbc.url=i am product
jdbc.username=i am product
jdbc.password=i am product

而resource目录下
jdbc.properties

jdbc.url=${jdbc.url}
jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}

maven在构建的时候会自动替换变量里的变量