maven中占位符配置(动态部署)

        maven占位符类似于EL表达式,通过${}来获取pom中定义的变量,这样可以根据不同的部署环境,将对应的xml文件中的变量进行替换(一般情况下,测试和开发环境引用的变量都是不一样的,如果手动替换,这样工作量会很大)。根据pom.xml中不同部署环境配置的变量,可以动态的将xml文件中引用变量替换,也可以将Index页面中引用的静态文件路径动态变换(详情见:https://blog.csdn.net/fz13768884254/article/details/81334951)。

        下面是项目中pom配置的一部分,简单阐述一下,有不到位的地方,请指正,谢谢阅览:

<build>
    <finalName>ROOT</finalName>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>               <!--https://blog.csdn.net/fz13768884254/article/details/81189473-->
                <include>**/*.xml</include>     <!--必须配置,读取项目resources包下的xml文件,并且将占位符替换-->
            </includes>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>

    <testResources>
        <testResource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.xml</include><!--必须配置,读取测试环境resources包下的xml文件,并且将占位符替换-->
            </includes>
        </testResource>
    </testResources>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
<!--You can use the mojo copy-resources to copy resources which are not in the default maven layout or not declared in the build/resources element and attach it to a phase-->
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <!-- here the phase you need -->
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <filtering>true</filtering>
                                <includes>
                                    <include>**/*.xml</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin><!--${project.build.directory} 构建目录,缺省为target,${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes-->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <outputDirectory>${project.build.directory}/artifact</outputDirectory>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <resource><!--https://blog.csdn.net/fz13768884254/article/details/81334951-->
                        <filtering>true</filtering>
                        <directory>src/main/webapp</directory>
                        <includes>
                            <include>index.html</include>
                            <include>**/*.xml</include><!--webapp中web.xml和index的动态替换-->
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>

</build>
<profiles><!--部署环境-->
    <profile>
        <id>local</id><!--local环境-->
        <build>
            <resources>
                <resource>
                    <directory>src/main/profiles/local</directory>
                </resource>
            </resources>
        </build>
        <properties>
            <profile.env>local</profile.env><!--变量,标识环境-->
            <log.root.level>INFO</log.root.level>
            <log.logger.level>DEBUG</log.logger.level>
            <log.console.level>INFO</log.console.level> 
        </properties>
    </profile>
    <profile>
        <id>product</id><!--生产部署环境-->
        <build>
            <resources>
                <resource>
                    <directory>src/main/profiles/product</directory>
                </resource>
            </resources>
        </build>
        <properties>
            <profile.env>product</profile.env><!--product-->
            <log.root.level>INFO</log.root.level>
            <log.logger.level>DEBUG</log.logger.level>
            <log.console.level>INFO</log.console.level>
           <!--相关占位符变量配置,例如:xml中配置为${node.path},则pom进行如下配置-->
           <node.path>main.js</node.path>
        </properties>
    </profile>

</profiles>

猜你喜欢

转载自blog.csdn.net/fz13768884254/article/details/81661409