Skip to content

Commit

Permalink
Publish all metadata after artifacts (gradle#28219)
Browse files Browse the repository at this point in the history
  • Loading branch information
bot-gradle authored Mar 7, 2024
2 parents 2eebf8f + eb7b513 commit ffe71ee
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,44 @@ class MavenPublishSnapshotIntegTest extends AbstractMavenPublishIntegTest {
snapshotVersions == ["1.0-SNAPSHOT"]
}
}

def "published metadata after artifacts"() {
settingsFile << 'rootProject.name = "snapshotPublish"'
buildFile << """
plugins {
id 'java'
id 'maven-publish'
}
group = 'org.gradle'
version = '1.0-SNAPSHOT'
publishing {
repositories {
maven { url "${mavenRepo.uri}" }
}
publications {
pub(MavenPublication) {
from components.java
}
}
}
"""
def module = mavenRepo.module('org.gradle', 'snapshotPublish', '1.0-SNAPSHOT')

when:
succeeds 'publish', '-i'

then:
def initialVersion = module.publishArtifactVersion
def publishTaskOutputLines = result.getGroupedOutput().task(':publishPubPublicationToMavenRepository').getOutput().split("\\R")

publishTaskOutputLines.collect { it =~ ~/Uploading (\S*).*/ }.findAll().collect { it.group(1) } == [
"snapshotPublish-${initialVersion}.jar",
"snapshotPublish-${initialVersion}.pom",
"snapshotPublish-${initialVersion}.module",
'maven-metadata.xml',
'maven-metadata.xml'
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,46 @@ protected void publish(MavenNormalizedPublication publication, ExternalResourceR
String version = publication.getVersion();

ModuleArtifactPublisher artifactPublisher = new ModuleArtifactPublisher(repository, localRepo, rootUri, groupId, artifactId, version);
SnapshotMetadataResult snapshotMetadataResult = computeSnapshotMetadata(publication, repository, localRepo, version, artifactPublisher, groupId, artifactId);

if (snapshotMetadataResult != null && !localRepo) {
// Use the timestamped version for all published artifacts
artifactPublisher.artifactVersion = snapshotMetadataResult.getVersion();
}

publishArtifactsAndMetadata(publication, artifactPublisher);

publishPublicationMetadata(repository, version, artifactPublisher, groupId, artifactId, snapshotMetadataResult);
}

@Nullable
private SnapshotMetadataResult computeSnapshotMetadata(MavenNormalizedPublication publication, ExternalResourceRepository repository, boolean localRepo, String version, ModuleArtifactPublisher artifactPublisher, String groupId, String artifactId) {
if (isSnapshot(version)) {
ExternalResourceName snapshotMetadataPath = artifactPublisher.getSnapshotMetadataLocation();
Metadata snapshotMetadata = createSnapshotMetadata(publication, groupId, artifactId, version, repository, snapshotMetadataPath);
return new SnapshotMetadataResult(snapshotMetadataPath, snapshotMetadata);
}
return null;
}

artifactPublisher.publish(snapshotMetadataPath, writeMetadataToTmpFile(snapshotMetadata, "snapshot-maven-metadata.xml"));

if (!localRepo) {
// Use the timestamped version for all published artifacts:
// The timestamped version is hidden deep in `Metadata.versioning.snapshotVersions`
artifactPublisher.artifactVersion = snapshotMetadata.getVersioning().getSnapshotVersions().get(0).getVersion();
}
private void publishPublicationMetadata(ExternalResourceRepository repository, String version, ModuleArtifactPublisher artifactPublisher, String groupId, String artifactId, @Nullable SnapshotMetadataResult snapshotMetadataResult) {
if (snapshotMetadataResult != null) {
artifactPublisher.publish(snapshotMetadataResult.snapshotMetadataPath, writeMetadataToTmpFile(snapshotMetadataResult.snapshotMetadata, "snapshot-maven-metadata.xml"));
}

ExternalResourceName externalResource = artifactPublisher.getMetadataLocation();
Metadata metadata = createMetadata(groupId, artifactId, version, repository, externalResource);
artifactPublisher.publish(externalResource, writeMetadataToTmpFile(metadata, "module-maven-metadata.xml"));
}

private static void publishArtifactsAndMetadata(MavenNormalizedPublication publication, ModuleArtifactPublisher artifactPublisher) {
if (publication.getMainArtifact() != null) {
artifactPublisher.publish(null, publication.getMainArtifact().getExtension(), publication.getMainArtifact().getFile());
}
artifactPublisher.publish(null, "pom", publication.getPomArtifact().getFile());
for (MavenArtifact artifact : publication.getAdditionalArtifacts()) {
artifactPublisher.publish(artifact.getClassifier(), artifact.getExtension(), artifact.getFile());
}

ExternalResourceName externalResource = artifactPublisher.getMetadataLocation();
Metadata metadata = createMetadata(groupId, artifactId, version, repository, externalResource);
artifactPublisher.publish(externalResource, writeMetadataToTmpFile(metadata, "module-maven-metadata.xml"));
}

private Metadata createMetadata(String groupId, String artifactId, String version, ExternalResourceRepository repository, ExternalResourceName metadataResource) {
Expand Down Expand Up @@ -177,6 +192,26 @@ public String toString() {

protected abstract Metadata createSnapshotMetadata(MavenNormalizedPublication publication, String groupId, String artifactId, String version, ExternalResourceRepository repository, ExternalResourceName metadataResource);

@NonNullApi
private static class SnapshotMetadataResult {
public final ExternalResourceName snapshotMetadataPath;
public final Metadata snapshotMetadata;

public SnapshotMetadataResult(ExternalResourceName snapshotMetadataPath, Metadata snapshotMetadata) {
this.snapshotMetadataPath = snapshotMetadataPath;
this.snapshotMetadata = snapshotMetadata;
}

/**
* The timestamped version is hidden deep in `Metadata.versioning.snapshotVersions`
*
* @return The snapshot timestamped version
*/
public String getVersion() {
return snapshotMetadata.getVersioning().getSnapshotVersions().get(0).getVersion();
}
}

/**
* Publishes artifacts for a single Maven module.
*/
Expand Down

0 comments on commit ffe71ee

Please sign in to comment.