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

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

The newest version!
if (project.isBuildSrcProject) {
    return
}

allprojects {
    pluginManager.withPlugin('name.remal.classes-relocation') {
        dependencies {
            excludeFromForcedClassesRelocation 'com.google.code.findbugs:jsr305:3.0.2'
            excludeFromForcedClassesRelocation 'org.jetbrains:annotations:26.0.1'
            excludeFromForcedClassesRelocation 'com.github.spotbugs:spotbugs-annotations:4.8.6'
        }

        classesRelocation {
            relocatedClassesPackageName = "${project.calculateBaseJavaPackage()}.internal._relocated"
        }

        List relocationConfNames = [
            'relocateClasses',
            'excludeFromClassesRelocation',
            'excludeFromForcedClassesRelocation',
        ]

        tasks.withType(AbstractCompile).configureEach { AbstractCompile task ->
            task.inputs.property('relocatedClassesPackageName', classesRelocation.relocatedClassesPackageName)
            relocationConfNames.forEach { relocationConfName ->
                task.dependsOn(configurations.named(relocationConfName))
                task.inputs.files(configurations.named(relocationConfName))
                    .optional()
                    .withPropertyName("configuration-$relocationConfName")
            }
        }

        configurations.matching { relocationConfNames.contains(it.name) }.configureEach { Configuration conf ->
            conf.canBeConsumed = false

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


    pluginManager.withPlugin('com.gradleup.shadow') {
        configurations.compileOnly.extendsFrom(configurations.shadow)

        configurations.named('shadow') { Configuration conf ->
            conf.dependencies.withType(ModuleDependency).configureEach { ModuleDependency dep ->
                ; [
                    [group: 'com.google.code.findbugs', module: 'jsr305'],
                    [group: 'org.checkerframework', module: 'checker-qual'],
                    [group: 'com.google.errorprone', module: 'error_prone_annotations'],
                    [group: 'com.google.j2objc', module: 'j2objc-annotations'],
                    [group: 'org.jetbrains', module: 'annotations'],
                    [group: 'com.google.auto.service', module: 'auto-service'],
                ].forEach { dep.exclude(it) }
            }

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

        TaskProvider jar = tasks.named('jar', Jar) {
            archiveClassifier = 'original'
        }

        TaskProvider shadowJar = tasks.named('shadowJar', Jar) {
            dependsOn('classes')
            configurations = [project.configurations.shadow]
            archiveClassifier = ''

            enableRelocation = true
            relocationPrefix = "${project.calculateBaseJavaPackage()}.internal._shadow"
            mergeServiceFiles()
            mergeGroovyExtensionModules()
            duplicatesStrategy = DuplicatesStrategy.INCLUDE

            exclude('**/module-info.class')
            exclude('**/package-info.class')
            exclude('org/gradle/**')

            if (true) {
                minimize()
            }

            //doLast { println stats }
        }

        configurations.matching { it.canBeConsumed }.all { Configuration conf ->
            boolean hasJar = conf.artifacts.removeIf { it.file == jar.get().archiveFile.get().asFile }
            if (hasJar) {
                project.artifacts.add(conf.name, shadowJar)
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy