Backend/SpringBoot

[VSCode] 코틀린 + 자바 섞어서 쓰기

Dean83 2024. 10. 21. 15:52

https://dean83.tistory.com/272

 

[VSCode] 코틀린 기반 SpringBoot 설정 (윈도우)

자바 기반으로 되어 있는것은 간편하게 설정이 가능하나, 코틀린 기반으로 된 스프링부트 설정하는데에 애를 먹었다.  굳이 VSCode가 아닌 다른 IDE를 사용하면 되는데 VSCode를 쓴 이유는, 범용적

dean83.tistory.com

여기서 코틀린 기반 VSCode 초기 프로젝트 생성을 다뤘었다. 코틀린 기반으로 시작을 하니 안되는게 너무 많았다. 

대표적으로 spring boot dashboard 가 안되었었다. 

 

그래서, 기본 프로젝트 설정은 Java로 설정하여 메인Class 는 자바로 동작하되, (즉 껍데기는 자바) 나머지 항목들은 코틀린으로 구현하면 될거 같아서 그렇게 구성을 하였고, 결과적으로 잘 작동하는듯 하다. 

 

  • Build.gradle 설정
    • 먼저, 자바 베이스로 프로젝트를 생성했기 때문에 코틀린 관련 항목들을 추가해 주어야 한다. 
    • (기본 예시 build.gradle 을 통째로 올린다)
      • 버전, 프로젝트 이름 등은 적절히 바꾸면 된다.
buildscript {
    ext {
        kotlinVersion = '1.9.25'
        springBootVersion = '3.3.4'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-noarg:${kotlinVersion}")
    }
}

plugins {
	id 'java'
	id 'org.springframework.boot' version '3.3.4'
	id 'io.spring.dependency-management' version '1.1.6'
	id 'org.jetbrains.kotlin.jvm' version '1.9.25'
	id 'org.jetbrains.kotlin.plugin.spring' version '1.9.25'
}

group = 'com.example.test1'
version = '0.0.1-SNAPSHOT'

java {
	toolchain {
		languageVersion = JavaLanguageVersion.of(17)
	}
}

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
}

dependencies {
	implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
	implementation 'org.jetbrains.kotlin:kotlin-reflect'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	compileOnly 'org.projectlombok:lombok'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
		testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5'
	testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

kotlin {
	compilerOptions {
		freeCompilerArgs.addAll '-Xjsr305=strict'
	}
}

tasks.named('test') {
	useJUnitPlatform()
}

 

이렇게 설정 후,

VSCode 우측 코끼리 아이콘 (Gradle) -> Tasks -> Build -> build 에서 빌드 하고,

Tasks -> application -> bootRun 에서 실행

 

하면 메인 url과 (Java) controller 클래스 (kotlin) 에서 매핑한 서브url  모두 잘 동작한다.