-
message.properties → *.json 변환하여 프론트 프로젝트에 넣기. (gradle)IT 2021. 2. 9. 10:56
안녕하세요
server side 쪽에서 사용중인 message.properties를
front end 쪽에서도 같이 사용하고 싶을 경우가 생깁니다.
이때 generate할수 있는 gradle plugin을 사용하면됩니다.
message.properties → *.json 보통 server side 쪽 프로젝트의 message.properties가 있을것입니다.
message_*.properties properties details 이제 이부분을 json으로 변환하여 front end project에 넣는
gradle plugin을 작성하겠습니다.
gradle custom plugin: docs.gradle.org/current/userguide/custom_plugins.html
Developing Custom Gradle Plugins
In this example, GreetingPluginExtension is an object with a property called message. The extension object is added to the project with the name greeting. This object then becomes available as a project property with the same name as the extension object.
docs.gradle.org
1. root project 경로에 buildSrc 디렉토리를 만들어 줍니다.
2. buildSrc/build.gradle 쪽에 사용할 라이브러리등을 작성해줍니다.
3. plugin class를 extends DefaultTask {..} 작성해줍니다.
build.gradle
apply plugin: 'java' apply plugin: 'idea' repositories { jcenter() mavenCentral() maven { url "https://repo1.maven.org/maven2" } maven { url 'https://plugins.gradle.org/m2/' } } dependencies { compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5' compile group: 'com.google.guava', name: 'guava', version: '28.0-jre' compile group: 'commons-io', name: 'commons-io', version: '2.6' }
PropertiesToJson.java
public class PropertiesToJson extends DefaultTask { @Input String sourceDirPath = getProject().getProjectDir().toString(); @Input String destDirPath = getProject().getProjectDir().toString(); @TaskAction void run() throws IOException { File msgDir = new File(sourceDirPath); File[] files = msgDir.listFiles(); for (int i = 0; i < files.length; i++) { File it = files[i]; Properties props = new Properties(); props.load(new FileInputStream(it)); File destDirPathFile = new File(destDirPath); destDirPathFile.mkdirs(); Gson gsonObj = new Gson(); String jsonStr = gsonObj.toJson(props); FileOutputStream outputStream = new FileOutputStream(new File(destDirPathFile, FilenameUtils.removeExtension(it.getName()) + ".json")); byte[] strToBytes = jsonStr.getBytes(StandardCharsets.UTF_8); outputStream.write(strToBytes); outputStream.close(); } } }
plugin 사용
def webcoreAngularDir = project(':cms-angular').projectDir task messagePropertiesToJsonWebCoreAngular(type: com.....gradle.plugin.PropertiesToJson) { sourceDirPath = "${projectDir}/src/main/resources/messages" destDirPath = "${webcoreAngularDir}/generate/i18n" }
BUILD SUCCESSFUL in 880ms
1 actionable task: 1 executedjson generate json json 사용
import enUS from '@generate/i18n/message_en_US.json'; import koKR from '@generate/i18n/message_ko_KR.json'; export const languages: any = [ { key: 'us', param: 'en_US', alt: 'United States', title: 'English (US)', defaultData: enUS }, { key: 'kr', param: 'ko_KR', alt: 'Korea', title: '한국어', defaultData: koKR } ];
감사합니다.
'IT' 카테고리의 다른 글
spring data jpql 에서 GROUP_CONCAT function 사용하기. (0) 2021.04.08 java frameworks "simple-boot" open source project (자바 프레임워크) (0) 2021.03.07 java 자바 이미지 텍스트 변환하기 (0) 2021.02.05 하이버네이트 5.x 시작하기 (pdf) (0) 2021.02.04 비트코인 자동 매도,매수 프로그램 (coinone) (0) 2021.02.04