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

name.remal.gradle_plugins.toolkit.build_logic.gradle-plugin.gradle Maven / Gradle / Ivy

import org.gradle.api.internal.artifacts.dependencies.SelfResolvingDependencyInternal

if (project.isBuildSrcProject) {
    return
}

allprojects {
    pluginManager.withPlugin('java-gradle-plugin') {
        configurations.all { Configuration conf ->
            conf.dependencies.all { Dependency dep ->
                if (conf.state != Configuration.State.UNRESOLVED) {
                    return
                }
                if (dep instanceof SelfResolvingDependencyInternal) {
                    if (dep.targetComponentId != null) {
                        conf.dependencies.remove(dep)
                    }
                }
            }
        }

        configurations.create('gradleApi') { Configuration conf ->
            conf.canBeResolved = true
            conf.canBeConsumed = false
            conf.description = "Gradle API"
            configurations.optional.extendsFrom(conf)
        }

        dependencies {
            gradleApi 'name.remal.gradle-api:gradle-api'
            testImplementation 'name.remal.gradle-api:gradle-test-kit'
        }


        List> loggingExclusions = [
            [group: 'org.slf4j', module: '*'],
            [group: 'log4j', module: 'log4j'],
            [group: 'commons-logging', module: 'commons-logging'],
            [group: 'org.springframework', module: 'spring-jcl'],
        ]

        Closure> getGradleApiResolvedDependencies = {
            return configurations.gradleApi
                .resolvedConfiguration
                .lenientConfiguration
                .allModuleDependencies
                .findAll { !it.moduleArtifacts.isEmpty() }
        }.memoize()

        sourceSets.all { SourceSet sourceSet ->
            Collection includedConfigurationNames = project.getSourceSetConfigurationNames(sourceSet) + [
                'compileOnlyAll',
                'optional',
                'optionalHidden',
                'shadow',
            ]

            Collection excludedConfigurationNames = [
                sourceSet.annotationProcessorConfigurationName,
            ]

            configurations
                .matching { includedConfigurationNames.contains(it.name) }
                .matching { !excludedConfigurationNames.contains(it.name) }
                .configureEach { Configuration conf ->
                    conf.dependencies.withType(ExternalModuleDependency).configureEach { dep ->
                        if (dep.group != 'name.remal.gradle-api') {
                            loggingExclusions.forEach { dep.exclude(it) }
                            getGradleApiResolvedDependencies().forEach { resolvedDep ->
                                dep.exclude(group: resolvedDep.moduleGroup, module: resolvedDep.moduleName)
                            }
                        }
                    }
                }
        }

        configurations.matching { ['shadow', 'excludeFromClassesRelocation'].contains(it.name) }.configureEach { conf ->
            conf.dependencies.addAllLater(provider {
                Collection resolvedDeps = getGradleApiResolvedDependencies()
                return resolvedDeps.collect {
                    project.dependencies.create("${it.moduleGroup}:${it.moduleName}:${it.moduleVersion}")
                }
            })
        }


        SourceSet functionalTestSourceSet = testSourceSets.create('functionalTest')

        project.file('src/functional').with { dir ->
            if (dir.exists()) {
                throw new GradleException("$dir should be renamed to ${functionalTestSourceSet.name}")
            }
        }

        project.tasks.named(functionalTestSourceSet.name, Test) { Test task ->
            Closure> getPublishToBuildDirTasks = {
                rootProject.allprojects
                    .collect { it.tasks.matching { it.name == 'publishToBuildDir' } }
                    .flatten()
            }

            dependsOn(project.provider { getPublishToBuildDirTasks() })

            onlyIf {
                task.systemProperty(
                    'build-dir-maven-repos',
                    getPublishToBuildDirTasks()
                        .collect { it.outputDir.absolutePath }
                        .toSorted()
                        .join(File.pathSeparator)
                )
                return true
            }
        }


        Closure getCorrespondingKotlinVersion = { project.getGradleApiDependencyVersion('org.jetbrains.kotlin:kotlin-stdlib') }.memoize()

        tasks.withType(Test).configureEach {
            onlyIf {
                String correspondingKotlinVersion = getCorrespondingKotlinVersion()
                systemProperty('corresponding-kotlin.version', correspondingKotlinVersion ?: '')
                return true
            }
        }

        configurations.create('correspondingKotlinPlugin') { Configuration conf ->
            conf.canBeResolved = false
            conf.canBeConsumed = false

            conf.defaultDependencies { deps ->
                String correspondingKotlinVersion = getCorrespondingKotlinVersion()
                Dependency kotlinPluginDep = project.dependencies.create(
                    "org.jetbrains.kotlin:kotlin-gradle-plugin:${correspondingKotlinVersion}"
                ) {
                    exclude(group: 'commons-logging', module: 'commons-logging')
                    exclude(group: 'com.google.code.findbugs')
                    exclude(group: 'org.checkerframework')
                    exclude(group: 'com.google.errorprone')
                    exclude(group: 'com.google.j2objc', module: 'j2objc-annotations')

                    attributes {
                        attribute(
                            Usage.USAGE_ATTRIBUTE,
                            objects.named(Usage.class, Usage.JAVA_RUNTIME)
                        )
                        attribute(
                            Category.CATEGORY_ATTRIBUTE,
                            objects.named(Category.class, Category.LIBRARY)
                        )
                    }
                }

                deps.add(kotlinPluginDep)
            }

            conf.extendsFrom = []
            conf.description = 'Kotlin Gradle plugin os the same version, as Kotlin that bundled in the current Gradle instance'
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy