Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Dependencies with ignored scopes don't appear in the pom-debloated.xml bug #153

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -13,6 +14,7 @@
import se.kth.depclean.core.model.ClassName;
import se.kth.depclean.core.model.Dependency;
import se.kth.depclean.core.model.ProjectContext;
import se.kth.depclean.core.model.Scope;

/**
* Builds the analysis given the declared dependencies and the one actually used.
Expand Down Expand Up @@ -60,6 +62,12 @@ public ProjectDependencyAnalysis analyse() {
ignoreDependency(usedInheritedDirectDependencies, unusedInheritedDirectDependencies, dependencyToIgnore);
ignoreDependency(usedInheritedTransitiveDependencies, unusedInheritedTransitiveDependencies, dependencyToIgnore);
});
// ignore scopes
ignoreDependencyWithIgnoredScope(usedDirectDependencies, unusedDirectDependencies, context.getIgnoredScopes());
ignoreDependencyWithIgnoredScope(usedTransitiveDependencies, unusedTransitiveDependencies, context.getIgnoredScopes());
ignoreDependencyWithIgnoredScope(usedInheritedDirectDependencies, unusedInheritedDirectDependencies, context.getIgnoredScopes());
ignoreDependencyWithIgnoredScope(usedInheritedTransitiveDependencies, unusedInheritedTransitiveDependencies, context.getIgnoredScopes());

return new ProjectDependencyAnalysis(
usedDirectDependencies,
usedTransitiveDependencies,
Expand Down Expand Up @@ -138,8 +146,8 @@ private Set<Dependency> getUnusedDependencies(Set<Dependency> baseDependencies,
}

/**
* If the dependency to ignore is an unused dependency, then add it to the set of usedDependencyCoordinates
* and remove it from the set of unusedDependencyCoordinates.
* If the dependency to ignore is an unused dependency, then add it to the set of usedDependencyCoordinates and remove it from the set of
* unusedDependencyCoordinates.
*
* @param usedDependencies The set of used artifacts where the dependency will be added.
* @param unusedDependencies The set of unused artifacts where the dependency will be removed.
Expand All @@ -155,4 +163,26 @@ private void ignoreDependency(Set<Dependency> usedDependencies, Set<Dependency>
}
}
}

/**
* If the scope of the unused dependency is to be ignored, then add the dependency to the set of used dependencies and remove it from the used set.
*
* @param usedDependencies The set of used artifacts where the dependency will be added.
* @param unusedDependencies The set of unused artifacts where the dependency will be removed.
* @param ignoredScopes The set of scopes to ignore.
*/
private void ignoreDependencyWithIgnoredScope(Set<Dependency> usedDependencies, Set<Dependency> unusedDependencies, Set<Scope> ignoredScopes) {
for (Iterator<Dependency> i = unusedDependencies.iterator(); i.hasNext(); ) {
Dependency unusedDependency = i.next();
List<String> scopesToIgnore = ignoredScopes.stream().map(Scope::getValue).collect(Collectors.toList());
log.debug("Scopes to ignore: {}", scopesToIgnore);
log.debug("Unused dependency scope: {}", unusedDependency.getScope());
if (scopesToIgnore.contains(unusedDependency.getScope())) {
log.debug("Ignoring dependency {} with scope {}", unusedDependency, unusedDependency.getScope());
usedDependencies.add(unusedDependency);
i.remove();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,25 @@ public boolean ignoreTests() {

private void populateDependenciesAndClassesMap(Set<Dependency> dependencies) {
dependencies.stream()
.filter(this::excludeScopes)
.filter(this::excludeDependenciesBasedOnIgnoredScopes)
.forEach(dc -> {
log.debug("Adding dependency {} with related classes: {}", dc, dc.getRelatedClasses());
classesPerDependency.putAll(dc, dc.getRelatedClasses());
});


}

private boolean excludeScopes(Dependency dc) {
/**
* Exclude dependencies based on the scopes.
*
* @param dc the dependency to check
* @return true if the dependency should be excluded, false otherwise
*/
private boolean excludeDependenciesBasedOnIgnoredScopes(Dependency dc) {
final String declaredScope = dc.getScope();
log.debug("ignoreScopes: " + ignoredScopes);
log.debug("dc = " + dc + " declaredScope = " + declaredScope);
return ignoredScopes.stream()
.map(Scope::getValue)
.noneMatch(declaredScope::equalsIgnoreCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,37 @@ void unused_inherited_exists(MavenExecutionResult result) {
" org.apiguardian:apiguardian-api:1.1.2:test (6 KB)"
);
}


@MavenTest
@Disabled
void ignored_scopes(MavenExecutionResult result) {
log.trace("Test that DepClean ignores dependencies (considers them as used) with the ignored scopes");
assertThat(result).isSuccessful().out()
.plain().contains(
"-------------------------------------------------------",
" D E P C L E A N A N A L Y S I S R E S U L T S",
"-------------------------------------------------------",
"USED DIRECT DEPENDENCIES [2]: ",
" com.fasterxml.jackson.core:jackson-core:2.12.2:compile (356 KB)",
" commons-io:commons-io:2.11.0:test (319 KB)",
"USED TRANSITIVE DEPENDENCIES [2]: ",
" com.fasterxml.jackson.core:jackson-core:2.12.2:provided (356 KB)",
" com.fasterxml.jackson.core:jackson-annotations:2.12.2:provided (73 KB)",
"USED INHERITED DIRECT DEPENDENCIES [0]: ",
"USED INHERITED TRANSITIVE DEPENDENCIES [0]: ",
"POTENTIALLY UNUSED DIRECT DEPENDENCIES [1]: ",
" com.google.guava:guava:31.0.1-jre:compile (2 MB)",
"POTENTIALLY UNUSED TRANSITIVE DEPENDENCIES [6]: ",
" org.checkerframework:checker-qual:3.12.0:compile (203 KB)",
" com.google.code.findbugs:jsr305:3.0.2:compile (19 KB)",
" com.google.errorprone:error_prone_annotations:2.7.1:compile (14 KB)",
" com.google.j2objc:j2objc-annotations:1.3:compile (8 KB)",
" com.google.guava:failureaccess:1.0.1:compile (4 KB)",
" com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava:compile (2 KB)",
"POTENTIALLY UNUSED INHERITED DIRECT DEPENDENCIES [0]: ",
"POTENTIALLY UNUSED INHERITED TRANSITIVE DEPENDENCIES [0]: "
);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.foo.bar</groupId>
<artifactId>foobar</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>foobar</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<!-- Jackson's dependencies should always be considered as used because the "provided" scope is ignored -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.2</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>

<!-- Commons IO should be always considered as used because the "test" scope is ignored -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.soebes.itf.jupiter.extension</groupId>
<artifactId>itf-failure-plugin</artifactId>
<version>0.9.0</version>
<executions>
<execution>
<id>first_very_simple</id>
<phase>initialize</phase>
<goals>
<goal>failure</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>se.kth.castor</groupId>
<artifactId>depclean-maven-plugin</artifactId>
<version>2.0.5</version>
<executions>
<execution>
<goals>
<goal>depclean</goal>
</goals>
<configuration>
<ignoreScopes>test,provided,import,runtime</ignoreScopes>
<createPomDebloated>true</createPomDebloated>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class Main {
int field = 42;
}