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

Avoid duplicate archives (due to transitive dependencies) #85

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions src/main/java/me/champeau/gradle/japicmp/JApiCmpWorkerAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class JApiCmpWorkerAction extends JapiCmpWorkerConfiguration implements Runnable {
Expand Down Expand Up @@ -410,5 +411,18 @@ public String toString() {
public JApiCmpArchive toJapicmpArchive() {
return new JApiCmpArchive(file, version);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
return obj instanceof Archive && hashCode() == obj.hashCode();
}

@Override
public int hashCode() {
return Objects.hash(file.getName(), version);
}
}
}
13 changes: 6 additions & 7 deletions src/main/java/me/champeau/gradle/japicmp/JapicmpTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -225,15 +227,12 @@ private static List<JApiCmpWorkerAction.Archive> toArchives(FileCollection fc) {
return archives;
}

private void collectArchives(final List<JApiCmpWorkerAction.Archive> archives, ResolvedDependency resolvedDependency) {
String version = resolvedDependency.getModule().getId().getVersion();
private void collectArchives(final Collection<JApiCmpWorkerAction.Archive> archives, ResolvedDependency resolvedDependency) {
Set<ResolvedArtifact> allModuleArtifacts = resolvedDependency.getAllModuleArtifacts();
for (ResolvedArtifact resolvedArtifact : allModuleArtifacts) {
String version = resolvedArtifact.getModuleVersion().getId().getVersion();
archives.add(new JApiCmpWorkerAction.Archive(resolvedArtifact.getFile(), version));
}
for (ResolvedDependency dependency : resolvedDependency.getChildren()) {
collectArchives(archives, dependency);
}
}

public void richReport(Action<? super RichReport> configureAction) {
Expand Down Expand Up @@ -308,7 +307,7 @@ public void addExcludeFilter(Class<? extends Filter> excludeFilterClass) {
public abstract ListProperty<JApiCmpWorkerAction.Archive> getOldArchiveList();

public void addOldArchives(Configuration config) {
List<JApiCmpWorkerAction.Archive> oldArchives = new ArrayList<>();
Set<JApiCmpWorkerAction.Archive> oldArchives = new LinkedHashSet<>();
for (ResolvedDependency resolvedDependency : config.getResolvedConfiguration().getFirstLevelModuleDependencies()) {
collectArchives(oldArchives, resolvedDependency);
}
Expand All @@ -320,7 +319,7 @@ public void addOldArchives(Configuration config) {
public abstract ListProperty<JApiCmpWorkerAction.Archive> getNewArchiveList();

public void addNewArchives(Configuration config) {
List<JApiCmpWorkerAction.Archive> newArchives = new ArrayList<>();
Set<JApiCmpWorkerAction.Archive> newArchives = new LinkedHashSet<>();
for (ResolvedDependency resolvedDependency : config.getResolvedConfiguration().getFirstLevelModuleDependencies()) {
collectArchives(newArchives, resolvedDependency);
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/groovy/me/champeau/gradle/ReportsFunctionalTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ class ReportsFunctionalTest extends BaseFunctionalTest {
result.task(":japicmpXmlWithVersions").outcome == TaskOutcome.UP_TO_DATE
}

def "can generate an XML report with no duplicate transitive versions"() {
when:
def result = run 'japicmpXmlWithTransitiveVersions'

then:
result.task(":japicmpXmlWithTransitiveVersions").outcome == TaskOutcome.SUCCESS
hasXmlReport('oldJar="assertj-guava-3.25.3.jar;assertj-core-3.25.3.jar;byte-buddy-1.14.11.jar;assertj-joda-time-2.2.0.jar"')
hasXmlReport('newJar="assertj-guava-3.26.3.jar;assertj-core-3.26.3.jar;byte-buddy-1.14.18.jar;assertj-joda-time-2.1.0.jar"')
hasXmlReport('oldVersion="3.25.3;3.25.3;1.14.11;2.2.0"')
hasXmlReport('newVersion="3.26.3;3.26.3;1.14.18;2.1.0"')

noTxtReport()
noMarkdownReport()
noSemverReport()
noHtmlReport()
noRichReport()

when:
result = run 'japicmpXmlWithTransitiveVersions'

then:
result.task(":japicmpXmlWithTransitiveVersions").outcome == TaskOutcome.UP_TO_DATE
}

def "can generate rich report"() {
when:
def result = run 'japicmpRich'
Expand Down
14 changes: 14 additions & 0 deletions src/test/test-projects/html-report/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ repositories {
configurations {
baseline
current
baseline2
current2
}

dependencies {
baseline 'org.apache.commons:commons-lang3:3.5'
current 'org.apache.commons:commons-lang3:3.6'

baseline2 'org.assertj:assertj-guava:3.25.3'
baseline2 'org.assertj:assertj-joda-time:2.2.0'
current2 'org.assertj:assertj-guava:3.26.3'
current2 'org.assertj:assertj-joda-time:2.1.0'
}

task japicmp(type: me.champeau.gradle.japicmp.JapicmpTask) {
Expand Down Expand Up @@ -61,3 +68,10 @@ task japicmpXmlWithVersions(type: me.champeau.gradle.japicmp.JapicmpTask) {
addNewArchives(configurations.current)
xmlOutputFile = layout.buildDirectory.file('reports/japi.xml').get().asFile
}

task japicmpXmlWithTransitiveVersions(type: me.champeau.gradle.japicmp.JapicmpTask) {
addOldArchives(configurations.baseline2)
addNewArchives(configurations.current2)
ignoreMissingClasses = true
xmlOutputFile = layout.buildDirectory.file('reports/japi.xml').get().asFile
}