Hello World

Spring-loaded를 IntelliJ 와 Gradle를 사용하여 설정 하기 본문

Spring/Boot(4.x)

Spring-loaded를 IntelliJ 와 Gradle를 사용하여 설정 하기

EnterKey 2016. 1. 10. 12:50
반응형

참고 URL : http://docs.spring.io/spring-boot/docs/current/reference/html/howto-hotswapping.html

72.6.2. Spring Loaded를 Gradle와 IntelliJ에서 설정하기

몇가지 단계를 지나면 Spring Loaded를 Gradle와 IntelliJ 에 결합하여 사용 하고 싶을 것이다. 기본적으로 Spring Loaded가 바라보는 classes의 컴파일되는 위치가 IntelliJ에서 Gradle일 경우 달라서 실패 할 것이다.

IntellJ에서 idea를 사용하여 Gradle plugin을 정확하게 설정 할 수 있다.

buildscript {
    repositories { jcenter() }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:1.2.7.RELEASE"
        classpath 'org.springframework:springloaded:1.2.0.RELEASE'
    }
}

apply plugin: 'idea'

idea {
    module {
        inheritOutputDirs = false
        outputDir = file("$buildDir/classes/main/")
    }
}

// ...

IntelliJ 는 Gradle task의 명령에서 동일한 Java 버젼을 사용하도록 설정 해야 하고,springloaded는 buildscript 의 의존성을 포함 하고 있어야 한다.

또한 Make Project Automatically 가 사용하도록 하여 IntelliJ 안에서 파일이 저장되어 코드가 자동으로 컴파일 되게 해야 한다.

IntelliJ 설정 해보기

  1. 프로젝트 생성 하기
    • Gradle Project 생성
      • 생성시 디렉토리 자동 생성 설정
  2. 간단한 Spring-boot web 만들기

    • build.gradle 설정
      compile('org.springframework.boot:spring-boot-starter-web:1.2.7.RELEASE')
      
    • Application 만들기
      @SpringBootApplication
      public class Application {
      public static void main(String[] args){
         SpringApplication.run(Application.class, args);
      }
      }
      
    • IndexController 만들기
      @RestController
      public class IndexController {
      @RequestMapping("/")
      public String index(){
         return "Hello World";
      }
      }
      
    • 테스트
      http://localhost:8080/
      
  3. Spring Loaded 설정하기

    • build.gradle 설정 추가하기
      dependencies {
      testCompile group: 'junit', name: 'junit', version: '4.11'
      compile('org.springframework.boot:spring-boot-starter-web:1.2.7.RELEASE')
      compile('org.springframework:springloaded:1.2.4.RELEASE') //추가하여 javaagent 옵션 추가시 위치 가져옴 변경
      }
      buildscript {
      repositories { jcenter() }
      dependencies {
         classpath "org.springframework.boot:spring-boot-gradle-plugin:1.2.7.RELEASE"
         classpath 'org.springframework:springloaded:1.2.0.RELEASE'
      }
      }
      apply plugin: 'idea'
      idea {
      module {
         inheritOutputDirs = false
         outputDir = file("$buildDir/classes/main/")
      }
      }
      
    • Make Project Automatically 설정하기
      File > Settings > Build, Execution,Deployment > Commpiler > check MakeProject automatically
      Settings[이미지 중간에 Make Project automatically 체크]
    • Controller Method 추가 하기
      @RequestMapping("/addMethod")
      public String addMethod(){
         return "Add Method";
      }
      
    • 실행 설정에 javagent 추가 하기 참조정보
      javaagent[VM options: 추가]
      -javaagent:<pathTo>/springloaded-{VERSION}.jar -noverify
      
    • 추가한 내역 반영하기(아주 중요합니다!)
      Alt + F9 를 눌러 주세요.
    • 테스트
      http://localhost:8080/addMethod
  4. 추가 테스트 내용

    • method의 추가 삭제 변경에 대하여 재반영 됩니다.
    • 일반 class를 추가하는 것은 재반영 되지만, Controller Class를 추가 하는 것은 annotation 이 인식을 하지 않아서 재반영 되지 않습니다.
      • 이부분은 ApplicationContext에서 refresh() 를 호출할 경우 해결 할 수 있습니다.(Spring-boot 제외) 자세한 내용은 추후 다루겠습니다. 테스트만 해봐서, 사실은 저도 잘 몰라서 봐야 합니다.
    • 기존 class에 annotation 추가 수정 삭제는 동작 하였습니다.

설정 관련 동영상



MD 파일 : 


 HotSwapping.md


출처: http://lahuman.jabsiri.co.kr/137

반응형
Comments