Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
onobc committed Oct 6, 2023
1 parent 2d190a3 commit 1fe909f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,17 @@ private void addMavenOptionalFeature(Project project) {
javaComponent.addVariantsFromConfiguration(
project.getConfigurations().findByName("mavenOptionalRuntimeElements"),
ConfigurationVariantDetails::mapToOptional);
// TODO comment
javaComponent.withVariantsFromConfiguration(
project.getConfigurations().findByName("mavenOptionalRuntimeElements"),
ConfigurationVariantDetails::skip);
}

private void suppressMavenOptionalFeatureWarnings(MavenPublication publication) {
publication.suppressPomMetadataWarningsFor("mavenOptionalApiElements");
publication.suppressPomMetadataWarningsFor("mavenOptionalRuntimeElements");
publication.suppressPomMetadataWarningsFor("mavenOptionalRuntimeElementsClasses");
publication.suppressPomMetadataWarningsFor("mavenOptionalRuntimeElementsResources");
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.springframework.pulsar.gradle.publish;

import java.util.LinkedHashSet;
import java.util.stream.Collectors;

import org.gradle.api.Plugin;
Expand All @@ -9,38 +8,48 @@
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication;
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;

import org.springframework.util.ReflectionUtils;

public class PublishAllJavaComponentsPlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
project.getPlugins().withType(MavenPublishPlugin.class).all((mavenPublish) -> {
PublishingExtension publishing = project.getExtensions().getByType(PublishingExtension.class);

// To get around some weirdness w/ the Signing plugin attempting to use java-classes-dir and java-resources-dir
// as artifacts to sign, we load the artifacts into a hidden dummy MavenPublication using from(components.java)
// and then filter out the unwanted artifacts. Finally, we add those to the actual mavenJava artifact.
// NOTE: If MavenPublication.setArtifacts is called after MavenPublication.from then downstream issues will
// occur when building due to artifacts modified exceptions.

var filteredMainArtifacts = new LinkedHashSet<>();
var hiddenMavenJavaPub = publishing.getPublications().create("hiddenMavenJava", MavenPublication.class, maven -> {
publishing.getPublications().create("mavenJava", MavenPublication.class, mavenPublication -> {
project.getPlugins().withType(JavaPlugin.class, (plugin) -> {
maven.from(project.getComponents().getByName("java"));
filteredMainArtifacts.addAll(maven.getArtifacts().stream().filter((ma) -> ma.getFile().isFile()).collect(Collectors.toSet()));
});
});
publishing.getPublications().remove(hiddenMavenJavaPub);
publishing.getPublications().create("mavenJava", MavenPublication.class, maven -> {
project.getPlugins().withType(JavaPlugin.class, (plugin) -> {
filteredMainArtifacts.forEach((ma) -> maven.artifact(ma));
mavenPublication.from(project.getComponents().getByName("java"));
//filterOutClassesAndResourcesArtifacts(mavenPublication);
});
project.getPlugins().withType(JavaPlatformPlugin.class, (plugin) -> {
maven.from(project.getComponents().getByName("javaPlatform"));
mavenPublication.from(project.getComponents().getByName("javaPlatform"));
});
});
});
}

/**
Workaround for bug in SigningPlugin where it attempts to sign 'java-classes-dir' and 'java-resources-dir'
from the 'mavenOptionalRuntimeElements' and 'mavenOptionalRuntimeElementsResources', respsectively.
GOAL: Filter out these artifacts from the MavenJava publication.
Mavebas artifacts to sign, we filter out the unwanted artifacts from the mavenJava publication and the
have to reflectively set `MavenPublication.artifactsOverridden = false' and reflectively invoke
'MavenPublication.updateModuleDescriptorArtifact()'
* @param mavenPublication
*/
private void filterOutClassesAndResourcesArtifacts(MavenPublication mavenPublication) {
var artifacts = mavenPublication.getArtifacts().stream().filter((ma) -> ma.getFile().isFile()).collect(Collectors.toList());
mavenPublication.setArtifacts(artifacts);
var f = ReflectionUtils.findField(DefaultMavenPublication.class, "artifactsOverridden");
ReflectionUtils.makeAccessible(f);
ReflectionUtils.setField(f, mavenPublication, Boolean.FALSE);
var m = ReflectionUtils.findMethod(DefaultMavenPublication.class, "updateModuleDescriptorArtifact");
ReflectionUtils.makeAccessible(m);
ReflectionUtils.invokeMethod(m, mavenPublication);
}

}

0 comments on commit 1fe909f

Please sign in to comment.