【问题标题】:Can not ready file text within build.gradle.kts无法在 build.gradle.kts 中准备好文件文本
【发布时间】:2020-06-24 14:40:51
【问题描述】:

我试图在编译它以生成另一个类之前打印一个 kotlin 类。我尝试了很多方法来确保在通过File.readText() 编译文件之前打印文件文本,但没有成功-ed 总是打印带有符号的代码,如下面的截图。

我的 Gradle 代码

tasks.findByName("compileKotlin")
    ?.dependsOn(tasks.register("compileKotlin2") {
        doFirst {
            var names2 = "filesPath= "
            fileTree("$buildDir\\classes\\kotlin\\main\\tech\\example\\ecommerce\\ecommerce\\controller\\")
                    .visit {
                        if (this.file.name != "WebPagesController.class") {
                            names2 += this.file.path + "\n"

                            println(file(this.file.path).readText())
                        }
                    }
            println(names2)
        }
    })

完整的 build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("plugin.jpa") version "1.3.61"
    id("org.springframework.boot") version "2.2.5.RELEASE"
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
    kotlin("jvm") version "1.3.61"
    kotlin("plugin.spring") version "1.3.61"
}

group = "tech.example.ecommerce"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}

dependencies {

    // springframework
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
    implementation("org.springframework.boot:spring-boot-starter-mail")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("mysql:mysql-connector-java")

    // kotlin
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    // testing
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    }
}
tasks.findByName("compileKotlin")
        ?.dependsOn(tasks.register("compileKotlin2") {
            doFirst {
                println("hiiiiiiiii")
                var names2 = "a= "
                fileTree("$buildDir\\classes\\kotlin\\main\\tech\\example\\ecommerce\\ecommerce\\controller\\")
                        .visit {
                            if (this.file.name != "WebPagesController.class") {
                                names2 += this.file.path + "\n"

                                println(file(this.file.path).readText())
                            }
                        }
                println(names2)
            }
        })


tasks.withType<Test> {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "1.8"
    }
}

欢迎任何提示,提前致谢

【问题讨论】:

    标签: intellij-idea android-gradle-plugin gradle-kotlin-dsl


    【解决方案1】:

    fileTree("$buildDir\\classes\\kotlin\\main\\tech\\example\\ecommerce\\ecommerce\\controller\\") 使用 projectDir 而不是 buildDir 的语法 fileTree("$projectDir/src/main/kotlin/tech/example/ecommerce/ecommerce/controller")s

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-29
      相关资源
      最近更新 更多