Skip to content

Commit

Permalink
Fix minimize for configuration cache support
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Merlin <[email protected]>
  • Loading branch information
eskatos committed Sep 12, 2020
1 parent a45502d commit c9e30f1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.artifacts.SelfResolvingDependency
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.SourceSet
import org.vafer.jdependency.Clazz
import org.vafer.jdependency.Clazzpath
import org.vafer.jdependency.ClazzpathUnit
Expand All @@ -18,7 +17,7 @@ class UnusedTracker {
private final List<ClazzpathUnit> projectUnits
private final Clazzpath cp = new Clazzpath()

private UnusedTracker(List<File> classDirs, FileCollection classJars, FileCollection toMinimize) {
private UnusedTracker(Iterable<File> classDirs, FileCollection classJars, FileCollection toMinimize) {
this.toMinimize = toMinimize
projectUnits = classDirs.collect { cp.addClazzpathUnit(it) }
projectUnits.addAll(classJars.collect { cp.addClazzpathUnit(it) })
Expand All @@ -39,16 +38,8 @@ class UnusedTracker {
}
}

static UnusedTracker forProject(Project project, List<Configuration> configurations, DependencyFilter dependencyFilter) {
def apiJars = getApiJarsFromProject(project)
FileCollection toMinimize = dependencyFilter.resolve(configurations) - apiJars

final List<File> classDirs = new ArrayList<>()
for (SourceSet sourceSet in project.sourceSets) {
Iterable<File> classesDirs = sourceSet.output.classesDirs
classDirs.addAll(classesDirs.findAll { it.isDirectory() })
}
return new UnusedTracker(classDirs, apiJars, toMinimize)
static UnusedTracker forProject(FileCollection apiJars, Iterable<File> sourceSetsClassesDirs, FileCollection toMinimize) {
return new UnusedTracker(sourceSetsClassesDirs, apiJars, toMinimize)
}

@InputFiles
Expand All @@ -71,7 +62,7 @@ class UnusedTracker {
}
}

private static FileCollection getApiJarsFromProject(Project project) {
static FileCollection getApiJarsFromProject(Project project) {
def apiDependencies = project.configurations.asMap['api']?.dependencies ?: null
if (apiDependencies == null) return project.files()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.gradle.api.tasks.bundling.Jar;
import org.gradle.api.tasks.util.PatternSet;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -30,10 +31,14 @@ public class ShadowJar extends Jar implements ShadowSpec {

private List<Transformer> transformers;
private List<Relocator> relocators;
private List<Configuration> configurations;
private transient List<Configuration> configurations;
private transient DependencyFilter dependencyFilter;

private boolean minimizeJar;
private transient DependencyFilter dependencyFilterForMinimize;
private FileCollection toMinimize;
private FileCollection apiJars;
private FileCollection sourceSetsClassesDirs;

private final ShadowStats shadowStats = new ShadowStats();
private final GradleVersionUtil versionUtil;
Expand Down Expand Up @@ -108,12 +113,55 @@ public InheritManifest getManifest() {
@Override
protected CopyAction createCopyAction() {
DocumentationRegistry documentationRegistry = getServices().get(DocumentationRegistry.class);
final UnusedTracker unusedTracker = minimizeJar ? UnusedTracker.forProject(getProject(), configurations, dependencyFilterForMinimize) : null;
final UnusedTracker unusedTracker = minimizeJar ? UnusedTracker.forProject(getApiJars(), getSourceSetsClassesDirs().getFiles(), getToMinimize()) : null;
return new ShadowCopyAction(getArchiveFile().get().getAsFile(), getInternalCompressor(), documentationRegistry,
this.getMetadataCharset(), transformers, relocators, getRootPatternSet(), shadowStats,
versionUtil, isPreserveFileTimestamps(), minimizeJar, unusedTracker);
}

@Classpath
FileCollection getToMinimize() {
if (toMinimize == null) {
toMinimize = minimizeJar
? dependencyFilterForMinimize.resolve(configurations).minus(getApiJars())
: getProject().getObjects().fileCollection();
}
return toMinimize;
}


@Classpath
FileCollection getApiJars() {
if (apiJars == null) {
apiJars = minimizeJar
? UnusedTracker.getApiJarsFromProject(getProject())
: getProject().getObjects().fileCollection();
}
return apiJars;
}


@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
FileCollection getSourceSetsClassesDirs() {
if (sourceSetsClassesDirs == null) {
ConfigurableFileCollection allClassesDirs = getProject().getObjects().fileCollection();
if (minimizeJar) {
for (SourceSet sourceSet : getProject().getExtensions().getByType(SourceSetContainer.class)) {
FileCollection classesDirs = sourceSet.getOutput().getClassesDirs();
allClassesDirs.from(classesDirs);
}
}
sourceSetsClassesDirs = allClassesDirs.filter(new Spec<File>() {
@Override
public boolean isSatisfiedBy(File file) {
return file.isDirectory();
}
});
}
return sourceSetsClassesDirs;
}

@Internal
protected ZipCompressor getInternalCompressor() {
return versionUtil.getInternalCompressor(getEntryCompression(), this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.jengelman.gradle.plugins.shadow

import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification
import groovy.transform.NotYetImplemented
import org.gradle.util.GradleVersion
import spock.lang.IgnoreIf

Expand Down Expand Up @@ -81,7 +80,6 @@ class ConfigurationCacheSpec extends PluginSpecification {
result.output.contains("Reusing configuration cache.")
}

@NotYetImplemented
def "configuration caching supports minimize"() {
given:
file('settings.gradle') << """
Expand Down

0 comments on commit c9e30f1

Please sign in to comment.