'java code formatter'에 해당되는 글 1건

  1. 2016.02.22 [Eclipse] Commandline으로 Java Code Formatter 실행하기

Eclipse 에서 Java Code Convention 을 설정 후 해당 룰에 해당하는 xml 파일로 대 Java 파일들을 포멧팅하는 방법에 대해 알아보자.


대량의 파일을 포멧팅할때 Eclipse Command-line 옵션과 변환된 룰파일을 사용하여 포멧팅 가능하다.


Eclipse code format from command line

Eclipse를 이용하여 command line 상태에서 java code formtting을 수행하는 방법을 알아보자.


다음과 같이 Eclipse Runtime Options을 이용하여 Java Code Formatter를 command line에서 수행 가능하다.

  • eclipse -application <id> (Runtime) // id는 eclipse product id를 의미

Eclipse내에 내장된 Java Code Formatter의 product id는 다음과 같다.

  • org.eclipse.jdt.core.JavaCodeFormatter

Eclipse Runtime Options을 이용하여 JavaCodeFormatter를 실행하는 예제 및 옵션은 다음과 같다.

Usage: eclipse -nosplash -application org.eclipse.jdt.core.JavaCodeFormatter [ OPTIONS ] -config <configFile> <files>

   <files> Java source files and/or directories to format.

           Only files ending with .java will be formatted in the given directory.

   -config <configFile> Use the formatting style from the specified properties file.

                        Refer to the help documentation to find out how to generate this file.

OPTIONS:

 -help Display this message.

 -quiet Only print error messages.

 -verbose Be verbose about the formatting job.


JavaCodeFormatter의 Command line에서 쓰이는 config 파일형식은 Eclipse Coding Convention 설정에서 정의된 xml 파일 형식이 아니다.
따라서 xml 파일을 config 파일로 바꾸는 일이 필요하다.

xml 파일을 config 파일로 변환하기 위해 다음과 같이 수행한다.

  • Eclipse에서 아무 Java 프로젝트를 생성
  • Project 선택 후 우클릭 > Properties 메뉴 선택
  • Java Code Style > Formatter > Enable project specific settings 선택 > Active profile에서 TIDE_JAVA_CONVENTION.xml 을 import 하거나 메뉴이 이미 있다면 선택
  • Apply > OK
  • 생성한 Java 프로젝트 경로로 이동
  • 프로젝트 경로 하위에 .settings 폴더밑의 org.eclipse.jdt.core.prefs 파일이 config 파일이므로 이를 원하는 곳에 복사해놓는다.

ConfigFile을 생성했으면 다음과 같이 실행 가능하다.

$ {eclipse path}/eclipse -nosplash -application org.eclipse.jdt.core.JavaCodeFormatter -verbose -config {configFile path} {source file path}


// example

$ /home/leechwin/eclipse/eclipse -nosplash -application org.eclipse.jdt.core.JavaCodeFormatter -verbose -config /home/leechwin/eclipse/org.eclipse.jdt.core.prefs /home/leechwin/git/org.leechwin.com/src/


References


Posted by leechwin
,