├── .coveralls.yml ├── settings.gradle ├── src ├── test │ ├── resources │ │ ├── application.yml │ │ └── config │ │ │ ├── custom-test2.yml │ │ │ ├── custom-test3.yml │ │ │ ├── custom-test4.yml │ │ │ ├── custom-test1.yml │ │ │ ├── custom-test6.yml │ │ │ ├── custom-test7.yml │ │ │ ├── custom-test8.yml │ │ │ └── custom-test5.yml │ └── java │ │ └── com │ │ └── github │ │ └── kingbbode │ │ └── custom │ │ └── yaml │ │ └── importer │ │ └── processor │ │ ├── Test2Properties.java │ │ ├── Test3Properties.java │ │ ├── Test4Properties.java │ │ ├── Test5Properties.java │ │ ├── Test6Properties.java │ │ ├── Test7Properties.java │ │ ├── Test8Properties.java │ │ ├── Test1Properties.java │ │ ├── SpringBootTestApplication.java │ │ └── properties │ │ ├── LocalPropertiesLoadTest.java │ │ ├── StubPropertiesLoadTest.java │ │ ├── DevPropertiesLoadTest.java │ │ ├── RealStubPropertiesLoadTest.java │ │ └── DevStubPropertiesLoadTest.java └── main │ ├── resources │ └── META-INF │ │ └── spring.factories │ └── java │ └── com │ └── github │ └── kingbbode │ └── custom │ └── yaml │ └── importer │ └── processor │ └── CustomYamlPropertiesEnvironmentPostProcessor.java ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .travis.yml ├── .gitignore ├── gradlew.bat ├── README.md └── gradlew /.coveralls.yml: -------------------------------------------------------------------------------- 1 | repo_token: 7cgptjtflIOzyHGs39iZJ8wTpO1h3GvYC -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'spring-boot-custom-yaml-importer' 2 | 3 | -------------------------------------------------------------------------------- /src/test/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: local -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kingbbode/spring-boot-custom-yaml-importer/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.env.EnvironmentPostProcessor=\ 2 | com.github.kingbbode.custom.yaml.importer.processor.CustomYamlPropertiesEnvironmentPostProcessor -------------------------------------------------------------------------------- /src/test/resources/config/custom-test2.yml: -------------------------------------------------------------------------------- 1 | test2: 2 | test: a 3 | 4 | --- 5 | spring: 6 | profiles: beta 7 | 8 | test2: 9 | test: b 10 | 11 | --- 12 | spring: 13 | profiles: real 14 | 15 | test2: 16 | test: d -------------------------------------------------------------------------------- /src/test/resources/config/custom-test3.yml: -------------------------------------------------------------------------------- 1 | test3: 2 | test: a 3 | 4 | --- 5 | spring: 6 | profiles: beta, stub 7 | 8 | test3: 9 | test: b 10 | 11 | --- 12 | spring: 13 | profiles: real 14 | 15 | test3: 16 | test: d -------------------------------------------------------------------------------- /src/test/resources/config/custom-test4.yml: -------------------------------------------------------------------------------- 1 | test4: 2 | test: a 3 | 4 | --- 5 | spring: 6 | profiles: 7 | - beta 8 | - stub 9 | 10 | test4: 11 | test: b 12 | 13 | --- 14 | spring: 15 | profiles: real 16 | 17 | test4: 18 | test: d -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /src/test/resources/config/custom-test1.yml: -------------------------------------------------------------------------------- 1 | test1: 2 | test: a 3 | 4 | --- 5 | spring: 6 | profiles: beta 7 | 8 | test1: 9 | test: b 10 | 11 | --- 12 | spring: 13 | profiles: stub 14 | 15 | test1: 16 | test: c 17 | 18 | --- 19 | spring: 20 | profiles: real 21 | 22 | test1: 23 | test: d -------------------------------------------------------------------------------- /src/test/resources/config/custom-test6.yml: -------------------------------------------------------------------------------- 1 | test6: 2 | test: a 3 | 4 | --- 5 | spring: 6 | config: 7 | activate: 8 | on-profile: beta 9 | 10 | test6: 11 | test: b 12 | 13 | --- 14 | spring: 15 | config: 16 | activate: 17 | on-profile: real 18 | 19 | test6: 20 | test: d -------------------------------------------------------------------------------- /src/test/resources/config/custom-test7.yml: -------------------------------------------------------------------------------- 1 | test7: 2 | test: a 3 | 4 | --- 5 | spring: 6 | config: 7 | activate: 8 | on-profile: beta, stub 9 | 10 | test7: 11 | test: b 12 | 13 | --- 14 | spring: 15 | config: 16 | activate: 17 | on-profile: real 18 | 19 | test7: 20 | test: d -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - openjdk8 4 | 5 | branches: 6 | only: 7 | - master 8 | 9 | cache: 10 | directories: 11 | - '$HOME/.m2/repository' 12 | - '$HOME/.gradle' 13 | 14 | script: "./gradlew clean test" 15 | 16 | after_success: 17 | - ./gradlew jacocoTestReport coveralls -------------------------------------------------------------------------------- /src/test/resources/config/custom-test8.yml: -------------------------------------------------------------------------------- 1 | test8: 2 | test: a 3 | 4 | --- 5 | spring: 6 | config: 7 | activate: 8 | on-profile: 9 | - beta 10 | - stub 11 | 12 | test8: 13 | test: b 14 | 15 | --- 16 | spring: 17 | config: 18 | activate: 19 | on-profile: real 20 | 21 | test8: 22 | test: d -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /build/ 3 | !gradle/wrapper/gradle-wrapper.jar 4 | out 5 | credential-application.yml 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | 15 | ### IntelliJ IDEA ### 16 | .idea 17 | *.iws 18 | *.iml 19 | *.ipr 20 | 21 | ### NetBeans ### 22 | nbproject/private/ 23 | build/ 24 | nbbuild/ 25 | dist/ 26 | nbdist/ 27 | .nb-gradle/ -------------------------------------------------------------------------------- /src/test/resources/config/custom-test5.yml: -------------------------------------------------------------------------------- 1 | test5: 2 | test: a 3 | 4 | --- 5 | spring: 6 | config: 7 | activate: 8 | on-profile: beta 9 | 10 | test5: 11 | test: b 12 | 13 | --- 14 | spring: 15 | config: 16 | activate: 17 | on-profile: stub 18 | 19 | test5: 20 | test: c 21 | 22 | --- 23 | spring: 24 | config: 25 | activate: 26 | on-profile: real 27 | 28 | test5: 29 | test: d -------------------------------------------------------------------------------- /src/test/java/com/github/kingbbode/custom/yaml/importer/processor/Test2Properties.java: -------------------------------------------------------------------------------- 1 | package com.github.kingbbode.custom.yaml.importer.processor; 2 | 3 | import lombok.Getter; 4 | import lombok.NoArgsConstructor; 5 | import lombok.Setter; 6 | import org.springframework.boot.context.properties.ConfigurationProperties; 7 | 8 | @Getter 9 | @Setter 10 | @NoArgsConstructor 11 | @ConfigurationProperties(prefix = "test2") 12 | public class Test2Properties { 13 | private String test; 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/github/kingbbode/custom/yaml/importer/processor/Test3Properties.java: -------------------------------------------------------------------------------- 1 | package com.github.kingbbode.custom.yaml.importer.processor; 2 | 3 | import lombok.Getter; 4 | import lombok.NoArgsConstructor; 5 | import lombok.Setter; 6 | import org.springframework.boot.context.properties.ConfigurationProperties; 7 | 8 | @Getter 9 | @Setter 10 | @NoArgsConstructor 11 | @ConfigurationProperties(prefix = "test3") 12 | public class Test3Properties { 13 | private String test; 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/github/kingbbode/custom/yaml/importer/processor/Test4Properties.java: -------------------------------------------------------------------------------- 1 | package com.github.kingbbode.custom.yaml.importer.processor; 2 | 3 | import lombok.Getter; 4 | import lombok.NoArgsConstructor; 5 | import lombok.Setter; 6 | import org.springframework.boot.context.properties.ConfigurationProperties; 7 | 8 | @Getter 9 | @Setter 10 | @NoArgsConstructor 11 | @ConfigurationProperties(prefix = "test4") 12 | public class Test4Properties { 13 | private String test; 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/github/kingbbode/custom/yaml/importer/processor/Test5Properties.java: -------------------------------------------------------------------------------- 1 | package com.github.kingbbode.custom.yaml.importer.processor; 2 | 3 | import lombok.Getter; 4 | import lombok.NoArgsConstructor; 5 | import lombok.Setter; 6 | import org.springframework.boot.context.properties.ConfigurationProperties; 7 | 8 | @Getter 9 | @Setter 10 | @NoArgsConstructor 11 | @ConfigurationProperties(prefix = "test5") 12 | public class Test5Properties { 13 | private String test; 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/github/kingbbode/custom/yaml/importer/processor/Test6Properties.java: -------------------------------------------------------------------------------- 1 | package com.github.kingbbode.custom.yaml.importer.processor; 2 | 3 | import lombok.Getter; 4 | import lombok.NoArgsConstructor; 5 | import lombok.Setter; 6 | import org.springframework.boot.context.properties.ConfigurationProperties; 7 | 8 | @Getter 9 | @Setter 10 | @NoArgsConstructor 11 | @ConfigurationProperties(prefix = "test6") 12 | public class Test6Properties { 13 | private String test; 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/github/kingbbode/custom/yaml/importer/processor/Test7Properties.java: -------------------------------------------------------------------------------- 1 | package com.github.kingbbode.custom.yaml.importer.processor; 2 | 3 | import lombok.Getter; 4 | import lombok.NoArgsConstructor; 5 | import lombok.Setter; 6 | import org.springframework.boot.context.properties.ConfigurationProperties; 7 | 8 | @Getter 9 | @Setter 10 | @NoArgsConstructor 11 | @ConfigurationProperties(prefix = "test7") 12 | public class Test7Properties { 13 | private String test; 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/github/kingbbode/custom/yaml/importer/processor/Test8Properties.java: -------------------------------------------------------------------------------- 1 | package com.github.kingbbode.custom.yaml.importer.processor; 2 | 3 | import lombok.Getter; 4 | import lombok.NoArgsConstructor; 5 | import lombok.Setter; 6 | import org.springframework.boot.context.properties.ConfigurationProperties; 7 | 8 | @Getter 9 | @Setter 10 | @NoArgsConstructor 11 | @ConfigurationProperties(prefix = "test8") 12 | public class Test8Properties { 13 | private String test; 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/github/kingbbode/custom/yaml/importer/processor/Test1Properties.java: -------------------------------------------------------------------------------- 1 | package com.github.kingbbode.custom.yaml.importer.processor; 2 | 3 | 4 | import lombok.Getter; 5 | import lombok.NoArgsConstructor; 6 | import lombok.Setter; 7 | import org.springframework.boot.context.properties.ConfigurationProperties; 8 | 9 | @Getter 10 | @Setter 11 | @NoArgsConstructor 12 | @ConfigurationProperties(prefix = "test1") 13 | public class Test1Properties { 14 | private String test; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /src/test/java/com/github/kingbbode/custom/yaml/importer/processor/SpringBootTestApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.kingbbode.custom.yaml.importer.processor; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | @EnableConfigurationProperties({ 9 | Test1Properties.class, 10 | Test2Properties.class, 11 | Test3Properties.class, 12 | Test4Properties.class, 13 | Test5Properties.class, 14 | Test6Properties.class, 15 | Test7Properties.class, 16 | Test8Properties.class 17 | }) 18 | @SpringBootApplication 19 | public class SpringBootTestApplication { 20 | @Configuration 21 | public static class TestConfig { 22 | @Bean 23 | public CustomYamlPropertiesEnvironmentPostProcessor thirdPropertiesEnvironmentPostProcessor() { 24 | return new CustomYamlPropertiesEnvironmentPostProcessor(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/com/github/kingbbode/custom/yaml/importer/processor/properties/LocalPropertiesLoadTest.java: -------------------------------------------------------------------------------- 1 | package com.github.kingbbode.custom.yaml.importer.processor.properties; 2 | 3 | import com.github.kingbbode.custom.yaml.importer.processor.*; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.ActiveProfiles; 9 | 10 | import static org.junit.jupiter.api.Assertions.assertEquals; 11 | 12 | @Slf4j 13 | @ActiveProfiles("") 14 | @SpringBootTest 15 | public class LocalPropertiesLoadTest { 16 | 17 | @Autowired 18 | private Test1Properties test1Properties; 19 | 20 | @Autowired 21 | private Test2Properties test2Properties; 22 | 23 | @Autowired 24 | private Test3Properties test3Properties; 25 | 26 | @Autowired 27 | private Test4Properties test4Properties; 28 | 29 | @Autowired 30 | private Test5Properties test5Properties; 31 | 32 | @Autowired 33 | private Test6Properties test6Properties; 34 | 35 | @Autowired 36 | private Test7Properties test7Properties; 37 | 38 | @Autowired 39 | private Test8Properties test8Properties; 40 | 41 | @Test 42 | public void properties() { 43 | assertEquals("a", test1Properties.getTest()); 44 | assertEquals("a", test2Properties.getTest()); 45 | assertEquals("a", test3Properties.getTest()); 46 | assertEquals("a", test4Properties.getTest()); 47 | assertEquals("a", test5Properties.getTest()); 48 | assertEquals("a", test6Properties.getTest()); 49 | assertEquals("a", test7Properties.getTest()); 50 | assertEquals("a", test8Properties.getTest()); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/com/github/kingbbode/custom/yaml/importer/processor/properties/StubPropertiesLoadTest.java: -------------------------------------------------------------------------------- 1 | package com.github.kingbbode.custom.yaml.importer.processor.properties; 2 | 3 | import com.github.kingbbode.custom.yaml.importer.processor.*; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.ActiveProfiles; 9 | 10 | import static org.junit.jupiter.api.Assertions.assertEquals; 11 | 12 | @Slf4j 13 | @ActiveProfiles("stub") 14 | @SpringBootTest 15 | public class StubPropertiesLoadTest { 16 | 17 | @Autowired 18 | private Test1Properties test1Properties; 19 | 20 | @Autowired 21 | private Test2Properties test2Properties; 22 | 23 | @Autowired 24 | private Test3Properties test3Properties; 25 | 26 | @Autowired 27 | private Test4Properties test4Properties; 28 | 29 | @Autowired 30 | private Test5Properties test5Properties; 31 | 32 | @Autowired 33 | private Test6Properties test6Properties; 34 | 35 | @Autowired 36 | private Test7Properties test7Properties; 37 | 38 | @Autowired 39 | private Test8Properties test8Properties; 40 | 41 | @Test 42 | public void stub_properties() { 43 | assertEquals("c", test1Properties.getTest()); 44 | assertEquals("a", test2Properties.getTest()); 45 | assertEquals("b", test3Properties.getTest()); 46 | assertEquals("b", test4Properties.getTest()); 47 | assertEquals("c", test5Properties.getTest()); 48 | assertEquals("a", test6Properties.getTest()); 49 | assertEquals("b", test7Properties.getTest()); 50 | assertEquals("b", test8Properties.getTest()); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/com/github/kingbbode/custom/yaml/importer/processor/properties/DevPropertiesLoadTest.java: -------------------------------------------------------------------------------- 1 | package com.github.kingbbode.custom.yaml.importer.processor.properties; 2 | 3 | 4 | import com.github.kingbbode.custom.yaml.importer.processor.*; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.junit.jupiter.api.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.test.context.ActiveProfiles; 10 | 11 | import static org.junit.jupiter.api.Assertions.assertEquals; 12 | 13 | @Slf4j 14 | @ActiveProfiles("beta") 15 | @SpringBootTest 16 | public class DevPropertiesLoadTest { 17 | 18 | @Autowired 19 | private Test1Properties test1Properties; 20 | 21 | @Autowired 22 | private Test2Properties test2Properties; 23 | 24 | @Autowired 25 | private Test3Properties test3Properties; 26 | 27 | @Autowired 28 | private Test4Properties test4Properties; 29 | 30 | @Autowired 31 | private Test5Properties test5Properties; 32 | 33 | @Autowired 34 | private Test6Properties test6Properties; 35 | 36 | @Autowired 37 | private Test7Properties test7Properties; 38 | 39 | @Autowired 40 | private Test8Properties test8Properties; 41 | 42 | @Test 43 | public void dev_properties() { 44 | assertEquals("b", test1Properties.getTest()); 45 | assertEquals("b", test2Properties.getTest()); 46 | assertEquals("b", test3Properties.getTest()); 47 | assertEquals("b", test4Properties.getTest()); 48 | assertEquals("b", test5Properties.getTest()); 49 | assertEquals("b", test6Properties.getTest()); 50 | assertEquals("b", test7Properties.getTest()); 51 | assertEquals("b", test8Properties.getTest()); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/com/github/kingbbode/custom/yaml/importer/processor/properties/RealStubPropertiesLoadTest.java: -------------------------------------------------------------------------------- 1 | package com.github.kingbbode.custom.yaml.importer.processor.properties; 2 | 3 | import com.github.kingbbode.custom.yaml.importer.processor.*; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.ActiveProfiles; 9 | 10 | import static org.junit.jupiter.api.Assertions.assertEquals; 11 | 12 | @Slf4j 13 | @ActiveProfiles("real") 14 | @SpringBootTest 15 | public class RealStubPropertiesLoadTest { 16 | 17 | @Autowired 18 | private Test1Properties test1Properties; 19 | 20 | @Autowired 21 | private Test2Properties test2Properties; 22 | 23 | @Autowired 24 | private Test3Properties test3Properties; 25 | 26 | @Autowired 27 | private Test4Properties test4Properties; 28 | 29 | @Autowired 30 | private Test5Properties test5Properties; 31 | 32 | @Autowired 33 | private Test6Properties test6Properties; 34 | 35 | @Autowired 36 | private Test7Properties test7Properties; 37 | 38 | @Autowired 39 | private Test8Properties test8Properties; 40 | 41 | @Test 42 | public void real_properties() { 43 | assertEquals("d", test1Properties.getTest()); 44 | assertEquals("d", test2Properties.getTest()); 45 | assertEquals("d", test3Properties.getTest()); 46 | assertEquals("d", test4Properties.getTest()); 47 | assertEquals("d", test5Properties.getTest()); 48 | assertEquals("d", test6Properties.getTest()); 49 | assertEquals("d", test7Properties.getTest()); 50 | assertEquals("d", test8Properties.getTest()); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/com/github/kingbbode/custom/yaml/importer/processor/properties/DevStubPropertiesLoadTest.java: -------------------------------------------------------------------------------- 1 | package com.github.kingbbode.custom.yaml.importer.processor.properties; 2 | 3 | 4 | import com.github.kingbbode.custom.yaml.importer.processor.*; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.junit.jupiter.api.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.test.context.ActiveProfiles; 10 | 11 | import static org.junit.jupiter.api.Assertions.assertEquals; 12 | 13 | @Slf4j 14 | @ActiveProfiles({"beta", "stub"}) 15 | @SpringBootTest 16 | public class DevStubPropertiesLoadTest { 17 | 18 | @Autowired 19 | private Test1Properties test1Properties; 20 | 21 | @Autowired 22 | private Test2Properties test2Properties; 23 | 24 | @Autowired 25 | private Test3Properties test3Properties; 26 | 27 | @Autowired 28 | private Test4Properties test4Properties; 29 | 30 | @Autowired 31 | private Test5Properties test5Properties; 32 | 33 | @Autowired 34 | private Test6Properties test6Properties; 35 | 36 | @Autowired 37 | private Test7Properties test7Properties; 38 | 39 | @Autowired 40 | private Test8Properties test8Properties; 41 | 42 | @Test 43 | public void dev_stub_properties() { 44 | assertEquals("c", test1Properties.getTest()); 45 | assertEquals("b", test2Properties.getTest()); 46 | assertEquals("b", test3Properties.getTest()); 47 | assertEquals("b", test4Properties.getTest()); 48 | assertEquals("c", test5Properties.getTest()); 49 | assertEquals("b", test6Properties.getTest()); 50 | assertEquals("b", test7Properties.getTest()); 51 | assertEquals("b", test8Properties.getTest()); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS="-Xmx64m" 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spring-boot-custom-yaml-importer 2 | 3 | [![Build Status](https://travis-ci.org/kingbbode/spring-boot-custom-yaml-importer.svg?branch=master)](https://travis-ci.org/kingbbode/spring-boot-custom-yaml-importer) 4 | [![](https://jitpack.io/v/kingbbode/spring-boot-custom-yaml-importer.svg)](https://jitpack.io/#kingbbode/spring-boot-custom-yaml-importer) 5 | [![Coverage Status](https://coveralls.io/repos/github/kingbbode/spring-boot-custom-yaml-importer/badge.svg?branch=master)](https://coveralls.io/github/kingbbode/spring-boot-custom-yaml-importer?branch=master) 6 | 7 | spring boot support for automatically loading custom yaml file. 8 | 9 | ### Spring Boot Compatibility 10 | 11 | | Version | Spring Boot Version | 12 | |:---------:|:-------------------:| 13 | | 0.3.0 | 2.4.x ~ | 14 | | 0.2.0 | 2.2.x, 2.3.x | 15 | | 0.1.0 | 2.0.x, 2.1.x | 16 | 17 | --- 18 | 19 | Spring Boot offers three ways to load custom settings. 20 | 21 | - spring.config.location, spring.config.name 22 | - SpringApplicationBuilder 23 | - EnvironmentPostProcessor 24 | 25 | [[Spring Docs : howto-properties-and-configuration](https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html)] 26 | 27 | There is also this method using Profile 28 | 29 | *[main module]* 30 | ``` 31 | spring: 32 | profiles: 33 | include: 34 | - a 35 | - b 36 | - c 37 | ``` 38 | *[sub module a]* 39 | `application-a.yml` 40 | 41 | *[sub module b]* 42 | `application-b.yml` 43 | 44 | *[sub module c]* 45 | `application-c.yml` 46 | 47 | [[Spring Docs : boot-features-external-config](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html)] 48 | 49 | **but** this is cumbersome. when a lot of multi-modules and settings are created, you have to work on them every time. 50 | 51 | so i made `spring-boot-custom-yaml-importer` 52 | 53 | **so that you can load the settings just by having dependencies.** 54 | 55 | ## Install 56 | 57 | ``` 58 | repositories { 59 | maven { url 'https://jitpack.io' } 60 | } 61 | 62 | dependencies { 63 | compile 'com.github.kingbbode:spring-boot-custom-yaml-importer:0.3.0' 64 | } 65 | ``` 66 | 67 | ## Usage 68 | 69 | load the yaml file that is matched with `resources/config/custom-*.yml`. 70 | 71 | write the settings you need for each of your modules here. 72 | 73 | ### Single Module 74 | 75 | ``` 76 | |_src 77 | | |_main 78 | | |_resources 79 | | |_config 80 | | | |_ custom-a.yml 81 | | | |_ custom-b.yml 82 | | | |_ custom-c.yml 83 | | | 84 | | |_application.yml 85 | ``` 86 | 87 | ### Multi Module 88 | 89 | ``` 90 | |_app 91 | | |_src 92 | | |_main 93 | | |_resources 94 | | |_config 95 | | | |_custom-a.yml 96 | | | 97 | | |_application.yml 98 | | 99 | |_module-b 100 | | |_src 101 | | |_main 102 | | |_resources 103 | | |_config 104 | | |_custom-b.yml 105 | |_module-c 106 | | |_src 107 | | |_main 108 | | |_resources 109 | | |_config 110 | | |_custom-c.yml 111 | ``` 112 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS='"-Xmx64m"' 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /src/main/java/com/github/kingbbode/custom/yaml/importer/processor/CustomYamlPropertiesEnvironmentPostProcessor.java: -------------------------------------------------------------------------------- 1 | package com.github.kingbbode.custom.yaml.importer.processor; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.context.properties.bind.Bindable; 6 | import org.springframework.boot.context.properties.bind.Binder; 7 | import org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver; 8 | import org.springframework.boot.context.properties.source.ConfigurationPropertySources; 9 | import org.springframework.boot.env.EnvironmentPostProcessor; 10 | import org.springframework.boot.env.YamlPropertySourceLoader; 11 | import org.springframework.boot.origin.OriginTrackedValue; 12 | import org.springframework.core.Ordered; 13 | import org.springframework.core.env.ConfigurableEnvironment; 14 | import org.springframework.core.env.Profiles; 15 | import org.springframework.core.env.PropertySource; 16 | import org.springframework.core.io.DefaultResourceLoader; 17 | import org.springframework.core.io.Resource; 18 | import org.springframework.core.io.ResourceLoader; 19 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 20 | import org.springframework.core.io.support.ResourcePatternResolver; 21 | 22 | import java.io.IOException; 23 | import java.util.*; 24 | import java.util.stream.Collectors; 25 | import java.util.stream.IntStream; 26 | import java.util.stream.Stream; 27 | 28 | @Slf4j 29 | public class CustomYamlPropertiesEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { 30 | 31 | private static final int DEFAULT_ORDER = Ordered.LOWEST_PRECEDENCE; 32 | private static final String OLD_SPRING_PROFILES = "spring.profiles"; 33 | private static final String SPRING_PROFILES = "spring.config.activate.on-profile"; 34 | 35 | private final YamlPropertySourceLoader loader = new YamlPropertySourceLoader(); 36 | 37 | @Override 38 | public void postProcessEnvironment(ConfigurableEnvironment environment, 39 | SpringApplication application) { 40 | log.info("** START CustomYamlPropertiesEnvironmentPostProcessor **"); 41 | ResourceLoader resourceLoader = Optional 42 | .ofNullable(application.getResourceLoader()) 43 | .orElseGet(DefaultResourceLoader::new); 44 | 45 | ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(resourceLoader); 46 | 47 | Resource[] resources = new Resource[]{}; 48 | try{ 49 | resources = resourcePatternResolver.getResources("classpath*:config/custom-*.yml"); 50 | }catch(IOException e){ 51 | log.error("{}", e.getMessage(), e); 52 | } 53 | for(Resource resource : resources) { 54 | loadYaml(resource, environment) 55 | .forEach(propertySource -> { 56 | log.info("[Yaml Third Properties Load] file: {}", resource.getFilename()); 57 | environment.getPropertySources().addLast(propertySource); 58 | }); 59 | } 60 | } 61 | 62 | private List> loadYaml(Resource path, ConfigurableEnvironment environment) { 63 | if (!path.exists()) { 64 | throw new IllegalArgumentException("Resource " + path + " does not exist"); 65 | } 66 | 67 | try { 68 | List> defaultPropertySource = new ArrayList<>(); 69 | 70 | List> propertySourceList = this.loader.load(path.getFilename(), path).stream().filter( 71 | propertySource -> { 72 | Binder binder = new Binder( 73 | ConfigurationPropertySources.from(propertySource), 74 | new PropertySourcesPlaceholdersResolver(environment)); 75 | String[] profiles = Stream.concat( 76 | Arrays.stream(loadProfiles(binder, OLD_SPRING_PROFILES)), 77 | Arrays.stream(loadProfiles(binder, SPRING_PROFILES)) 78 | ) 79 | .toArray(String[]::new); 80 | if (profiles.length == 0) { 81 | defaultPropertySource.add(propertySource); 82 | return false; 83 | } 84 | return environment.acceptsProfiles(Profiles.of(profiles)); 85 | }).collect(Collectors.toList()); 86 | 87 | propertySourceList = sortProfilePriority(environment, propertySourceList); 88 | propertySourceList.addAll(defaultPropertySource); 89 | 90 | return propertySourceList; 91 | } 92 | catch (IOException ex) { 93 | throw new IllegalStateException( 94 | "Failed to load yaml configuration from " + path, ex); 95 | } 96 | } 97 | 98 | private String[] loadProfiles(Binder binder, String profileConfig) { 99 | String[] profiles = binder.bind(profileConfig, Bindable.of(String[].class)) 100 | .orElse(new String[0]); 101 | if(OLD_SPRING_PROFILES.equals(profileConfig) && profiles.length > 0) { 102 | log.warn("'{}' is deprecated since spring boot 2.4 and later.", OLD_SPRING_PROFILES); 103 | } 104 | return profiles; 105 | } 106 | 107 | private List> sortProfilePriority(ConfigurableEnvironment environment, List> propertySourceList) { 108 | String[] activeProfiles = environment.getActiveProfiles(); 109 | 110 | return IntStream.range(0, activeProfiles.length) 111 | .map(i -> activeProfiles.length - i - 1) 112 | .mapToObj(i -> activeProfiles[i]) 113 | .filter(Objects::nonNull) 114 | .map(profile -> propertySourceList.stream().filter(propertySource -> matchProfiles(profile, propertySource)).findAny().orElse(null)) 115 | .filter(Objects::nonNull) 116 | .collect(Collectors.toList()); 117 | } 118 | 119 | private boolean matchProfiles(String profile, PropertySource propertySource) { 120 | return matchProfiles(profile, propertySource, OLD_SPRING_PROFILES) || matchProfiles(profile, propertySource, SPRING_PROFILES); 121 | } 122 | 123 | @SuppressWarnings("unchecked") 124 | private boolean matchProfiles(String profile, PropertySource propertySource, String springProfileConfig) { 125 | Object property = propertySource.getProperty(springProfileConfig); 126 | if(property == null) { 127 | return ((Map) propertySource.getSource()) 128 | .entrySet() 129 | .stream() 130 | .filter(entry -> entry.getKey().contains(springProfileConfig)) 131 | .map(trackedValueEntry -> trackedValueEntry.getValue().toString()) 132 | .anyMatch(profile::equals); 133 | } 134 | 135 | return Arrays.stream(((String) property).split(",")) 136 | .map(String::trim) 137 | .anyMatch(s -> s.equals(profile)); 138 | } 139 | 140 | @Override 141 | public int getOrder() { 142 | return DEFAULT_ORDER; 143 | } 144 | } 145 | --------------------------------------------------------------------------------