18
18
import static com .google .common .collect .ImmutableSet .toImmutableSet ;
19
19
20
20
import com .google .idea .blaze .common .Context ;
21
+ import com .google .idea .blaze .common .PrintOutput ;
21
22
import com .google .idea .blaze .qsync .artifacts .BuildArtifact ;
22
23
import com .google .idea .blaze .qsync .deps .ArtifactDirectories ;
23
24
import com .google .idea .blaze .qsync .deps .ArtifactDirectoryBuilder ;
27
28
import com .google .idea .blaze .qsync .deps .ProjectProtoUpdateOperation ;
28
29
import com .google .idea .blaze .qsync .deps .TargetBuildInfo ;
29
30
import com .google .idea .blaze .qsync .project .ProjectProto .JarDirectory ;
31
+ import java .util .Comparator ;
30
32
import java .util .HashMap ;
31
33
import java .util .HashSet ;
32
34
import java .util .Map ;
33
35
import java .util .Set ;
36
+ import java .util .stream .Collectors ;
34
37
35
38
/** Adds compiled jars from dependencies to the project. */
36
39
public class AddCompiledJavaDeps implements ProjectProtoUpdateOperation {
@@ -43,18 +46,32 @@ public AddCompiledJavaDeps(boolean enableBazelAdditionalLibraryRootsProvider) {
43
46
@ Override
44
47
public void update (ProjectProtoUpdate update , ArtifactTracker .State artifactState , Context <?> context ) {
45
48
ArtifactDirectoryBuilder javaDepsDir = update .artifactDirectory (ArtifactDirectories .JAVADEPS );
49
+ Set <String > skipped = new HashSet <>();
50
+ Set <String > seen = new HashSet <>();
46
51
Map <String , Set <String >> libNameToJars = new HashMap <>();
47
52
for (TargetBuildInfo target : artifactState .targets ()) {
48
53
if (target .javaInfo ().isPresent ()) {
49
54
JavaArtifactInfo javaInfo = target .javaInfo ().get ();
50
- for (BuildArtifact jar : javaInfo .jars ()) {
55
+ Set <String > locallySeen = new HashSet <>();
56
+ for (BuildArtifact jar :
57
+ javaInfo .jars ().stream ()
58
+ // Prefer already added artifactPaths.
59
+ .sorted (Comparator .comparing (it -> !seen .contains (it .artifactPath ().toString ())))
60
+ .toList ()) {
61
+ if (locallySeen .contains (jar .digest ())) {
62
+ skipped .add (jar .artifactPath ().toString ());
63
+ continue ;
64
+ }
65
+ locallySeen .add (jar .digest ());
66
+ seen .add (jar .artifactPath ().toString ());
51
67
javaDepsDir .addIfNewer (jar .artifactPath (), jar , target .buildContext ());
52
68
libNameToJars
53
- .computeIfAbsent (target .label ().toString (), t -> new HashSet ())
69
+ .computeIfAbsent (target .label ().toString (), t -> new HashSet <> ())
54
70
.add (javaDepsDir .path ().resolve (jar .artifactPath ()).toString ());
55
71
}
56
72
}
57
73
}
74
+ context .output (PrintOutput .output ("Skipped " + skipped .size () + " duplicate jars" ));
58
75
if (!enableBazelAdditionalLibraryRootsProvider ) {
59
76
updateProjectProtoUpdateAllJarsInOneLibrary (javaDepsDir , update );
60
77
} else {
0 commit comments