Maven资源过滤

直接上图,一目了然

配置文件

web.xml文件配置

Maven资源过滤_第1张图片

pom.xml文件配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>aaa</groupId>
    <artifactId>aaa</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>aaa Maven Webapp</name>
    <url>http://maven.apache.org</url>


    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <profiles>

        <!--开发环境-->
        <profile>
            <id>dev</id>
            <!--在这里定义property就相当于在property文件中定义属性一样-->
            <properties>
                <env>dev</env>
            </properties>
            <build>
                <filters>
                    <!--每个property文件都需要写一个filter-->
                    <filter>src/main/env/dev/dubbo.properties</filter>
                    <filter>src/main/env/dev/mysql.properties</filter>
                    <filter>src/main/env/dev/sms.properties</filter>
                </filters>
            </build>
        </profile>

        <!--测试环境-->
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
            <build>
                <filters>
                    <filter>src/main/env/test/dubbo.properties</filter>
                    <filter>src/main/env/test/mysql.properties</filter>
                    <filter>src/main/env/test/sms.properties</filter>
                </filters>
            </build>
        </profile>

        <!--线上环境-->
        <profile>
            <id>product</id>
            <properties>
                <env>product</env>
            </properties>
            <build>
                <filters>
                    <filter>src/main/env/product/dubbo.properties</filter>
                    <filter>src/main/env/product/mysql.properties</filter>
                    <filter>src/main/env/product/sms.properties</filter>
                </filters>
            </build>
        </profile>
    </profiles>


    <!--配置所有profile都公用的信息-->
    <build>

        <finalName>aaa</finalName>

        <!--指定资源目录 配置是否启用资源过滤(就是是否启用占位符替换)-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>


        <!--通过这个插件可以对web.xml文件进行资源过滤-->
        <plugins>
            <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>**/web.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

执行效果(以开发环境示例)

你可能感兴趣的:(maven)