1. 程式人生 > >java專案中index頁的動態替換(maven-war-plugin)

java專案中index頁的動態替換(maven-war-plugin)

利用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環境:

 這樣生成的target就會生成web.xml:

web.xml檔案就會會被替換:

local

 development