name.remal.gradle_plugins.toolkit.build_logic.publish-maven-to-build-dir.gradle Maven / Gradle / Ivy
if (project.isBuildSrcProject) {
return
}
allprojects {
pluginManager.withPlugin('maven-publish') {
TaskContainer tasks = project.tasks
tasks.register('publishToBuildDir') { Task task ->
task.group = 'publishing'
Closure getGeneratePomFileTask = { MavenPublication publication ->
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.layout.buildDirectory.dir(".m2").get().asFile
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 = new 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 - 2025 Weber Informatics LLC | Privacy Policy