maven-maven-resources-plugin插件使用
修改默认读取资源地址
默认情况下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>
改变输出目录
<resource> <directory>src/main/resources</directory> <targetPath>d</targetPath> </resource>
如:
打包后
<!-- 生产环境指定profile为acm,接入阿里云应用配置服务 --> <profiles> <profile> <id>acm</id> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>0.2.1.RELEASE</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/resources/canal/pro/</directory> <targetPath>canal</targetPath> </resource> <resource> <directory>src/main/resources</directory> <excludes> <exclude>canal/pro/*.yml</exclude> <exclude>canal/test/*.yml</exclude> </excludes> </resource> </resources> </build> </profile> <profile> <id>test</id> <activation> <!--没有指定变量默认激活--> <activeByDefault>true</activeByDefault> </activation> <build> <resources> <resource> <directory>src/main/resources/canal/test/</directory> <targetPath>canal</targetPath> </resource> <resource> <directory>src/main/resources</directory> <excludes> <exclude>canal/test/*.yml</exclude> <exclude>canal/pro/*.yml</exclude> </excludes> </resource> </resources> </build> </profile> </profiles>