message.properties → *.json 변환하여 프론트 프로젝트에 넣기. (gradle)

message.properties → *.json 변환하여 프론트 프로젝트에 넣기. (gradle)

728x90

안녕하세요

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

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 executed

json 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 } ];

감사합니다.

728x90

from http://life-journey.tistory.com/42 by ccl(A) rewrite - 2021-02-09 11:01:01