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

name.remal.gradle_plugins.toolkit.build_logic.publish-maven-to-build-dir.gradle Maven / Gradle / Ivy

There is a newer version: 0.64.11
Show newest version
if (project.isBuildSrcProject) return

allprojects {
    pluginManager.withPlugin('maven-publish') {
        tasks.register('publishToBuildDir') { Task task ->
            Closure getGeneratePomFileTask = { MavenPublication publication ->
                project.tasks.named(
                    "generatePomFileFor${publication.name.capitalize()}Publication",
                    GenerateMavenPom
                ).get()
            }

            NamedDomainObjectSet publications = publishing.publications.withType(MavenPublication)
            task.dependsOn(project.provider { publications.collect { getGeneratePomFileTask(it) } })
            task.dependsOn(project.provider { publications.collect { it.getArtifacts() }.flatten() })

            task.inputs.files(project.provider { publications.collect { getGeneratePomFileTask(it).destination }.flatten() }).optional().withPropertyName('pomFiles')
            task.inputs.files(project.provider { publications.collect { it.artifacts.collect { it.file } }.flatten() }).optional().withPropertyName('artifactFiles')

            File outputDir = project.file("${project.buildDir}/.m2")
            task.outputs.dir(outputDir).withPropertyName('outputDir')
            task.ext.outputDir = outputDir

            task.doFirst {
                outputDir.delete()
                outputDir.deleteDir()
                outputDir.mkdirs()
            }

            task.doLast {
                publications.forEach { publication ->
                    File artifactDir = project.file("${outputDir}/${publication.groupId.replace('.', '/')}/${publication.artifactId}")
                    File versionDir = new File(artifactDir, publication.version)

                    File pomFile = getGeneratePomFileTask(publication).destination
                    File pomDestFile = new File(versionDir, "${publication.artifactId}-${publication.version}.pom")
                    pomDestFile.parentFile.mkdirs()
                    pomDestFile.bytes = pomFile.bytes

                    publication.getArtifacts().forEach { artifact ->
                        StringBuilder artifactDestFileName = new StringBuilder()
                        artifactDestFileName.append(publication.artifactId).append('-').append(publication.version)
                        if (artifact.classifier != null && !artifact.classifier.isEmpty()) {
                            artifactDestFileName.append('-').append(artifact.classifier)
                        }
                        if (artifact.extension != null && !artifact.extension.isEmpty()) {
                            artifactDestFileName.append('.').append(artifact.extension)
                        } else {
                            artifactDestFileName.append('.').append('jar')
                        }
                        File artifactDestFile = new File(versionDir, artifactDestFileName.toString())
                        artifactDestFile.parentFile.mkdirs()
                        artifactDestFile.bytes = artifact.file.bytes
                    }

                    File mavenMetadataLocalFile = new File(artifactDir, 'maven-metadata-local.xml')
                    mavenMetadataLocalFile.parentFile.mkdirs()
                    mavenMetadataLocalFile.setText(
                        [
                            '',
                            '',
                            '  ' + publication.groupId + '',
                            '  ' + publication.artifactId + '',
                            '  ',
                            '    ' + publication.version + '',
                            '    ',
                            '      ' + publication.version + '',
                            '    ',
                            '    ' + System.currentTimeMillis() + '',
                            '  ',
                            '',
                        ].join('\n'),
                        'UTF-8'
                    )
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy