0

I am trying to print a kotlin class before compiling it to generate another class. I tried many methods to be sure about printing the file text before compiling it through File.readText() but nothings success-ed always printing code joined with symbols as the following screenshot.

enter image description here

My Gradle code

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)
        }
    })

Full 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"
    }
}

Any hints are welcomed, Thanks in advance

Dasser Basyouni
  • 3,142
  • 5
  • 26
  • 50

1 Answers1

0

In fileTree("$buildDir\\classes\\kotlin\\main\\tech\\example\\ecommerce\\ecommerce\\controller\\") using projectDir instead of buildDir with syntax fileTree("$projectDir/src/main/kotlin/tech/example/ecommerce/ecommerce/controller") s

Dasser Basyouni
  • 3,142
  • 5
  • 26
  • 50