Skip to content

Commit baebbdc

Browse files
authored
Manipulating classpath items should preserve item order (#865)
`getClasspathElements` computes the compilation and test compilation classpath from `MavenProject#getCompileClasspathElements` which is a `List<String>`. Transforming from the ordered `List<String>` into an unordered `Hashset<File>` can change the order of classpath elements. While this didn't cause a compilation issue in my case it could. It did break the [develocity cache key computation](https://develocity.apache.org/c/unuozmm6ecqpc/nusnauq3l6a6u/goal-inputs?expanded=WyJuemRibm96bGFrZ2FnLWNsYXNzcGF0aGVsZW1lbnRzIiwibnpkYm5vemxha2dhZy1jbGFzc3BhdGhFbGVtZW50cy0wLWZpbGUtb3JkZXIiXQ) I'm using to improve Apache James build times.
1 parent a25b942 commit baebbdc

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/main/java/util/FileUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public static File fileOf(File f, boolean canonical) throws Exception {
3636
}
3737

3838
public static Set<File> fromStrings(Collection<String> s) {
39-
return s.stream().map(File::new).collect(Collectors.toSet());
39+
return s.stream().map(File::new).collect(
40+
Collectors.toCollection(LinkedHashSet::new)
41+
);
4042
}
4143

4244
public static String toMultiPath(Collection<File> paths) {

src/test/java/util/FileUtilsTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* This is free and unencumbered software released into the public domain.
3+
* See UNLICENSE.
4+
*/
5+
package util;
6+
7+
import com.google.common.collect.Lists;
8+
import junit.framework.TestCase;
9+
import org.junit.Test;
10+
11+
import java.io.File;
12+
import java.util.List;
13+
import java.util.Set;
14+
15+
import static org.junit.Assert.assertEquals;
16+
17+
public class FileUtilsTest {
18+
@Test
19+
public void fromStrings_should_preserve_insertion_order(){
20+
List<String> expected = Lists.newArrayList("5.jar", "23.jar", "56.jar", "2.jar", "48.jar", "99.jar", "1234.jar");
21+
Set<File> files = FileUtils.fromStrings(expected);
22+
List<String> actual = Lists.newArrayList();
23+
for (File file : files) {
24+
actual.add(file.getName());
25+
}
26+
assertEquals(actual, expected);
27+
}
28+
}

0 commit comments

Comments
 (0)