首页 > 其他 > 详细

maven-maven-resources-plugin插件使用

时间:2021-05-17 13:37:41      阅读:24      评论:0      收藏:0      [点我收藏+]

修改默认读取资源地址

默认情况下maven会读取src/main/resources的资源打包到target的classes 可以通过进行修改

    <build>
        <resources>
                <!--读取资源1-->
        <resource>
            <directory>src/main/resources1</directory>
        </resource>
                <!--读取资源2-->
        <resource>
            <directory>src/main/resources2</directory>
        </resource>
        </resources>
        <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <!--使用默认的变量分割符即${} 可以自己定义格式-->
            <configuration>
                <useDefaultDelimiters>true</useDefaultDelimiters>
            </configuration>
            <version>3.1.0</version>
        </plugin>
    </plugins>
    </build>

占位符替换

src/main/resources 下的yml配置文件 打包后则会默认填充

spring:
  profiles:
    active: ${spring.profiles.active}
    name: ${name}

 

<!--定义占位符-->    
<properties>
        <java.version>1.8</java.version>
        <name>version</name>
        <spring.profiles.active>pro</spring.profiles.active>
    </properties>
    <!--也可以定义在profile里面 如果写在profile就是针对profile的build-->
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!--这个配置的意思是过滤上面指定属性文件中的占位符,占位符是${变量名称}这样的形式,maven会自动读取includes配置文件,然后解析其中的占位符,使用上面pom文件中定义的属性进行替换-->
                <filtering>true</filtering>
                <includes>
                    <include>*.yml</include>
                </includes>
                <!--可用于排除某些-->
<!--                <excludes>-->
<!--                    <exclude>file</exclude>-->
<!--                </excludes>-->
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <!--使用默认的变量分割符即${} 可以自己定义格式-->
                <configuration>
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                </configuration>
                <version>3.1.0</version>
            </plugin>
        </plugins>
    </build>

多环境配置 只需要在profile定义即可

 mvn clean install -Dmaven.test.skip -Denv=dev  打包后 则会填充对应的的占位符,注:profile的优先级比全局的properties定义优先级要高

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <!--没有指定变量默认激活-->
            <activeByDefault>true</activeByDefault>
            <!--maven打包的变量名和参数名字 如: mvn clean install -Dmaven.test.skip -Denv=dev-->
            <property>
                <name>env</name>
                <value>dev</value>
            </property>
        </activation>
        <!--占位符定义-->
        <properties>
            <spring.profiles.active>dev</spring.profiles.active>
            <name>dev</name>
        </properties>
    </profile>
    <profile>
        <id>test</id>
        <activation>
            <!--没有指定变量默认激活-->
            <activeByDefault>true</activeByDefault>
            <!--maven打包的变量名和参数名字 如: mvn clean install -Dmaven.test.skip -Denv=dev-->
            <property>
                <name>env</name>
                <value>test</value>
            </property>
        </activation>
        <!--占位符定义-->
        <properties>
            <spring.profiles.active>test</spring.profiles.active>
            <name>version2</name>
        </properties>
    </profile>
    <profile>
        <id>pro</id>
        <activation>
            <!--没有指定变量默认激活-->
            <activeByDefault>true</activeByDefault>
            <!--maven打包的变量名和参数名字 如: mvn clean install -Dmaven.test.skip -Denv=dev-->
            <property>
                <name>env</name>
                <value>pro</value>
            </property>
        </activation>
        <!--占位符定义-->
        <properties>
            <spring.profiles.active>pro</spring.profiles.active>
            <name>version3</name>
        </properties>
    </profile>
</profiles>

打包排除和包含文件

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
              <include>**/*.txt</include>
              <include>**/*.rtf</include>
            </includes>
            <excludes>
              <exclude>**/*.bmp</exclude>
              <exclude>**/*.jpg</exclude>
              <exclude>**/*.jpeg</exclude>
              <exclude>**/*.gif</exclude>
            </excludes>
        </resource>
    <resources>
</build>

改变输出目录

 

maven-maven-resources-plugin插件使用

原文:https://www.cnblogs.com/LQBlog/p/14775703.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!