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

io.github.pixee.maven.operator.UnwrapEffectivePom.kt Maven / Gradle / Ivy

package io.github.pixee.maven.operator

import org.codehaus.plexus.util.xml.Xpp3Dom
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.util.*

/**
 * Queries Version Set in the POM by inspecting the effective pom generated by Embedder
 */
class UnwrapEffectivePom : AbstractVersionCommand() {
    override fun execute(pm: ProjectModel): Boolean =
        try {
            executeInternal(pm)
        } catch (e: Exception) {
            LOGGER.warn("Failure", e)
            false
        }

    fun executeInternal(pm: ProjectModel): Boolean {
        val embedderFacadeResponse = EmbedderFacade.invokeEmbedder(
            EmbedderFacadeRequest(offline = pm.offline, pomFile = pm.pomFile.file)
        )

        val definedVersions: MutableSet =
            TreeSet(VERSION_KIND_COMPARATOR)

        val res = embedderFacadeResponse.modelBuildingResult

        listOf(
            res.effectiveModel.build.pluginManagement.plugins,
            res.effectiveModel.build.plugins,
        ).flatMap { it }.filter { p -> p.artifactId.equals("maven-compiler-plugin") }
            .mapNotNull { it.configuration }
            .filterIsInstance(Xpp3Dom::class.java)
            .map {
                val config = it as Xpp3Dom

                val definedOnes = TYPE_TO_KIND.entries.mapNotNull {
                    val child = config.getChild(it.key)

                    if (null != child) {
                        VersionDefinition(it.value, child.value)
                    } else {
                        null
                    }
                }.toList()

                definedVersions.addAll(definedOnes)
            }

        val definedProperties = res.effectiveModel.properties.mapNotNull {
            if (PROPERTY_TO_KIND.containsKey(it.key as String)) {
                val kind = PROPERTY_TO_KIND[it.key]!!

                VersionDefinition(kind, it.value as String)
            } else {
                null
            }
        }.toList()

        definedVersions.addAll(definedProperties)

        this.result.addAll(definedVersions)

        return definedVersions.isNotEmpty()
    }

    companion object {
        val LOGGER: Logger = LoggerFactory.getLogger(UnwrapEffectivePom::class.java)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy