Skip to content

Commit

Permalink
gradle: Avoid setting a manifest object
Browse files Browse the repository at this point in the history
Instead of setting a manifest object in the task so that we can
set the effective manifest, after building we merge the generated
manifest into the existing manifest object.

Fixes #5275

Signed-off-by: BJ Hargrave <bj@hargrave.dev>
  • Loading branch information
bjhargrave committed Jun 4, 2022
1 parent 6e3fd53 commit d06e702
Showing 1 changed file with 13 additions and 87 deletions.
Expand Up @@ -37,9 +37,6 @@
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.java.archives.Attributes;
import org.gradle.api.java.archives.ManifestException;
import org.gradle.api.java.archives.ManifestMergeSpec;
import org.gradle.api.java.archives.internal.DefaultManifest;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
Expand All @@ -66,7 +63,6 @@
import aQute.lib.io.IO;
import aQute.lib.strings.Strings;
import aQute.lib.utf8properties.UTF8Properties;
import groovy.lang.Closure;

/**
* BundleTaskExtension for Gradle.
Expand Down Expand Up @@ -100,7 +96,6 @@ public class BundleTaskExtension {
private final ConfigurableFileCollection classpath;
private final Provider<String> bnd;
private final MapProperty<String, Object> properties;
private final EffectiveManifest effectiveManifest;

/**
* The bndfile property.
Expand Down Expand Up @@ -216,12 +211,6 @@ public BundleTaskExtension(org.gradle.api.tasks.bundling.Jar task) {
classpath(mainSourceSet.getCompileClasspath());
properties = objects.mapProperty(String.class, Object.class)
.convention(Maps.of("project", "__convention__"));
// Wrap manifest
org.gradle.api.java.archives.Manifest manifest = task.getManifest();
effectiveManifest = new EffectiveManifest(manifest);
if (manifest != null) {
task.setManifest(effectiveManifest);
}
// need to programmatically add to inputs since @InputFiles in a
// extension is not processed
task.getInputs()
Expand Down Expand Up @@ -514,8 +503,19 @@ public void execute(Task t) {
builtJar.write(archiveFile);
long now = System.currentTimeMillis();
archiveFile.setLastModified(now);
// Set effective manifest to generated manifest
effectiveManifest.setEffectiveManifest(builtJar.getManifest());
// Set effective manifest from generated manifest
Manifest manifest = builtJar.getManifest();
org.gradle.api.java.archives.Manifest mergeManifest = new DefaultManifest(null);
mergeManifest.attributes(new AttributesMap(manifest.getMainAttributes()));
manifest.getEntries()
.forEach((section, attrs) -> mergeManifest.attributes(new AttributesMap(attrs), section));
getTask().getManifest()
.from(mergeManifest, merge -> merge.eachEntry(details -> {
if (details.getMergeValue() == null) {
// exclude if entry not in merge manifest
details.exclude();
}
}));
logReport(builder, getTask().getLogger());
if (!builder.isOk()) {
failTask("Bundle " + archiveFileName + " has errors", archiveFile);
Expand All @@ -537,80 +537,6 @@ private boolean isEmpty(String header) {
}
}

static final class EffectiveManifest implements org.gradle.api.java.archives.Manifest {
final org.gradle.api.java.archives.Manifest delegate;
Manifest effectiveManifest;

EffectiveManifest(org.gradle.api.java.archives.Manifest delegate) {
this.delegate = delegate;
this.effectiveManifest = null;
}

void setEffectiveManifest(Manifest effectiveManifest) {
this.effectiveManifest = effectiveManifest;
}

@Override
public org.gradle.api.java.archives.Manifest attributes(Map<String, ?> attributes) throws ManifestException {
delegate.attributes(attributes);
return this;
}

@Override
public org.gradle.api.java.archives.Manifest attributes(Map<String, ?> attributes, String sectionName)
throws ManifestException {
delegate.attributes(attributes, sectionName);
return this;
}

@Override
public org.gradle.api.java.archives.Manifest from(Object... mergePath) {
delegate.from(mergePath);
return this;
}

@Override
public org.gradle.api.java.archives.Manifest from(Object mergePath, Closure<?> closure) {
delegate.from(mergePath, closure);
return this;
}

@Override
public org.gradle.api.java.archives.Manifest from(Object mergePath, Action<ManifestMergeSpec> action) {
delegate.from(mergePath, action);
return this;
}

@Override
public Attributes getAttributes() {
return delegate.getAttributes();
}

@Override
public org.gradle.api.java.archives.Manifest getEffectiveManifest() {
Manifest effectiveManifest = this.effectiveManifest;
if (effectiveManifest == null) {
return delegate.getEffectiveManifest();
}
org.gradle.api.java.archives.Manifest result = new DefaultManifest(null);
result.attributes(new AttributesMap(effectiveManifest.getMainAttributes()));
effectiveManifest.getEntries()
.forEach((section, attrs) -> result.attributes(new AttributesMap(attrs), section));
return result;
}

@Override
public Map<String, Attributes> getSections() {
return delegate.getSections();
}

@Override
public org.gradle.api.java.archives.Manifest writeTo(Object path) {
getEffectiveManifest().writeTo(path);
return this;
}
}

static final class AttributesMap extends AbstractMap<String, Object> {
final java.util.jar.Attributes source;

Expand Down

0 comments on commit d06e702

Please sign in to comment.