AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79563368
Accepted
mikeb
mikeb
Asked: 2025-04-09 10:46:58 +0800 CST2025-04-09 10:46:58 +0800 CST 2025-04-09 10:46:58 +0800 CST

Gradle - 在哪里获取 Spring Boot 项目的“filesMatching”和“expand”

  • 772

尝试对我的 Spring Boot 3.4 项目使用以下 DSL,并且我的 build.gradle.kts 有以下内容 - 它抱怨在这里找不到filesMatching或expand方法。

tasks.named("processResources") {
    filesMatching("application.properties") {
        expand(project.properties)
    }
}

我需要什么插件?我有这个:

plugins {
    id("io.spring.dependency-management") version "1.1.7"
    id("org.springframework.boot") version "3.4.4"
    id("org.owasp.dependencycheck") version "12.1.0"
    kotlin("jvm") version "1.9.23"
    kotlin("plugin.spring") version "1.9.23"
}

我如何才能在这里使用这些方法?

spring-boot
  • 1 1 个回答
  • 38 Views

1 个回答

  • Voted
  1. Best Answer
    life888888
    2025-04-09T20:31:14+08:002025-04-09T20:31:14+08:00

    项目目录

    demo-kotlin-app
    ├── settings.gradle.kts
    ├── build.gradle.kts
    ├── gradle.properties
    └── src
        └── main
            ├── kotlin
            │   └── com
            │       └── example
            │           ├── DemoKotlinAppApplication.kt
            │           ├── model
            │           │   └── Person.kt
            │           ├── repository
            │           │   └── PersonRepository.kt
            │           ├── service
            │           │   └── PersonService.kt
            │           └── controller
            │               └── PersonController.kt
            └── resources
                ├── application.properties
                └── logback-spring.xml
    

    设置.gradle.kts

    该文件的内容是默认设置,没有被改变且不重要。

    rootProject.name = "demo-kotlin-app"
    

    构建.gradle.kts

    plugins {
        kotlin("jvm") version "1.9.25"
        kotlin("plugin.spring") version "1.9.25"
        id("org.springframework.boot") version "3.4.4"
        id("io.spring.dependency-management") version "1.1.7"
        kotlin("plugin.jpa") version "1.9.25"
    }
    
    group = "com.example"
    version = "0.0.1-SNAPSHOT"
    
    java {
        toolchain {
            languageVersion = JavaLanguageVersion.of(17)
        }
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation("org.springframework.boot:spring-boot-starter-data-jpa")
            implementation("org.springframework.boot:spring-boot-starter-validation")
        implementation("org.springframework.boot:spring-boot-starter-web")
        implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
        implementation("org.jetbrains.kotlin:kotlin-reflect")
            implementation("org.springframework.boot:spring-boot-starter-logging")
            implementation("com.mysql:mysql-connector-j")
    }
    
    kotlin {
        compilerOptions {
            freeCompilerArgs.addAll("-Xjsr305=strict")
        }
    }
    
    allOpen {
        annotation("jakarta.persistence.Entity")
        annotation("jakarta.persistence.MappedSuperclass")
        annotation("jakarta.persistence.Embeddable")
    }
    
    tasks.named<ProcessResources>("processResources") {
        filesMatching("application.properties") {
            expand(project.properties)
        }
    }
    

    笔记:

    我的配置:

    • tasks.named<ProcessResources>("processResources")
    tasks.named<ProcessResources>("processResources") {
        filesMatching("application.properties") {
            expand(project.properties)
        }
    }
    

    您的配置:

    • tasks.named("processResources")
    tasks.named("processResources") {
        filesMatching("application.properties") {
            expand(project.properties)
        }
    }
    

    gradle.properties

    此文件中的设置配置为与一起使用expand(project.properties),这会转换文件${XXX}中的占位符application.properties。

    serverPort=8080
    databaseUrl=jdbc:mysql://localhost:3306/demodb
    jdbcDriver=com.mysql.cj.jdbc.Driver
    databaseUser=demouser
    databasePassword=Passw0rd!
    dialect=org.hibernate.dialect.MySQL8Dialect
    

    DemoKotlinAppApplication.kt

    package com.example
    
    import org.springframework.boot.autoconfigure.SpringBootApplication
    import org.springframework.boot.runApplication
    
    @SpringBootApplication
    class DemoKotlinAppApplication
    
    fun main(args: Array<String>) {
        runApplication<DemoKotlinAppApplication>(*args)
    }
    

    Person.kt

    package com.example.model
    
    import jakarta.persistence.Entity
    import jakarta.persistence.GeneratedValue
    import jakarta.persistence.GenerationType
    import jakarta.persistence.Id
    
    @Entity
    data class Person(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        val id: Long = 0,
        val name: String,
        val email: String
    )
    

    PersonRepository.kt

    package com.example.repository
    
    import com.example.model.Person
    import org.springframework.data.jpa.repository.JpaRepository
    import org.springframework.stereotype.Repository
    
    @Repository
    interface PersonRepository : JpaRepository<Person, Long> {
        fun findByNameContaining(name: String): List<Person>
    }
    

    PersonService.kt

    package com.example.service
    
    import com.example.model.Person
    import com.example.repository.PersonRepository
    import org.slf4j.Logger
    import org.slf4j.LoggerFactory
    import org.springframework.stereotype.Service
    
    @Service
    class PersonService(private val personRepository: PersonRepository) {
        private val logger: Logger = LoggerFactory.getLogger(PersonService::class.java)
    
        fun getAllPersons(): List<Person> {
            logger.debug("Fetching all persons from the database")
            return personRepository.findAll()
        }
    
        fun getPersonById(id: Long): Person? {
            logger.debug("Fetching person with ID: $id")
            return personRepository.findById(id).orElse(null)
        }
    
        fun createPerson(name: String, email: String): Person {
            logger.debug("Creating person with name: $name and email: $email")
            val person = Person(name = name, email = email)
            return personRepository.save(person)
        }
    
        fun getPersonsByName(name: String): List<Person> {
            logger.debug("Searching for persons with name containing: $name")
            return personRepository.findByNameContaining(name)
        }
    }
    

    PersonController.kt

    package com.example.controller
    
    import com.example.model.Person
    import com.example.service.PersonService
    import org.slf4j.Logger
    import org.slf4j.LoggerFactory
    import org.springframework.http.HttpStatus
    import org.springframework.web.bind.annotation.*
    
    @RestController
    @RequestMapping("/persons")
    class PersonController(private val personService: PersonService) {
        private val logger: Logger = LoggerFactory.getLogger(PersonController::class.java)
    
        @GetMapping
        fun getAllPersons(): List<Person> {
            logger.info("Fetching all persons")
            return personService.getAllPersons()
        }
    
        @GetMapping("/{id}")
        fun getPersonById(@PathVariable id: Long): Person? {
            logger.info("Fetching person with ID: $id")
            return personService.getPersonById(id)
        }
    
        @PostMapping
        @ResponseStatus(HttpStatus.CREATED)
        fun createPerson(@RequestBody person: Person): Person {
            logger.info("Creating new person: ${person.name}")
            return personService.createPerson(person.name, person.email)
        }
    
        @GetMapping("/search")
        fun getPersonsByName(@RequestParam name: String): List<Person> {
            logger.info("Searching persons with name: $name")
            return personService.getPersonsByName(name)
        }
    }
    

    logback-spring.xml

    <configuration>
        <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss} - %msg%n</pattern>
            </encoder>
        </appender>
    
        <logger name="org.springframework" level="INFO"/>
        <logger name="com.example" level="DEBUG"/>
    
        <root level="INFO">
            <appender-ref ref="CONSOLE"/>
        </root>
    </configuration>
    

    应用程序.属性

    此文件中的占位符${XXXX}将使用 替换为 gradle.properties 文件的内容expand(project.properties)。

    spring.application.name=demo-kotlin-app
    server.port=${serverPort}
    
    spring.datasource.url=${databaseUrl}
    spring.datasource.username=${databaseUser}
    spring.datasource.password=${databasePassword}
    spring.datasource.driver-class-name=${jdbcDriver}
    spring.jpa.database-platform=${dialect}
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.show-sql=true
    spring.jpa.properties.hibernate.format_sql=true
    spring.jpa.properties.hibernate.type.descriptor.sql.BasicBinder.log.level=TRACE
    
    logging.level.org.springframework=INFO
    logging.level.com.example=DEBUG
    logging.level.root=INFO
    logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
    

    建造

    我的构建说明如下:

    gradle clean build
    

    跑步

    本示例需要MySQL,其配置如下:

    • jdbc:mysql://localhost:3306/demodb
    • 用户:demouser
    • 密码:Passw0rd!

    在执行以下命令之前,请确保 MySQL 正在运行。

    java -jar build/libs/demo-kotlin-app-0.0.1-SNAPSHOT.jar
    

    测试

    创建数据:

    curl -X POST http://localhost:8080/persons \
      -H "Content-Type: application/json" \
      -d '{
            "name": "John Doe",
            "email": "[email protected]"
          }'
    

    读取所有数据:

    curl -X GET http://localhost:8080/persons
    

    读取id = 1的数据

    curl -X GET http://localhost:8080/persons/1
    

    查看

    解压 demo-kotlin-app-0.0.1-SNAPSHOT.jar (demo-kotlin-app/build/libs/demo-kotlin-app-0.0.1-SNAPSHOT.jar)

    demo-kotlin-app/build/libs/demo-kotlin-app-0.0.1-SNAPSHOT/BOOT-INF/classes

    应用程序.属性

    该文件的${XXXX}内容已被转换,并且按预期显示。

    spring.application.name=demo-kotlin-app
    server.port=8080
    
    spring.datasource.url=jdbc:mysql://localhost:3306/demodb
    spring.datasource.username=demouser
    spring.datasource.password=Passw0rd!
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.show-sql=true
    spring.jpa.properties.hibernate.format_sql=true
    spring.jpa.properties.hibernate.type.descriptor.sql.BasicBinder.log.level=TRACE
    
    logging.level.org.springframework=INFO
    logging.level.com.example=DEBUG
    logging.level.root=INFO
    logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
    

    我的环境

    $ gradle -v
    
    ------------------------------------------------------------
    Gradle 8.10
    ------------------------------------------------------------
    
    Build time:    2024-08-14 11:07:45 UTC
    Revision:      fef2edbed8af1022cefaf44d4c0514c5f89d7b78
    
    Kotlin:        1.9.24
    Groovy:        3.0.22
    Ant:           Apache Ant(TM) version 1.10.14 compiled on August 16 2023
    Launcher JVM:  17.0.12 (Eclipse Adoptium 17.0.12+7)
    Daemon JVM:    /home/demo/.sdkman/candidates/java/17.0.12-tem (no JDK specified, using current Java home)
    OS:            Linux 6.8.0-57-generic amd64
    
    • 2

相关问题

  • 使用 spring boot oauth2 进行测试

  • Spring Boot响应编码不起作用

  • Spring Boot 3 和 Thymeleaf - 对象未以表单呈现

  • 想要使用eureka服务器使用微服务名称而不使用端口号来访问微服务

  • spring kafka registerListenerContainer 方法 startImmediately 标志 true 还是 false?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve