diff --git a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/DependencyTask.groovy b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/DependencyTask.groovy index c7848735..5dbfbb30 100644 --- a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/DependencyTask.groovy +++ b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/DependencyTask.groovy @@ -76,7 +76,7 @@ abstract class DependencyTask extends DefaultTask { } as AppDependencies } - private static Set convertDependenciesToArtifactInfo( + private static List convertDependenciesToArtifactInfo( AppDependencies appDependencies ) { return appDependencies.libraryList.stream() @@ -88,7 +88,8 @@ abstract class DependencyTask extends DefaultTask { library.mavenLibrary.version ) } - .collect(Collectors.toUnmodifiableSet()) + .sorted(Comparator.comparing { it.toString() }) + .collect(Collectors.toUnmodifiableList()) } private static void initOutput(File outputDir) { diff --git a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy index 851846b5..7dac8aae 100644 --- a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy +++ b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy @@ -25,6 +25,7 @@ import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.TaskAction import org.slf4j.LoggerFactory +import java.util.stream.Collectors import java.util.zip.ZipEntry import java.util.zip.ZipFile @@ -107,14 +108,16 @@ abstract class LicensesTask extends DefaultTask { writeMetadata() } - private static Set loadDependenciesJson(File jsonFile) { + private static List loadDependenciesJson(File jsonFile) { def allDependencies = new JsonSlurper().parse(jsonFile) def artifactInfoSet = new HashSet() for (entry in allDependencies) { ArtifactInfo artifactInfo = artifactInfoFromEntry(entry) artifactInfoSet.add(artifactInfo) } - artifactInfoSet.asImmutable() + artifactInfoSet.stream() + .sorted(Comparator.comparing { it.toString() }) + .collect(Collectors.toUnmodifiableList()) } private void addDebugLicense() { diff --git a/oss-licenses-plugin/src/test/java/com/google/android/gms/oss/licenses/plugin/DependencyTaskTest.java b/oss-licenses-plugin/src/test/java/com/google/android/gms/oss/licenses/plugin/DependencyTaskTest.java index e35a5490..50760521 100644 --- a/oss-licenses-plugin/src/test/java/com/google/android/gms/oss/licenses/plugin/DependencyTaskTest.java +++ b/oss-licenses-plugin/src/test/java/com/google/android/gms/oss/licenses/plugin/DependencyTaskTest.java @@ -29,6 +29,9 @@ import java.io.OutputStream; import java.lang.reflect.Type; import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; import org.gradle.api.Project; import org.gradle.testfixtures.ProjectBuilder; import org.junit.Before; @@ -84,8 +87,8 @@ public void testAction_valuesConvertedToJson() throws Exception { File outputJson = new File(outputDir, "test.json"); dependencyTask.getDependenciesJson().set(outputJson); ImmutableSet expectedArtifacts = ImmutableSet.of( - new ArtifactInfo("org.group.id", "artifactId", "1.0.0"), - new ArtifactInfo("org.group.other", "other-artifact", "3.2.1") + new ArtifactInfo("org.group.other", "other-artifact", "3.2.1"), + new ArtifactInfo("org.group.id", "artifactId", "1.0.0") ); AppDependencies appDependencies = createAppDependencies(expectedArtifacts); File protoFile = writeAppDependencies(appDependencies, temporaryFolder.newFile()); @@ -102,8 +105,8 @@ public void testAction_withNonMavenDeps_nonMavenDepsIgnored() throws Exception { File outputJson = new File(outputDir, "test.json"); dependencyTask.getDependenciesJson().set(outputJson); ImmutableSet expectedArtifacts = ImmutableSet.of( - new ArtifactInfo("org.group.id", "artifactId", "1.0.0"), - new ArtifactInfo("org.group.other", "other-artifact", "3.2.1") + new ArtifactInfo("org.group.other", "other-artifact", "3.2.1"), + new ArtifactInfo("org.group.id", "artifactId", "1.0.0") ); AppDependencies appDependencies = createAppDependencies(expectedArtifacts).toBuilder() .addLibrary(Library.getDefaultInstance()) // There aren't any other library types supported. @@ -131,11 +134,15 @@ public void testAction_depFileAbsent_writesAbsentDep() throws Exception { private void verifyExpectedDependencies(ImmutableSet expectedArtifacts, File outputJson) throws Exception { Gson gson = new Gson(); + List expectedArtifactsSorted = expectedArtifacts + .stream() + .sorted(Comparator.comparing(ArtifactInfo::toString)) + .collect(Collectors.toUnmodifiableList()); try (FileReader reader = new FileReader(outputJson)) { Type collectionOfArtifactInfo = new TypeToken>() { }.getType(); Collection jsonArtifacts = gson.fromJson(reader, collectionOfArtifactInfo); - assertThat(jsonArtifacts).containsExactlyElementsIn(expectedArtifacts); + assertThat(jsonArtifacts).containsExactlyElementsIn(expectedArtifactsSorted).inOrder(); } }