holy's story
Spotless 본문
SMALL
코드 포맷팅을 적용하기 위해 spotless 를 적용할 지 말지 결정하기 위해 찾아보았다.
spotless는 junit5에서 코드 포맷팅을 하기 위해 사용한다.
다음 내용은 junit5에서 spotless 적용에 관해 쓰여진 부분이다.
-
더보기Code formatting is enforced using the Spotless Gradle plugin. You can use gradle spotlessApply to format new code and add missing license headers to source files. Formatter and import order settings for Eclipse are available in the repository under junit-eclipse-formatter-settings.xml and junit-eclipse.importorder, respectively. For IntelliJ IDEA there's a plugin you can use in conjunction with the Eclipse settings.
-
더보기코드 형식은 Spotless Gradle 플러그인을 사용하여 적용됩니다. gradle spotlessApply 를 사용하여 새 코드의 형식을 지정하고 누락 된 라이센스 헤더를 소스 파일에 추가 할 수 있습니다. Eclipse에 대한 포맷터 및 가져오기 순서 설정은 각각 junit-eclipse-formatter-settings 및 junit-eclipse.importorder의 저장소에서 사용할 수 있습니다.xml IntelliJ IDEA의 경우 Eclipse 설정과 함께 사용할 수 있는 플러그인이 있습니다.
이제 spotless 적용 방식을 설명하겠다.
build.gradle 파일에 다음과 같은 플러그인을 추가해준다.
plugins{
``
``
id ‘com.diffplug.spotless’ version ‘6.11.0’
}
Setting
그다음에는 spotless에 대한 세팅을 해줘야 한다.
spotless를 이용하면 import 순서도 적용할 수 있다.
바로 importOrder()로 import를 하고싶은 순서대로 작성해주면 된다.
spotless{
kotlin{
importOrder("java", "javax", ``)
}
}
🪄🪄
이제 스타일 규칙을 정해보자.
아까 import 순서를 정해준 위치에 적용하고싶은 내용을 넣어주기만 하면 된다.
다음은 자주 사용될 것 같은 적용사항을 정리해 보았다.
removeUnusedImports() | 사용되지 않는 import문 제거 |
indentWithTabs() | 탭으로 들여쓰기(속성값) |
indentWithspaces() | 스페이스로 들여쓰기(속성값) |
trimTrailingWhitespace() | 무의미한 공백 제거 |
endWithNewline() | 끝부분 newline(파일이 하나의 줄바꿈으로 끝나게) |
googleJavaFormat() | 구글 포맷 적용 |
prettier() | 프리티어의 기본버전 사용 |
정리하면 다음과 같다.
spotless{
kotlin{
importOrder("java", "javax", ``)
}
googleJavaFormat()
trimTrailingWhitespace()
indentWithTabs()
indentWithspaces()
removeUnusedImports()
endWithNewline()
}
또 특정 파일에만 따로 적용하고 싶다면 다음과 같이 타겟을 정해줄 수 있다.
format 'misc', {
// 만약 특정 파일만 제외하고 싶다면 targetExclude를 적용해주자.
target '**/*.yml', '**/*.gradle', '**/*.md', '**/.gitignore'
indentWithSpaces(5)
indentWithTabs(2)
}
}
협업에서 다른사람의 코드를 읽고 이해할 때, 코딩 포맷이 통일 되어있다면 리소스 낭비를 방지할 수 있다고 생각한다.
이번 개발에는 ktlint만 적용하는 것으로 결정되었지만, 다음 개발에서는 spotless를 적용해보는 것도 좋을 것 같다.