-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds two new configuration options to the 'shadowJar' task. When 'minimizeJar' is set the shadow JAR would only include the classes for the project the task operates on and its dependencies. The user can protect other classes from minimization by specifying them as entry points. Here's an example configuration shadowJar { minimizeJar = true entryPoint 'foo.Foo' entryPoint 'foo.Bar' } N.B. imported but unused classes are not considered as dependencies.
- Loading branch information
1 parent
f21720a
commit 8c7c1af
Showing
9 changed files
with
232 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/groovy/com/github/jengelman/gradle/plugins/shadow/internal/UnusedTracker.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.github.jengelman.gradle.plugins.shadow.internal | ||
|
||
import org.gradle.api.Project | ||
import org.gradle.api.tasks.SourceSet | ||
import org.vafer.jdependency.Clazz | ||
import org.vafer.jdependency.Clazzpath | ||
import org.vafer.jdependency.ClazzpathUnit | ||
|
||
/** Tracks unused classes in the project classpath. */ | ||
class UnusedTracker { | ||
private final List<String> entryPoints | ||
private final List<ClazzpathUnit> projectUnits | ||
private final Clazzpath cp = new Clazzpath() | ||
|
||
private UnusedTracker(List<File> classDirs, List<String> entryPoints) { | ||
this.entryPoints = entryPoints | ||
projectUnits = classDirs.collect { cp.addClazzpathUnit(it) } | ||
} | ||
|
||
Set<String> findUnused() { | ||
Set<Clazz> unused = cp.clazzes | ||
|
||
for (cpu in projectUnits) { | ||
unused.removeAll(cpu.clazzes) | ||
unused.removeAll(cpu.transitiveDependencies) | ||
} | ||
|
||
for (entryPoint in entryPoints) { | ||
Clazz clazz = cp.getClazz(entryPoint) | ||
if (clazz == null) { | ||
throw new RuntimeException("Entry point not found: " + className); | ||
} | ||
|
||
unused.remove(clazz) | ||
unused.removeAll(clazz.transitiveDependencies) | ||
} | ||
|
||
return unused.collect { it.name }.toSet() | ||
} | ||
|
||
void addDependency(File jarOrDir) { | ||
cp.addClazzpathUnit(jarOrDir) | ||
} | ||
|
||
static UnusedTracker forProject(Project project, List<String> entryPoints) { | ||
final List<File> classDirs = new ArrayList<>() | ||
for (SourceSet sourceSet in project.sourceSets) { | ||
File classDir = sourceSet.output.classesDir | ||
if (classDir.isDirectory()) { | ||
classDirs.add(classDir) | ||
} | ||
} | ||
|
||
new UnusedTracker(classDirs, entryPoints) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.