jeudi 7 mai 2020

Gradle custom plugin using reflections throws "could not create Vfs.Dir from url"

I'm developing a custom Gradle plugin based on reflections: https://github.com/RoRoche/plantuml-gradle-plugin/tree/feature/using_reflections

I have to deal with some gradle specificities such as:

        final URL[] urls = project.sourceSets.main.output.classesDirs.files.collect {
            if (it != null) {
                it.toURI().toURL()
            }
        }.findAll {
            it != null
        } as URL[]
        getLogger().debug(
                "URLs to scan: " + urls
        )
        return new URLClassLoader(urls)

And then I construct the reflection such as:

/**
 * Utility class to find [Classes] in a given package.
 *
 * @property packageName The name of the package to scan.
 * @property reflections Utility to find classes in package.
 */
class ClsInPackage(
    private val packageName: String,
    private val reflections: Reflections
) : Classes {

    /**
     * Secondary constructor with reflections configuration.
     *
     * @param packageName The name of the package to scan.
     * @param configuration The [Configuration] to use.
     */
    constructor(
        packageName: String,
        configuration: Configuration
    ) : this(
        packageName = packageName,
        reflections = Reflections(configuration)
    )

    /**
     * Secondary constructor.
     *
     * @param packageName The name of the package to scan.
     * @param classLoader The [ClassLoader] to use.
     */
    constructor(
        packageName: String,
        classLoader: ClassLoader
    ) : this(
        packageName = packageName,
        configuration = ConfigurationBuilder(
        ).setUrls(
            ClasspathHelper.forClassLoader(classLoader)
        ).setScanners(
            SubTypesScanner(false),
            TypeAnnotationsScanner()
        ).addClassLoader(
            classLoader
        )
    )

    /**
     * Secondary constructor.
     *
     * @param packageName The name of the package to scan.
     * @param urls The [URL] array to use.
     */
    constructor(
        packageName: String,
        urls: Array<URL>
    ) : this(
        packageName = packageName,
        classLoader = URLClassLoader(urls)
    )

    /**
     * @return Classes to be used for diagram generation.
     */
    override fun list(): List<Class<out Any>> {
        val list = reflections.getSubTypesOf(
            Any::class.java
        ).asIterable().toList()
        if (list.isNullOrEmpty()) {
            throw InvalidPackageException(packageName)
        }
        return list
    }
}

After publishing my plugin to mavenLocal and use it in another project, I reach the following failure:

org.reflections.ReflectionsException: could not create Vfs.Dir from url, no matching UrlType was found [file:/Users/Romain/GitHub/eo-plantuml-builder/build/classes/java/main]
either use fromURL(final URL url, final List<UrlType> urlTypes) or use the static setDefaultURLTypes(final List<UrlType> urlTypes) or addDefaultURLTypes(UrlType urlType) with your specialized UrlType.

Any idea how to fix it?





Aucun commentaire:

Enregistrer un commentaire