java项目中index页的动态替换(maven-war-plugin)

原理类似于第三种配置方法:动态配置index中静态文件路径

利用maven占位符进行动态更换配置内容,实现index页面的动态替换(在打包时根据不同的部署环境,生成不同的web.xml文件):

1.在pom文件配置插件:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webResources>
                        <resource>
                            <filtering>true</filtering>
                            <directory>src/main/webapp</directory>
                            <includes>
                                <include>index.html</include>
                            </includes>
                            <includes>
                                <include>**/web.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>

2.在pom文件不同的部署环境配置占位符内容:

<profiles>
        <profile>
            <id>local</id>
            <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>
                
                <!--index页面静态资源配置-->
                <index.path>file.jd.com</index.path>
                 <!--index文件名,占位符内容-->
                <index.file>index.html</index.file>
            </properties>
        </profile>
        <profile>
            <id>development</id>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/profiles/development</directory>
                    </resource>
                </resources>
            </build>
            <properties>
                <profile.env>development</profile.env>
                <log.root.level>INFO</log.root.level>
                <log.logger.level>DEBUG</log.logger.level>
                <log.console.level>INFO</log.console.level>
                <log.base.path>/export/Logs/dev.ebooking-web.hotel.jd.com/log</log.base.path>

                <!--index页面静态资源配置-->
                <index.path>file.jd.com</index.path>
                 <!--index文件名,占位符内容,与local环境不同-->
                <index.file>demo.html</index.file>
            </properties>
        </profile>
</profiles>

3.在web.xml中进行配置占位符:

<welcome-file-list>
        <welcome-file>${index.file}</welcome-file>
</welcome-file-list>

4.选择install环境:

java项目中index页的动态替换(maven-war-plugin)

 这样生成的target就会生成web.xml:

java项目中index页的动态替换(maven-war-plugin)

web.xml文件就会会被替换:

local

java项目中index页的动态替换(maven-war-plugin)

 development

java项目中index页的动态替换(maven-war-plugin)