Skip to content

About

Provides an API to analyse and modify class dependencies. It provides the core to the maven shade plugin for removing unused classes.

Topics

Resources

Stars

89 stars

Watchers

5 watching

Forks

Latest commit

 

History

707 Commits

Folders and files

Repository files navigation

Build Status Coverage Status Maven Central

jdependency - explore your classpath

jdependency is small library that helps you analyze class level dependencies, clashes and missing classes.

Check the documentation on how to use it with javadocs and a source xref is also available.

Where to get it

The jars are available on maven central. The source releases you can get in the release section.

If feel adventures or want to help out feel free to get the latest code via git.

git clone git://github.com/tcurdt/jdependency.git

How to use it

final File jar1 = ...
final File jar2 = ...

or

final Path jar1 = ...
final Path jar2 = ...

finding classpath clashes

final Clazzpath cp = new Clazzpath();
cp.addClazzpathUnit(jar1, "jar1.jar");
cp.addClazzpathUnit(jar2, "jar2.jar");

final Set<Clazz> clashed = cp.getClashedClazzes();
for(Clazz clazz : clashed) {
  System.out.println("class " + clazz + " is contained in " + clazz.getClasspathUnits());
}

finding different class versions

final Clazzpath cp = new Clazzpath(true);
cp.addClazzpathUnit(jar1, "jar1.jar");
cp.addClazzpathUnit(jar2, "jar2.jar");

final Set<Clazz> clashed = cp.getClashedClazzes();

final Set<Clazz> uniq = clashed.stream()
  .filter(c -> c.getVersions().size() == 1)
  .collect(Collectors.toSet());

clashed.removeAll(uniq);

for(Clazz clazz : clashed) {
  System.out.println("class " + clazz + " differs accross " + clazz.getClasspathUnits());
}

finding missing classes

final Clazzpath cp = new Clazzpath();
cp.addClazzpathUnit(jar1, "jar1.jar");

final Set<Clazz> missing = cp.getMissingClazzes();
for(Clazz clazz : missing) {
  System.out.println("class " + clazz + " is missing");
}

finding unused classes

final Clazzpath cp = new Clazzpath();
final ClazzpathUnit artifact = cp.addClazzpathUnit(jar1, "artifact.jar");
cp.addClazzpathUnit(jar2, "dependency.jar");

final Set<Clazz> removable = cp.getClazzes();
removable.removeAll(artifact.getClazzes());
removable.removeAll(artifact.getTransitiveDependencies());

for(Clazz clazz : removable) {
  System.out.println("class " + clazz + " is not required");
}

including declared dependencies

Some classes are only loaded reflectively and are referenced from resources instead of bytecode (like META-INF/services). Without knowing about these they look unused. Declarations add an edge from the declared trigger (like the service interface) to the declared class, so it is part of the transitive dependencies whenever the trigger is.

final Clazzpath cp = new Clazzpath(false, DeclaredDependencies.all());
final ClazzpathUnit artifact = cp.addClazzpathUnit(jar1, "artifact.jar");
cp.addClazzpathUnit(jar2, "dependency.jar");

final Set<Clazz> removable = cp.getClazzes();
removable.removeAll(artifact.getClazzes());
removable.removeAll(artifact.getTransitiveDependencies());
Declaration Resource
DeclaredDependencies.services() META-INF/services/*
DeclaredDependencies.moduleInfo() provides in module-info.class
DeclaredDependencies.springImports() META-INF/spring/*.imports
DeclaredDependencies.springFactories() META-INF/spring.factories
DeclaredDependencies.sisuIndex() META-INF/sisu/*

Other formats listing one class per line can be added with DeclaredDependencies.classList(prefix, suffix) or by implementing DeclaredDependencies.

Related projects

provides a report of the dependencies used/unused and provides a debloated version of the pom.xml

Project Description
maven-shade-plugin allows to inline and optimize dependencies into a single jar
gradle-lean gradle version of the maven-shade plugin (stale)
shadow gradle version of the maven-shade plugin
jarjar allows to inline and optimize dependencies into a single jar (stale)
proguard obfuscator, shrinker (GPL)
DepClean provides a report of the dependencies used/unused and provides a debloated version of the pom.xml

License

All code and data is released under the Apache License 2.0.

About

Provides an API to analyse and modify class dependencies. It provides the core to the maven shade plugin for removing unused classes.

Topics

Resources

Stars

89 stars

Watchers

5 watching

Forks

Releases

Used by

Contributors

Languages