in src/main/java/org/apache/sling/maven/jspc/DependencyTracker.java [73:141]
public void processCompileDependencies() {
compileScopeArtifacts.forEach(artifact -> {
unusedDependencies.add(artifact.getId());
try (JarFile jar = new JarFile(artifact.getFile())) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry e = entries.nextElement();
if (e.isDirectory()) {
continue;
}
String path = e.getName();
if (path.endsWith(".class")) {
path = StringUtils.chomp(path, "/");
if (path.charAt(0) == '/') {
path = path.substring(1);
}
String packageName = path.replace("/", ".");
Set<String> artifacts = packageProviders.computeIfAbsent(packageName, k -> new HashSet<>());
artifacts.add(artifact.getId());
}
}
} catch (IOException e) {
logger.error("Error while accessing jar file " + artifact.getFile().getAbsolutePath(), e);
}
});
List<String> packages = new ArrayList<>(classLoader.getPackageNames());
Collections.sort(packages);
for (String packageName: packages) {
Set<String> artifacts = packageProviders.get(packageName);
if (artifacts != null && !artifacts.isEmpty()) {
artifacts.forEach(unusedDependencies::remove);
}
}
jspInfo.forEach((jsp, pageInfo) -> {
List dependencies = pageInfo.getDependants();
if (!dependencies.isEmpty()) {
Set<String> dependenciesSet =
jspDependencies.computeIfAbsent(Paths.get(relativeSourceDirectory.toString(), jsp).toString(), key -> new HashSet<>());
for (Object d : dependencies) {
String dependency = (String) d;
try {
URL dependencyURL = jspCServletContext.getResource(dependency);
if (dependencyURL != null) {
Path dependencyPath = Paths.get(dependencyURL.getPath());
if (dependencyPath.startsWith(sourceDirectory)) {
dependenciesSet.add(projectDirectory.relativize(dependencyPath).toString());
} else {
dependenciesSet.add(dependencyURL.toExternalForm());
}
} else {
// the dependency comes from a JAR
if (dependency.startsWith("/")) {
dependency = dependency.substring(1);
}
JarURLConnection jarURLConnection = (JarURLConnection) classLoader.findResource(dependency).openConnection();
for (Artifact a : compileScopeArtifacts) {
if (a.getFile().getAbsolutePath().equals(jarURLConnection.getJarFile().getName())) {
unusedDependencies.remove(a.getId());
}
}
dependenciesSet.add(Paths.get(jarURLConnection.getJarFileURL().getPath()).getFileName() + ":/" + dependency);
}
} catch (IOException e) {
dependenciesSet.add(dependency);
}
}
}
});
}