共享配置资源
write by valuewithTime, 2020-08-13 18:59引言
最近一个项目要做私有化,本身配置这一块,用的是配置中心,现在要兼容私有化和公有化的部署方式。考虑到避免针对不同的部署方式, 维护两套代码,计划配置模块化。私有话对用私有化的本地配置,公有化对应公有化的配置中。通过共享配置的模式,模块化配置,支持可插拔。
配置模块
在引言中描述的背景下,将模块查分出来,测试的时候发现除了问题,根本不起作用,网上搜了一番,多少多环境的配置,比如针对 开发,测试,预发,生成环境(dev,test, exp,prod)的配置分离,查到了共享配置是通过解压jar包方式,姿势不对,放弃了,感觉不考虑。使用英文Google一下, 找到答案,论英语的重要性。一个资源绑定的插件maven-remote-resources-plugin,具体连接见附件。从此可以看出,认知范围,直接影响你的思想及行为。
废话,少说,操作起来
在共享配置模块ananas-config中添加如下插件
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.7.0</version>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/properties/*.properties</include>
<include>**/conf/*.properties</include>
<include>**/conf/*.xml</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
</configuration>
</plugin>
在ananas-web模块中引用共享配置模块, 同时添加maven-remote-resources-plugin插件
<dependency>
<groupId>com.homestory</groupId>
<artifactId>ananas-config</artifactId>
<version>${ananas.config.version}</version>
</dependency>
...
<!--引用远程共享资源-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.7.0</version>
<configuration>
<resourceBundles>
<resourceBundle>com.homestory:ananas-config:${ananas.config.version}</resourceBundle>
</resourceBundles>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
走起,gone;
一顿操作猛如虎,Bingo。
附
引用文献
Sharing Resources
Changing the order of maven plugin execution
Maven 生命周期
classpath和classpath的区别](https://www.jianshu.com/p/a4b6904d90aa)
[Spring加载resource时classpath:与classpath:的区别
在延伸一下,如何动态修改环境变量呢?
How can I change a property in spring environment?
ConfigurableEnvironment
How to change/update/delete a property in ConfigurableEnvironment of Spring
profile:(dev,test, exp,prod;)
如果不在application-.{profile}properties的文件中,我们可以通过重写
PropertySource注解配置
@Component
@ConfigurationProperties(prefix = "spring.datasource")
@PropertySource(value = "classpath:properties/druidConfig.properties", encoding = "UTF-8")//需要设置UTF-8,防止乱码
@Data
public class DruidConfig {
...
}
注意PropertySource,不支持classpath*, 只支持classpath:
ananas-config build全配置
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/properties/*.properties</include>
<include>**/conf/*.properties</include>
<include>**/conf/*.xml</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.7.0</version>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/properties/*.properties</include>
<include>**/conf/*.properties</include>
<include>**/conf/*.xml</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
jar加压的方式共享资源文件
<!--打包依赖jar中的配置文件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.homestory</groupId>
<artifactId>ananas-config</artifactId>
<version>${ananas.config.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<includes>**/properties/*.properties</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
实测无效,亦或我的姿势不对;