All Downloads are FREE. Search and download functionalities are using the official Maven repository.

name.remal.building.gradle_plugins.ClassesProcessingPlugin.kt Maven / Gradle / Ivy

There is a newer version: 1.9.2
Show newest version
package name.remal.building.gradle_plugins

import mu.KLogging
import name.remal.building.gradle_plugins.utils.ProjectPlugin
import name.remal.building.gradle_plugins.utils.configure
import name.remal.building.gradle_plugins.utils.doLastOrdered
import org.gradle.api.Project
import org.gradle.api.tasks.compile.AbstractCompile
import java.io.File
import java.net.URL
import java.net.URLClassLoader

open class ClassesProcessingPlugin : ProjectPlugin() {

    companion object : KLogging() {
        const val CLASSES_PROCESSING_RUNNER_CLASS = "name.remal.building.classes_processing.internal.ClassesProcessorRunner"
    }

    override fun apply(project: Project) {
        URL("jar:file://valid_jar_url_syntax.jar!/").openConnection().defaultUseCaches = false

        project.tasks.configure(AbstractCompile::class.java) { task ->
            task.doLastOrdered {
                if (task.classpath.isEmpty) return@doLastOrdered
                if (!task.destinationDir.exists()) return@doLastOrdered

                logger.info("Processing classes in ${task.destinationDir}")

                val classpathURLs = task.classpath.filter(File::exists).map { it.toURI().toURL() }.toTypedArray()
                URLClassLoader(classpathURLs, Thread.currentThread().contextClassLoader).use { classLoader ->
                    val runnerClass: Class<*>
                    try {
                        runnerClass = classLoader.loadClass(CLASSES_PROCESSING_RUNNER_CLASS)
                    } catch (e: ClassNotFoundException) {
                        return@use
                    }

                    runnerClass.getMethod("run", File::class.java).invoke(null, task.destinationDir)
                }
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy