无法在 build.gradle.kts 中准备好文件文本

Can not ready file text within build.gradle.kts

提问人:Dasser Basyouni 提问时间:3/12/2020 更新时间:3/14/2020 访问量:514

问:

我正在尝试打印一个 kotlin 类,然后再编译它以生成另一个类。我尝试了许多方法来确保在编译文件文本之前打印文件文本,但没有任何成功,总是打印带有符号的代码,如下图所示。File.readText()

enter image description here

我的 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

评论


答:

0赞 Dasser Basyouni 3/14/2020 #1

在使用而不是与语法 sfileTree("$buildDir\\classes\\kotlin\\main\\tech\\example\\ecommerce\\ecommerce\\controller\\")projectDirbuildDirfileTree("$projectDir/src/main/kotlin/tech/example/ecommerce/ecommerce/controller")