|
| 1 | +--- |
| 2 | +title: "Spring formatter 와 Check Style 사용하기" |
| 3 | +date: "2024-05-26" |
| 4 | +tags: ["Clean Code", "Spring", "Java"] |
| 5 | +summary: "Using Spring formatter and Check Style" |
| 6 | +description: "IntelliJ 에서 Spring formatter 와 Check Style 로 일관성 있는 코드 작성하기" |
| 7 | +--- |
| 8 | + |
| 9 | +:::info |
| 10 | +IntelliJ 에서 Spring formatter 와 Check Style 을 이용해서 일관성 있는 코드 작성할 수 있다. |
| 11 | +팀이나 회사별로 Code Convention 이 있겠지만, 없는 경우 Spring formatter 와 Check Style 을 적용하면 일관성 있는 코드 작성하기 편해진다. |
| 12 | +::: |
| 13 | + |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## Spring Formatter plugin 설치하기 |
| 18 | + |
| 19 | +Maven Central 에서 Spring formatter 를 다운 받아서 설치한다. |
| 20 | +([다운로드](https://repo1.maven.org/maven2/io/spring/javaformat/spring-javaformat-intellij-idea-plugin/0.0.38/)) <br/> |
| 21 | +`IntelliJ > settings > Plugins` 에서 다운 받은 `Spring formatter` 를 추가한다. |
| 22 | + |
| 23 | +<div style={{ textAlign: 'center' }}> |
| 24 | + <img src="/img/post/java/spring-formatter/settings.png" alt="settings" style={{ display: 'inline-block' }} /> |
| 25 | +</div> |
| 26 | + |
| 27 | + |
| 28 | +### Plug-in 활성화 조건 3가지 |
| 29 | + |
| 30 | +Plugin 은 아래 3가지 조건 중에 하나라도 만족 하는 경우 활성화된다. |
| 31 | + |
| 32 | +:::note |
| 33 | +<a href="https://github.com/spring-io/spring-javaformat?tab=readme-ov-file#intellij-idea"> IntelliJ-IDEA 참고 </a> |
| 34 | +::: |
| 35 | + |
| 36 | + |
| 37 | +#### 1) .springjavaformatconfig 파일이 존재하는 경우 |
| 38 | + |
| 39 | +#### 2) Maven 은 spring-java-format-maven-plugin 이 pom.xml 에 정의된 경우 |
| 40 | + |
| 41 | +```shell |
| 42 | +<build> |
| 43 | + <plugins> |
| 44 | + <plugin> |
| 45 | + <groupId>io.spring.javaformat</groupId> |
| 46 | + <artifactId>spring-javaformat-maven-plugin></artifactId> |
| 47 | + <version>0.0.41</version> |
| 48 | + </<plugin> |
| 49 | + </plugins> |
| 50 | +</build> |
| 51 | +``` |
| 52 | + |
| 53 | +#### 3) Gradle 은 io.spring.javaformat 플러그인이 적용 된 경우 |
| 54 | +```shell |
| 55 | +buildscript { |
| 56 | + repositories { |
| 57 | + mavenCentral() |
| 58 | + } |
| 59 | + dependencies { |
| 60 | + classpath("io.spring.javaformatter:spring-javaformat-gradle-plugin:0.0.41") |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +apply plugin: 'io.spring.javaformatter' |
| 65 | +``` |
| 66 | + |
| 67 | +활성화가 되면 IntelliJ IDEA 우측 하단에 활성화 표시가 생성된다. |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## CheckStyle Plugin 설치하기 |
| 74 | + |
| 75 | +#### Step1) IntelliJ > settings > Plugins > Marketplace 에서 `CheckStyle-IDEA` 을 다운 받아서 설치한다. |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +#### Step2) Maven Central 에서 `spring-javaformat-checkstyle.jar` , `spring-javaformat-config.jar` 를 다운 받는다. |
| 81 | + |
| 82 | +* <a href="https://repo1.maven.org/maven2/io/spring/javaformat/spring-javaformat-checkstyle/0.0.41"> spring-javaformat-checkstyle.jar </a> |
| 83 | +* <a href="https://repo1.maven.org/maven2/io/spring/javaformat/spring-javaformat-config/0.0.41"> spring-javaformat-config.jar </a> |
| 84 | + |
| 85 | + |
| 86 | +#### Step3) Tools > CheckStyle 에서 Thrid-Party Checks 에 `spring-javaformat-checkstyle.jar`, `spring-javaformat-config.jar` 를 추가한다. |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +#### Step4) Tools > CheckStyle 에서 `checkstyle.xml` 를 추가한다. |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +#### Step5) check style run! |
| 97 | + |
| 98 | +실행을 하게 되면 현재 파일에서 `checkStyle` 이 맞지 않는 부분을 체크 해준다. |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +💡 **Tips** |
| 105 | + |
| 106 | +1) 현재 파일에 대한 Check run 은 `Keymap` (settings - keymap) 으로 설정 가능하다. |
| 107 | + |
| 108 | + |
| 109 | +2) 현재 파일에 warning 이나 error 표시를 끌 수 있고, checkStyle scan 파일 범위를 저정할 수 있다. |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +### 📚 Reference |
| 116 | + |
| 117 | +* [Spring-javaformat Git](https://github.com/spring-io/spring-javaformat) |
| 118 | + |
| 119 | + |
0 commit comments