This commit is contained in:
ybw 2022-08-06 15:31:56 +08:00
parent ce5d2a2404
commit 6446067dbf
6 changed files with 209 additions and 150 deletions

View File

@ -94,7 +94,7 @@
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.ybw.yaml.demo.YamlDemoApplication</mainClass>
<mainClass>com.ybw.profile.demo.ProfileDemoApplication</mainClass>
</configuration>
<executions>
<execution>

View File

@ -1,13 +1,13 @@
package com.ybw.yaml.demo;
package com.ybw.profile.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class YamlDemoApplication {
public class ProfileDemoApplication {
public static void main(String[] args) {
SpringApplication.run(YamlDemoApplication.class, args);
SpringApplication.run(ProfileDemoApplication.class, args);
}
}

View File

@ -1,10 +1,10 @@
package com.ybw.yaml.demo;
package com.ybw.profile.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class YamlDemoApplicationTests {
class ProfileDemoApplicationTests {
@Test
void contextLoads() {

View File

@ -1,66 +0,0 @@
package com.ybw.yaml.demo.generate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* @program: yaml-demo
* @description:
* @author: geoffrey
* @create: 2022-07-30 23:23
*/
@Slf4j
public class FileGenerateTest {
/**
* @methodName: generate
* @return: void
* @author: ybwei
* @date: 2022/7/30
**/
@Test
public void generate() throws IOException {
//1数据库配置
DataSourceProperties dataSourceProperties = new DataSourceProperties();
dataSourceProperties.setUrl("jdbc:mysql://localhost:3306/prod?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC");
dataSourceProperties.setUsername("prod");
dataSourceProperties.setPassword("prod123");
//2生成文件
generateApplication(dataSourceProperties);
}
/**
* @methodName: generateApplication
* @param dataSourceProperties
* @return: void
* @author: ybwei
* @date: 2022/7/30
**/
private void generateApplication(DataSourceProperties dataSourceProperties) throws IOException {
//1读取置文件以dev配置文件为模板
String rootPath = System.getProperty("user.dir");
String sourceFileName = "application-dev.yml";
String sourceFilePath = rootPath + "\\src\\main\\resources\\" + sourceFileName;
log.info(rootPath);
log.info(sourceFilePath);
String content= FileUtils.readFileToString(new File(sourceFilePath), StandardCharsets.UTF_8);
content=content.replace("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC",dataSourceProperties.getUrl());
content=content.replace("username: root","username: "+dataSourceProperties.getUsername());
content=content.replace("password: 123456","password: "+dataSourceProperties.getPassword());
log.info("generateApplication content:{}", content);
//4生成文件
String targetFileName = "application-prod.yml";
String targetFilePath = rootPath + "\\src\\main\\resources\\" + targetFileName;
FileUtils.writeStringToFile(new File(targetFilePath),content,StandardCharsets.UTF_8);
}
}

View File

@ -1,78 +0,0 @@
package com.ybw.yaml.demo.generate;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import java.io.*;
import java.util.Map;
/**
* @program: yaml-demo
* @description:
* @author: geoffrey
* @create: 2022-07-30 23:04
*/
@Slf4j
public class YamlGenerateTest {
/**
* @methodName: generate
* @return: void
* @author: ybwei
* @date: 2022/7/30
**/
@Test
public void generate() throws IOException {
//1数据库配置
DataSourceProperties dataSourceProperties = new DataSourceProperties();
dataSourceProperties.setUrl("jdbc:mysql://localhost:3306/prod?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC");
dataSourceProperties.setUsername("prod");
dataSourceProperties.setPassword("prod123");
//2生成文件
generateApplication(dataSourceProperties);
}
/**
* @param dataSourceProperties
* @methodName: generateApplication
* @return: void
* @author: ybwei
* @date: 2022/7/30
**/
private void generateApplication(DataSourceProperties dataSourceProperties) throws IOException {
//1读取置文件以dev配置文件为模板
String rootPath = System.getProperty("user.dir");
String sourceFileName = "application-dev.yml";
String sourceFilePath = rootPath + "\\src\\main\\resources\\" + sourceFileName;
log.info(rootPath);
log.info(sourceFilePath);
Yaml yaml = new Yaml();
File file = new File(sourceFilePath);
Map<String, Object> map = yaml.load(new FileInputStream(file));
log.info("map:{}", JSON.toJSONString(map));
//3替换参数
//3.1 替换数据库
Map<String, Object> spring = (Map<String, Object>) map.get("spring");
Map<String, Object> datasource = (Map<String, Object>) spring.get("datasource");
Map<String, Object> druid = (Map<String, Object>) datasource.get("druid");
druid.put("url", dataSourceProperties.getUrl());
druid.put("username", dataSourceProperties.getUsername());
druid.put("password", dataSourceProperties.getPassword());
//4生成文件
String targetFileName = "application-prod.yml";
String targetFilePath = rootPath + "\\src\\main\\resources\\" + targetFileName;
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml2 = new Yaml(options);
FileWriter fw = new FileWriter(targetFilePath);
yaml2.dump(map, fw);
}
}

View File

@ -0,0 +1,203 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="9a4ae1a0-56eb-473a-9f55-05c16669e336" name="变更" comment="edit">
<change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/mvnw" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/mvnw.cmd" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/ybw/yaml/demo/YamlDemoApplication.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/application-dev.yml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/application-prod.yml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/application.yml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/logback.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/com/ybw/yaml/demo/YamlDemoApplicationTests.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/com/ybw/yaml/demo/generate/FileGenerateTest.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/com/ybw/yaml/demo/generate/YamlGenerateTest.java" beforeDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="MavenImportPreferences">
<option name="generalSettings">
<MavenGeneralSettings>
<option name="localRepository" value="D:\mavenlib" />
<option name="useMavenConfig" value="true" />
<option name="userSettingsFile" value="D:\maven\conf\settings.xml" />
</MavenGeneralSettings>
</option>
<option name="importingSettings">
<MavenImportingSettings>
<option name="jdkForImporter" value="1.8" />
</MavenImportingSettings>
</option>
</component>
<component name="MavenRunner">
<option name="jreName" value="1.8" />
</component>
<component name="ProjectId" id="2CfTZnW7PzJyRIhNr80VK9S12SH" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
<option name="showMembers" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RequestMappingsPanelOrder0": "0",
"RequestMappingsPanelOrder1": "1",
"RequestMappingsPanelWidth0": "75",
"RequestMappingsPanelWidth1": "75",
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"WebServerToolWindowFactoryState": "false",
"last_opened_file_path": "E:/gitcode/mygit/share/spring/spring-profile-demo",
"settings.editor.selected.configurable": "MavenSettings",
"spring.configuration.checksum": "61d825a83a698c97373149c61039acff"
}
}]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="E:\gitcode\mygit\learn\yaml\yaml-demo\yaml-demo\src\main\resources" />
</key>
</component>
<component name="RunManager" selected="Spring Boot.YamlDemoApplication">
<configuration name="FileGenerateTest" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="yaml-demo" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.ybw.yaml.demo.generate.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="com.ybw.yaml.demo.generate" />
<option name="MAIN_CLASS_NAME" value="com.ybw.yaml.demo.generate.FileGenerateTest" />
<option name="TEST_OBJECT" value="class" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="YamlGenerateTest" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="yaml-demo" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.ybw.yaml.demo.generate.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="com.ybw.yaml.demo.generate" />
<option name="MAIN_CLASS_NAME" value="com.ybw.yaml.demo.generate.YamlGenerateTest" />
<option name="TEST_OBJECT" value="class" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="YamlGenerateTest.generate" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="yaml-demo" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.ybw.yaml.demo.generate.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="com.ybw.yaml.demo.generate" />
<option name="MAIN_CLASS_NAME" value="com.ybw.yaml.demo.generate.YamlGenerateTest" />
<option name="METHOD_NAME" value="generate" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="YamlDemoApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot">
<module name="yaml-demo" />
<option name="SPRING_BOOT_MAIN_CLASS" value="com.ybw.yaml.demo.YamlDemoApplication" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="JUnit.FileGenerateTest" />
<item itemvalue="JUnit.YamlGenerateTest" />
<item itemvalue="JUnit.YamlGenerateTest.generate" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="应用程序级" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="默认任务">
<changelist id="9a4ae1a0-56eb-473a-9f55-05c16669e336" name="变更" comment="" />
<created>1659192717887</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1659192717887</updated>
<workItem from="1659192722871" duration="1712000" />
<workItem from="1659194498642" duration="1340000" />
<workItem from="1659259774289" duration="720000" />
<workItem from="1659261744145" duration="1450000" />
</task>
<task id="LOCAL-00001" summary="edit">
<created>1659192843637</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1659192843637</updated>
</task>
<task id="LOCAL-00002" summary="edit">
<created>1659193404278</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1659193404278</updated>
</task>
<task id="LOCAL-00003" summary="edit">
<created>1659193523851</created>
<option name="number" value="00003" />
<option name="presentableId" value="LOCAL-00003" />
<option name="project" value="LOCAL" />
<updated>1659193523851</updated>
</task>
<task id="LOCAL-00004" summary="edit">
<created>1659194365786</created>
<option name="number" value="00004" />
<option name="presentableId" value="LOCAL-00004" />
<option name="project" value="LOCAL" />
<updated>1659194365786</updated>
</task>
<task id="LOCAL-00005" summary="edit">
<created>1659195445426</created>
<option name="number" value="00005" />
<option name="presentableId" value="LOCAL-00005" />
<option name="project" value="LOCAL" />
<updated>1659195445426</updated>
</task>
<option name="localTasksCounter" value="6" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="edit" />
<option name="LAST_COMMIT_MESSAGE" value="edit" />
</component>
</project>