Service

SpringBoot - 내장 톰캣 설정

dalgong 2025. 1. 16. 07:53
반응형

1. 환경 설명

  • Project : Gradle-Kotlin
  • Language : Kotlin
  • Spring Boot : Spring CLI v3.3.4
  • Packaging : Jar
  • Java : 21

2. build.gradle.kts

  • /data/gradle/gradle-8.11.1/app
/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java application project to get you started.
 * For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.11.1/userguide/building_java_projects.html in the Gradle documentation.
 * This project uses @Incubating APIs which are subject to change.
 */

plugins {
    id("org.springframework.boot") version "3.1.0"
    id("io.spring.dependency-management") version "1.1.0"
    id("java")
}


repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    // This dependency is used by the application.
    implementation(libs.guava)
    implementation("org.springframework.boot:spring-boot-starter-web:3.3.7")
    implementation("org.springframework.boot:spring-boot-starter:3.3.7")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation ("org.apache.tomcat.embed:tomcat-embed-core:10.1.30")
}

testing {
    suites {
        // Configure the built-in test suite
        val test by getting(JvmTestSuite::class) {
            // Use JUnit Jupiter test framework
            useJUnitJupiter("5.10.3")
        }
    }
}

// Apply a specific Java toolchain to ease working on different environments.
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

tasks.jar {
    manifest.attributes["Main-Class"] = "com.example.demo.DemoApplication"
    manifest.attributes["Class-Path"] = configurations
        .runtimeClasspath
        .get()
        .joinToString(separator = " ") { file ->
            "libs/${file.name}"
        }
}

tasks.named<Jar>("jar") {
    enabled = false
}

 

3. DemoApplication.java

  • /data/gradle/gradle-8.11.1/app/src/main/java/com/example
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

4. HelloController.java

  • /data/gradle/gradle-8.11.1/app/src/main/java/com/example
package com.example.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    // SLF4J 로거 선언
    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);

    @GetMapping("/hello")
    public String hello(@RequestParam(defaultValue = "World") String name) {
        logger.debug("Received request to /hello with name={}", name);

        String response = "Hello, " + name + "!";
        logger.info("Returning response: {}", response);

        return response;
    }
}

5. 실행

  • 포그라운드 실행
./gradlew bootRun
  • 포그라운드 실행 - log 함께
./gradlew bootRun --debug
  • 백그라운드 실행
./gradlew bootRun > gradle.log 2>&1 &
반응형